-
Notifications
You must be signed in to change notification settings - Fork 43
/
info.go
111 lines (89 loc) · 2.82 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2022 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
//
// Package info print the host information.
package info
import (
"fmt"
"reflect"
"strconv"
hoststat "github.com/likexian/host-stat-go"
"github.com/spf13/cobra"
cmdutil "github.com/superproj/onex/internal/onexctl/cmd/util"
"github.com/superproj/onex/internal/onexctl/util/templates"
"github.com/superproj/onex/pkg/cli/genericclioptions"
iputil "github.com/superproj/onex/pkg/util/ip"
)
// 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
onexctl 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
}