forked from sensu/sensu-go
/
info.go
122 lines (112 loc) · 2.53 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
112
113
114
115
116
117
118
119
120
121
122
package handler
import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/cli/elements/list"
"github.com/sensu/sensu-go/cli/elements/table"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
)
// InfoCommand defines new check info command
func InfoCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "info [ID]",
Short: "show detailed handler 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 handler from API
name := args[0]
r, err := cli.Client.FetchHandler(name)
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 {
handler, ok := v.(*types.Handler)
if !ok {
return fmt.Errorf("%t is not a Handler", v)
}
// Determine what will be executed based on the type
var execute string
switch handler.Type {
case types.HandlerTCPType:
fallthrough
case types.HandlerUDPType:
execute = fmt.Sprintf(
"%s %s://%s:%d",
table.TitleStyle("PUSH:"),
handler.Type,
handler.Socket.Host,
handler.Socket.Port,
)
case types.HandlerPipeType:
execute = fmt.Sprintf(
"%s %s",
table.TitleStyle("RUN:"),
handler.Command,
)
case types.HandlerSetType:
execute = fmt.Sprintf(
"%s %s",
table.TitleStyle("CALL:"),
strings.Join(handler.Handlers, ","),
)
default:
execute = "UNKNOWN"
}
cfg := &list.Config{
Title: handler.Name,
Rows: []*list.Row{
{
Label: "Name",
Value: handler.Name,
},
{
Label: "Type",
Value: handler.Type,
},
{
Label: "Timeout",
Value: strconv.FormatInt(int64(handler.Timeout), 10),
},
{
Label: "Filters",
Value: strings.Join(handler.Filters, ", "),
},
{
Label: "Mutator",
Value: handler.Mutator,
},
{
Label: "Execute",
Value: execute,
},
{
Label: "Environment Variables",
Value: strings.Join(handler.EnvVars, ", "),
},
{
Label: "Runtime Assets",
Value: strings.Join(handler.RuntimeAssets, ", "),
},
},
}
return list.Print(writer, cfg)
}