Files
open-im-server/tools/imctl/internal/imctl/cmd/info/info.go
T
Xinwei Xiong aa729a24b6 feat: set docs openim-server url address fix (#1043)
* feat: set docs openim-server url address fix

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>

* fix: add openim code fix

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>

---------

Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-09-08 07:20:30 +00:00

120 lines
3.2 KiB
Go

// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package info print the host information.
package info
import (
"fmt"
"reflect"
"strconv"
hoststat "github.com/likexian/host-stat-go"
"github.com/openim-sigs/component-base/util/iputil"
"github.com/spf13/cobra"
cmdutil "github.com/openimsdk/open-im-server/tools/imctl/internal/imctl/cmd/util"
"github.com/openimsdk/open-im-server/tools/imctl/pkg/cli/genericclioptions"
"github.com/openimsdk/open-im-server/tools/imctl/pkg/util/templates"
)
// Info defines the host information struct.
type Info struct {
HostName string
IPAddress string
OSRelease string
CPUCore uint64
MemTotal string
MemFree string
}
// InfoOptions is an options struct to support 'info' sub command.
type InfoOptions struct {
genericclioptions.IOStreams
}
var infoExample = templates.Examples(`
# Print the host information
imctl info`)
// NewInfoOptions returns an initialized InfoOptions instance.
func NewInfoOptions(ioStreams genericclioptions.IOStreams) *InfoOptions {
return &InfoOptions{
IOStreams: ioStreams,
}
}
// NewCmdInfo returns new initialized instance of 'info' sub command.
func NewCmdInfo(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
o := NewInfoOptions(ioStreams)
cmd := &cobra.Command{
Use: "info",
DisableFlagsInUseLine: true,
Aliases: []string{},
Short: "Print the host information",
Long: "Print the host information.",
Example: infoExample,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Run(args))
},
SuggestFor: []string{},
}
return cmd
}
// Run executes an info sub command using the specified options.
func (o *InfoOptions) Run(args []string) error {
var info Info
hostInfo, err := hoststat.GetHostInfo()
if err != nil {
return fmt.Errorf("get host info failed!error:%w", err)
}
info.HostName = hostInfo.HostName
info.OSRelease = hostInfo.Release + " " + hostInfo.OSBit
memStat, err := hoststat.GetMemStat()
if err != nil {
return fmt.Errorf("get mem stat failed!error:%w", err)
}
info.MemTotal = strconv.FormatUint(memStat.MemTotal, 10) + "M"
info.MemFree = strconv.FormatUint(memStat.MemFree, 10) + "M"
info.IPAddress = iputil.GetLocalIP()
cpuStat, err := hoststat.GetCPUInfo()
if err != nil {
return fmt.Errorf("get cpu stat failed!error:%w", err)
}
info.CPUCore = cpuStat.CoreCount
s := reflect.ValueOf(&info).Elem()
typeOfInfo := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
v := fmt.Sprintf("%v", f.Interface())
if v != "" {
fmt.Fprintf(o.Out, "%12s %v\n", typeOfInfo.Field(i).Name+":", f.Interface())
}
}
return nil
}