/
info.go
106 lines (97 loc) · 2.2 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
package entity
import (
"errors"
"fmt"
"io"
"strings"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/cli/commands/timeutil"
"github.com/sensu/sensu-go/cli/elements/globals"
"github.com/sensu/sensu-go/cli/elements/list"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
)
// InfoCommand defines new entity info command
func InfoCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "info [ID]",
Short: "show detailed entity information",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
_ = cmd.Help()
return errors.New("invalid argument(s) received")
}
// Fetch handlers from API
entityName := args[0]
r, err := cli.Client.FetchEntity(entityName)
if err != nil {
return err
}
// Determine the format to use to output the data
flag := helpers.GetChangedStringValueFlag("format", cmd.Flags())
format := cli.Config.Format()
return helpers.PrintFormatted(flag, format, r, cmd.OutOrStdout(), printToList)
},
}
helpers.AddFormatFlag(cmd.Flags())
return cmd
}
func printToList(v interface{}, writer io.Writer) error {
r, ok := v.(*types.Entity)
if !ok {
return fmt.Errorf("%t is not an Entity", v)
}
cfg := &list.Config{
Title: r.Name,
Rows: []*list.Row{
{
Label: "Name",
Value: r.Name,
},
{
Label: "Entity Class",
Value: r.EntityClass,
},
{
Label: "Subscriptions",
Value: strings.Join(r.Subscriptions, ", "),
},
{
Label: "Last Seen",
Value: timeutil.HumanTimestamp(r.LastSeen),
},
{
Label: "Hostname",
Value: r.System.Hostname,
},
{
Label: "OS",
Value: r.System.OS,
},
{
Label: "Platform",
Value: r.System.Platform,
},
{
Label: "Platform Family",
Value: r.System.PlatformFamily,
},
{
Label: "Platform Version",
Value: r.System.PlatformVersion,
},
// TODO: Network interfaces
{
Label: "Auto-Deregistration",
Value: globals.BooleanStyleP(r.Deregister),
},
{
Label: "Deregistration Handler",
Value: r.Deregistration.Handler,
},
},
}
return list.Print(writer, cfg)
}