Repository navigation
Expand file tree
/
Copy pathshow.go
More file actions
318 lines (288 loc) · 10.9 KB
/
Copy pathshow.go
File metadata and controls
318 lines (288 loc) · 10.9 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package cli
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
"github.com/umatare5/cisco-wnc-cli/internal/config"
"github.com/umatare5/cisco-wnc-cli/internal/render"
"github.com/umatare5/cisco-wnc-cli/internal/show"
)
// showCommand groups the read-only views.
func showCommand() *cli.Command {
return &cli.Command{
Name: "show",
Aliases: []string{"s"},
Usage: "Display controller state",
Flags: showFlags(),
Action: parentAction,
Commands: []*cli.Command{
overviewCommand(),
apCommand(),
apJoinCommand(),
apTagCommand(),
clientCommand(),
wlanCommand(),
policyTagCommand(),
siteTagCommand(),
rfTagCommand(),
},
}
}
// absenceNote is the one sentence every show leaf's DESCRIPTION carries. A dash is not a
// zero and not a No, and that is the single thing a reader most needs from these views.
const absenceNote = `A cell reading "-" is a value the controller did not send.`
func overviewCommand() *cli.Command {
return &cli.Command{
Name: "overview",
Aliases: []string{"o"},
Usage: "Per-radio RF summary across 2.4, 5 and 6 GHz",
Description: "One row per access point radio, sorted by ap_name.\n" +
absenceNote + "\n" +
"Admin State is the radio's own state: an access-point-level disable leaves\n" +
"it Enabled with Oper State reading Down, so read wnc show ap for that state.",
Flags: append(sortFlags(show.DefaultSortAPName),
radioFlag(),
),
Action: func(ctx context.Context, cmd *cli.Command) error {
band, err := config.ResolveRadio(cmd)
if err != nil {
return fmt.Errorf("%w: %w", ErrUsage, err)
}
return runShow(ctx, cmd, show.OverviewKeys(), show.DefaultSortAPName,
show.OverviewColumns(), show.FetchOverview(band))
},
}
}
func apCommand() *cli.Command {
return &cli.Command{
Name: leafAP,
Aliases: []string{"a"},
Usage: "Associated access points",
Description: "One row per access point in capwap-data, sorted by ap_name.\n" +
absenceNote + "\n" +
"Admin State is the access point's own state, which an access-point-level\n" +
"disable changes and the Admin State column of wnc show overview does not.",
Flags: sortFlags(show.DefaultSortAPName),
Action: func(ctx context.Context, cmd *cli.Command) error {
return runShow(ctx, cmd, show.APKeys(), show.DefaultSortAPName,
show.APColumns(), show.FetchAPs)
},
}
}
// apJoinCommand takes the "join" alias rather than a letter that would read as a filter.
func apJoinCommand() *cli.Command {
return &cli.Command{
Name: "ap-join",
Aliases: []string{"join"},
Usage: "Join, discovery and DTLS outcome per access point, joined or not",
Description: "One row per access point the controller remembers, joined or not, sorted\n" +
"by ap_name.\n" +
absenceNote + "\n" +
"It is the only view that reports an access point capwap-data has dropped.",
Flags: sortFlags(show.DefaultSortAPName),
Action: func(ctx context.Context, cmd *cli.Command) error {
return runShow(ctx, cmd, show.APJoinKeys(), show.DefaultSortAPName,
show.APJoinColumns(), show.FetchAPJoins)
},
}
}
// apTagCommand takes "tag" rather than a single letter: "wnc show t -t 30s" does not
// read as the command or as the timeout.
func apTagCommand() *cli.Command {
return &cli.Command{
Name: "ap-tag",
Aliases: []string{"tag"},
Usage: "Tag assignment and its resolved values, per access point",
Description: "One row per access point, sorted by ap_name.\n" +
absenceNote + "\n" +
"The tag columns are the tags resolved onto the access point. The profile\n" +
"columns come from the site tag and agree only while Tag Source is Static.",
Flags: sortFlags(show.DefaultSortAPName),
Action: func(ctx context.Context, cmd *cli.Command) error {
return runShow(ctx, cmd, show.APTagKeys(), show.DefaultSortAPName,
show.APTagColumns(), show.FetchAPTags)
},
}
}
func clientCommand() *cli.Command {
return &cli.Command{
Name: "client",
Aliases: []string{"c"},
Usage: "Associated wireless clients",
Description: "One row per associated client, sorted by ap_name.\n" +
absenceNote + "\n" +
"--radio, --ssid and --ap-name narrow the list. A client whose band the\n" +
"controller did not report is excluded by --radio, and the count is logged.",
Flags: append(sortFlags(show.DefaultSortAPName),
radioFlag(),
&cli.StringFlag{
Name: config.FlagSSID,
Aliases: []string{"s"},
Usage: "keep only clients on this SSID",
},
&cli.StringFlag{
Name: config.FlagAPName,
Usage: "keep only clients on this access point",
},
),
Action: func(ctx context.Context, cmd *cli.Command) error {
band, err := config.ResolveRadio(cmd)
if err != nil {
return fmt.Errorf("%w: %w", ErrUsage, err)
}
filter := show.ClientFilter{
Band: band,
SSID: cmd.String(config.FlagSSID),
APName: cmd.String(config.FlagAPName),
}
return runShow(ctx, cmd, show.ClientKeys(), show.DefaultSortAPName,
show.ClientColumns(), show.FetchClients(filter))
},
}
}
func wlanCommand() *cli.Command {
return &cli.Command{
Name: "wlan",
Aliases: []string{"w"},
Usage: "Configured WLANs and their bound policy profiles",
Description: "One row per WLAN and each policy profile bound to it, sorted by wlan_id,\n" +
"so a WLAN bound under two tags appears twice and an unbound one appears\n" +
"once with its policy columns empty.\n" +
absenceNote,
Flags: sortFlags(show.DefaultSortWLANID),
Action: func(ctx context.Context, cmd *cli.Command) error {
return runShow(ctx, cmd, show.WLANKeys(), show.DefaultSortWLANID,
show.WLANColumns(), show.FetchWLANs)
},
}
}
// The policy-tag, site-tag and rf-tag views take no alias: "s" is the show group's own and "-r"
// is --radio. "tag" stays on ap-tag, the one tag view an operator reaches during an incident.
func policyTagCommand() *cli.Command {
return &cli.Command{
Name: "policy-tag",
Usage: "Configured policy tags and the WLANs they bind",
Description: "One row per WLAN binding the tag carries, sorted by policy_tag, so a tag\n" +
"binding three WLANs appears three times and one binding none appears once.\n" +
absenceNote + "\n" +
"WLAN Profile is the profile name the binding keys on, not always the SSID.",
Flags: sortFlags(show.DefaultSortPolicyTag),
Action: func(ctx context.Context, cmd *cli.Command) error {
return runShow(ctx, cmd, show.PolicyTagKeys(), show.DefaultSortPolicyTag,
show.PolicyTagColumns(), show.FetchPolicyTags)
},
}
}
func siteTagCommand() *cli.Command {
return &cli.Command{
Name: "site-tag",
Usage: "Configured site tags and the profiles they name",
Description: "One row per site tag, sorted by site_tag.\n" +
absenceNote + "\n" +
"The read asks for the values in effect, so a leaf a tag left at its default\n" +
"is reported rather than arriving as an absence.",
Flags: sortFlags(show.DefaultSortSiteTag),
Action: func(ctx context.Context, cmd *cli.Command) error {
return runShow(ctx, cmd, show.SiteTagKeys(), show.DefaultSortSiteTag,
show.SiteTagColumns(), show.FetchSiteTags)
},
}
}
func rfTagCommand() *cli.Command {
return &cli.Command{
Name: "rf-tag",
Usage: "Configured RF tags and their per-band RF profiles",
Description: "One row per RF tag, sorted by rf_tag.\n" +
absenceNote + "\n" +
"The read asks for the values in effect, because a plain read omits the\n" +
"built-in tag's per-band profile names.",
Flags: sortFlags(show.DefaultSortRFTag),
Action: func(ctx context.Context, cmd *cli.Command) error {
return runShow(ctx, cmd, show.RFTagKeys(), show.DefaultSortRFTag,
show.RFTagColumns(), show.FetchRFTags)
},
}
}
// runShow is the body every show subcommand shares: resolve the settings against the
// configuration file, then hand the fan-out its columns and its read. Resolution
// happens here rather than in the root's Before because a subcommand's own flags are
// not visible from the root, and IsSet gives a flag precedence over the file.
func runShow[R any](
ctx context.Context,
cmd *cli.Command,
sortKeys []string,
defaultSortBy string,
cols []render.Column[R],
fetch show.Fetcher[R],
) error {
st := runtimeFrom(ctx)
// Answered before anything is resolved: listing a command's own vocabulary needs
// no controller and no token, so it must not fail on their absence.
if cmd.Bool(config.FlagListKeys) {
return printListKeys(cmd, st.Logger, sortKeys)
}
settings, err := config.Resolve(cmd, st.File, sortKeys, defaultSortBy, render.DefaultKeys(cols))
if err != nil {
return fmt.Errorf("%w: %w", ErrUsage, err)
}
if settings.Insecure {
st.Logger.Warn("TLS certificate verification is disabled")
}
// IsSet and not settings.Pretty: a configuration file carrying pretty next to
// format json would otherwise warn on every run, where the invocation asked for
// only one of them.
if cmd.IsSet(config.FlagPretty) && settings.Format == config.FormatJSON {
st.Logger.Warn("--" + config.FlagPretty + " styles the table only; --" +
config.FlagFormat + " " + config.FormatJSON + " output is unchanged")
}
// A dry run passes the same settings checks and warnings as a real run, and returns before
// show.Run builds a client.
if cmd.Bool(config.FlagDryRun) {
return printWouldRead(cmd, settings.Controllers)
}
return show.Run(ctx, show.Env{
Settings: settings,
Logger: st.Logger,
Out: st.Streams.Out,
UserAgent: UserAgent(),
}, cols, fetch)
}
// printWouldRead answers --dry-run with the controllers the read would reach, which is all a
// read can report before it is made.
func printWouldRead(cmd *cli.Command, targets []config.Target) error {
for _, t := range targets {
if _, err := fmt.Fprintf(cmd.Root().Writer, "%s: would read %s\n", t.Name, cmd.Name); err != nil {
return err
}
}
return nil
}
// ignoredByListKeys are the flags a listing parses and does not act on. Naming them
// is cheaper than the alternative: --list-keys short-circuits before the settings are
// resolved, so a value that would have been rejected there passes unremarked.
//
// --access-token is deliberately absent: it is the one flag here with an environment
// source, and IsSet is true for an exported variable. Listing it would warn on
// every invocation of anyone who exports the token.
var ignoredByListKeys = []string{
config.FlagController, config.FlagInsecure,
config.FlagFormat, config.FlagTimeout, config.FlagPretty, config.FlagColumns,
config.FlagSortBy, config.FlagSortOrder,
config.FlagRadio, config.FlagSSID, config.FlagAPName,
}
// printListKeys answers --list-keys with one key per line. The slice is the same one --sort-by is
// validated against, so the listing and the accepted set are one source.
func printListKeys(cmd *cli.Command, logger *logrus.Logger, keys []string) error {
for _, name := range ignoredByListKeys {
if cmd.IsSet(name) {
logger.Warnf("--%s does not affect --%s", name, config.FlagListKeys)
}
}
for _, k := range keys {
if _, err := fmt.Fprintln(cmd.Root().Writer, k); err != nil {
return err
}
}
return nil
}