1package check
2
3import (
4	"errors"
5	"io"
6	"strconv"
7	"strings"
8
9	"github.com/sensu/sensu-go/cli"
10	"github.com/sensu/sensu-go/cli/commands/flags"
11	"github.com/sensu/sensu-go/cli/commands/helpers"
12	"github.com/sensu/sensu-go/cli/elements/globals"
13	"github.com/sensu/sensu-go/cli/elements/table"
14	"github.com/sensu/sensu-go/types"
15
16	"github.com/spf13/cobra"
17)
18
19// ListCommand defines new list events command
20func ListCommand(cli *cli.SensuCli) *cobra.Command {
21	cmd := &cobra.Command{
22		Use:          "list",
23		Short:        "list checks",
24		SilenceUsage: true,
25		RunE: func(cmd *cobra.Command, args []string) error {
26			if len(args) != 0 {
27				_ = cmd.Help()
28				return errors.New("invalid argument(s) received")
29			}
30			namespace := cli.Config.Namespace()
31			if ok, _ := cmd.Flags().GetBool(flags.AllNamespaces); ok {
32				namespace = types.NamespaceTypeAll
33			}
34
35			opts, err := helpers.ListOptionsFromFlags(cmd.Flags())
36			if err != nil {
37				return err
38			}
39
40			// Fetch checks from the API
41			results, err := cli.Client.ListChecks(namespace, &opts)
42			if err != nil {
43				return err
44			}
45
46			// Print the results based on the user preferences
47			resources := []types.Resource{}
48			for i := range results {
49				resources = append(resources, &results[i])
50			}
51			return helpers.Print(cmd, cli.Config.Format(), printToTable, resources, results)
52		},
53	}
54
55	helpers.AddFormatFlag(cmd.Flags())
56	helpers.AddAllNamespace(cmd.Flags())
57	helpers.AddFieldSelectorFlag(cmd.Flags())
58	helpers.AddLabelSelectorFlag(cmd.Flags())
59	helpers.AddChunkSizeFlag(cmd.Flags())
60
61	return cmd
62}
63
64func printToTable(results interface{}, writer io.Writer) {
65	table := table.New([]*table.Column{
66		{
67			Title:       "Name",
68			ColumnStyle: table.PrimaryTextStyle,
69			CellTransformer: func(data interface{}) string {
70				check, ok := data.(types.CheckConfig)
71				if !ok {
72					return cli.TypeError
73				}
74				return check.Name
75			},
76		},
77		{
78			Title: "Command",
79			CellTransformer: func(data interface{}) string {
80				check, ok := data.(types.CheckConfig)
81				if !ok {
82					return cli.TypeError
83				}
84				return check.Command
85			},
86		},
87		{
88			Title: "Interval",
89			CellTransformer: func(data interface{}) string {
90				check, ok := data.(types.CheckConfig)
91				if !ok {
92					return cli.TypeError
93				}
94				interval := strconv.FormatUint(uint64(check.Interval), 10)
95				return interval
96			},
97		},
98		{
99			Title: "Cron",
100			CellTransformer: func(data interface{}) string {
101				check, ok := data.(types.CheckConfig)
102				if !ok {
103					return cli.TypeError
104				}
105				return check.Cron
106			},
107		},
108		{
109			Title: "Timeout",
110			CellTransformer: func(data interface{}) string {
111				check, ok := data.(types.CheckConfig)
112				if !ok {
113					return cli.TypeError
114				}
115				timeout := strconv.FormatUint(uint64(check.Timeout), 10)
116				return timeout
117			},
118		},
119		{
120			Title: "TTL",
121			CellTransformer: func(data interface{}) string {
122				check, ok := data.(types.CheckConfig)
123				if !ok {
124					return cli.TypeError
125				}
126				ttl := strconv.FormatUint(uint64(check.Ttl), 10)
127				return ttl
128			},
129		},
130		{
131			Title: "Subscriptions",
132			CellTransformer: func(data interface{}) string {
133				check, ok := data.(types.CheckConfig)
134				if !ok {
135					return cli.TypeError
136				}
137				return strings.Join(check.Subscriptions, ",")
138			},
139		},
140		{
141			Title: "Handlers",
142			CellTransformer: func(data interface{}) string {
143				check, ok := data.(types.CheckConfig)
144				if !ok {
145					return cli.TypeError
146				}
147				return strings.Join(check.Handlers, ",")
148			},
149		},
150		{
151			Title: "Assets",
152			CellTransformer: func(data interface{}) string {
153				check, ok := data.(types.CheckConfig)
154				if !ok {
155					return cli.TypeError
156				}
157				return strings.Join(check.RuntimeAssets, ",")
158			},
159		},
160		{
161			Title: "Hooks",
162			CellTransformer: func(data interface{}) string {
163				check, ok := data.(types.CheckConfig)
164				if !ok {
165					return cli.TypeError
166				}
167				return globals.FormatHookLists(check.CheckHooks)
168			},
169		},
170		{
171			Title: "Publish?",
172			CellTransformer: func(data interface{}) string {
173				check, ok := data.(types.CheckConfig)
174				if !ok {
175					return cli.TypeError
176				}
177				return globals.BooleanStyleP(check.Publish)
178			},
179		},
180		{
181			Title: "Stdin?",
182			CellTransformer: func(data interface{}) string {
183				check, ok := data.(types.CheckConfig)
184				if !ok {
185					return cli.TypeError
186				}
187				return strconv.FormatBool(check.Stdin)
188			},
189		},
190		{
191			Title: "Metric Format",
192			CellTransformer: func(data interface{}) string {
193				check, ok := data.(types.CheckConfig)
194				if !ok {
195					return cli.TypeError
196				}
197				return check.OutputMetricFormat
198			},
199		},
200		{
201			Title: "Metric Handlers",
202			CellTransformer: func(data interface{}) string {
203				check, ok := data.(types.CheckConfig)
204				if !ok {
205					return cli.TypeError
206				}
207				return strings.Join(check.OutputMetricHandlers, ",")
208			},
209		},
210	})
211
212	table.Render(writer, results)
213}
214