forked from snapcore/snapd
/
cmd_help.go
364 lines (334 loc) · 11 KB
/
cmd_help.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"bytes"
"fmt"
"io"
"regexp"
"strings"
"unicode/utf8"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/i18n"
)
var shortHelpHelp = i18n.G("Show help about a command")
var longHelpHelp = i18n.G(`
The help command displays information about snap commands.
`)
// addHelp adds --help like what go-flags would do for us, but hidden
func addHelp(parser *flags.Parser) error {
var help struct {
ShowHelp func() error `short:"h" long:"help"`
}
help.ShowHelp = func() error {
// this function is called via --help (or -h). In that
// case, parser.Command.Active should be the command
// on which help is being requested (like "snap foo
// --help", active is foo), or nil in the toplevel.
if parser.Command.Active == nil {
// this means *either* a bare 'snap --help',
// *or* 'snap --help command'
//
// If we return nil in the first case go-flags
// will throw up an ErrCommandRequired on its
// own, but in the second case it'll go on to
// run the command, which is very unexpected.
//
// So we force the ErrCommandRequired here.
// toplevel --help gets handled via ErrCommandRequired
return &flags.Error{Type: flags.ErrCommandRequired}
}
// not toplevel, so ask for regular help
return &flags.Error{Type: flags.ErrHelp}
}
hlpgrp, err := parser.AddGroup("Help Options", "", &help)
if err != nil {
return err
}
hlpgrp.Hidden = true
hlp := parser.FindOptionByLongName("help")
hlp.Description = i18n.G("Show this help message")
hlp.Hidden = true
return nil
}
type cmdHelp struct {
All bool `long:"all"`
Manpage bool `long:"man" hidden:"true"`
Positional struct {
// TODO: find a way to make Command tab-complete
Subs []string `positional-arg-name:"<command>"`
} `positional-args:"yes"`
parser *flags.Parser
}
func init() {
addCommand("help", shortHelpHelp, longHelpHelp, func() flags.Commander { return &cmdHelp{} },
map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"all": i18n.G("Show a short summary of all commands"),
// TRANSLATORS: This should not start with a lowercase letter.
"man": i18n.G("Generate the manpage"),
}, nil)
}
func (cmd *cmdHelp) setParser(parser *flags.Parser) {
cmd.parser = parser
}
// manfixer is a hackish way to fix drawbacks in the generated manpage:
// - no way to get it into section 8
// - duplicated TP lines that break older groff (e.g. 14.04), lp:1814767
type manfixer struct {
bytes.Buffer
done bool
}
func (w *manfixer) Write(buf []byte) (int, error) {
if !w.done {
w.done = true
if bytes.HasPrefix(buf, []byte(".TH snap 1 ")) {
// io.Writer.Write must not modify the buffer, even temporarily
n, _ := w.Buffer.Write(buf[:9])
w.Buffer.Write([]byte{'8'})
m, err := w.Buffer.Write(buf[10:])
return n + m + 1, err
}
}
return w.Buffer.Write(buf)
}
var tpRegexp = regexp.MustCompile(`(?m)(?:^\.TP\n)+`)
func (w *manfixer) flush() {
str := tpRegexp.ReplaceAllLiteralString(w.Buffer.String(), ".TP\n")
io.Copy(Stdout, strings.NewReader(str))
}
func (cmd cmdHelp) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
if cmd.Manpage {
// you shouldn't try to to combine --man with --all nor a
// subcommand, but --man is hidden so no real need to check.
out := &manfixer{}
cmd.parser.WriteManPage(out)
out.flush()
return nil
}
if cmd.All {
if len(cmd.Positional.Subs) > 0 {
return fmt.Errorf(i18n.G("help accepts a command, or '--all', but not both."))
}
printLongHelp(cmd.parser)
return nil
}
var subcmd = cmd.parser.Command
for _, subname := range cmd.Positional.Subs {
subcmd = subcmd.Find(subname)
if subcmd == nil {
sug := "snap help"
if x := cmd.parser.Command.Active; x != nil && x.Name != "help" {
sug = "snap help " + x.Name
}
// TRANSLATORS: %q is the command the user entered; %s is 'snap help' or 'snap help <cmd>'
return fmt.Errorf(i18n.G("unknown command %q, see '%s'."), subname, sug)
}
// this makes "snap help foo" work the same as "snap foo --help"
cmd.parser.Command.Active = subcmd
}
if subcmd != cmd.parser.Command {
return &flags.Error{Type: flags.ErrHelp}
}
return &flags.Error{Type: flags.ErrCommandRequired}
}
type helpCategory struct {
Label string
// Other is set if the category Commands should be listed
// together under "... Other" in the `snap help` list.
Other bool
Description string
// Commands list commands belonging to the category that should
// be listed under both `snap help` and "snap help --all`.
Commands []string
// AllOnlyCommands list commands belonging to the category that should
// be listed only under "snap help --all`.
AllOnlyCommands []string
}
// helpCategories helps us by grouping commands
var helpCategories = []helpCategory{
{
Label: i18n.G("Basics"),
Description: i18n.G("basic snap management"),
Commands: []string{"find", "info", "install", "remove", "list"},
}, {
Label: i18n.G("...more"),
Description: i18n.G("slightly more advanced snap management"),
Commands: []string{"refresh", "revert", "switch", "disable", "enable", "create-cohort"},
}, {
Label: i18n.G("History"),
Description: i18n.G("manage system change transactions"),
Commands: []string{"changes", "tasks", "abort", "watch"},
}, {
Label: i18n.G("Daemons"),
Description: i18n.G("manage services"),
Commands: []string{"services", "start", "stop", "restart", "logs"},
}, {
Label: i18n.G("Permissions"),
Description: i18n.G("manage permissions"),
Commands: []string{"connections", "interface", "connect", "disconnect"},
}, {
Label: i18n.G("Configuration"),
Description: i18n.G("system administration and configuration"),
Commands: []string{"get", "set", "unset", "wait"},
}, {
Label: i18n.G("App Aliases"),
Description: i18n.G("manage aliases"),
Commands: []string{"alias", "aliases", "unalias", "prefer"},
}, {
Label: i18n.G("Account"),
Description: i18n.G("authentication to snapd and the snap store"),
Commands: []string{"login", "logout", "whoami"},
}, {
Label: i18n.G("Snapshots"),
Description: i18n.G("archives of snap data"),
Commands: []string{"saved", "save", "check-snapshot", "restore", "forget"},
}, {
Label: i18n.G("Device"),
Description: i18n.G("manage device"),
Commands: []string{"model", "reboot", "recovery"},
}, {
Label: i18n.G("Warnings"),
Other: true,
Description: i18n.G("manage warnings"),
Commands: []string{"warnings", "okay"},
}, {
Label: i18n.G("Assertions"),
Other: true,
Description: i18n.G("manage assertions"),
Commands: []string{"known", "ack"},
}, {
Label: i18n.G("Introspection"),
Other: true,
Description: i18n.G("introspection and debugging of snapd"),
Commands: []string{"version"},
AllOnlyCommands: []string{"debug"},
},
{
Label: i18n.G("Development"),
Description: i18n.G("developer-oriented features"),
Commands: []string{"download", "pack", "run", "try"},
AllOnlyCommands: []string{"prepare-image"},
},
}
var (
longSnapDescription = strings.TrimSpace(i18n.G(`
The snap command lets you install, configure, refresh and remove snaps.
Snaps are packages that work across many different Linux distributions,
enabling secure delivery and operation of the latest apps and utilities.
`))
snapUsage = i18n.G("Usage: snap <command> [<options>...]")
snapHelpCategoriesIntro = i18n.G("Commonly used commands can be classified as follows:")
snapHelpAllIntro = i18n.G("Commands can be classified as follows:")
snapHelpAllFooter = i18n.G("For more information about a command, run 'snap help <command>'.")
snapHelpFooter = i18n.G("For a short summary of all commands, run 'snap help --all'.")
)
func printHelpHeader(cmdsIntro string) {
fmt.Fprintln(Stdout, longSnapDescription)
fmt.Fprintln(Stdout)
fmt.Fprintln(Stdout, snapUsage)
fmt.Fprintln(Stdout)
fmt.Fprintln(Stdout, cmdsIntro)
}
func printHelpAllFooter() {
fmt.Fprintln(Stdout)
fmt.Fprintln(Stdout, snapHelpAllFooter)
}
func printHelpFooter() {
printHelpAllFooter()
fmt.Fprintln(Stdout, snapHelpFooter)
}
// this is called when the Execute returns a flags.Error with ErrCommandRequired
func printShortHelp() {
printHelpHeader(snapHelpCategoriesIntro)
maxLen := utf8.RuneCountInString("... Other")
var otherCommands []string
var develCateg *helpCategory
for _, categ := range helpCategories {
if categ.Other {
otherCommands = append(otherCommands, categ.Commands...)
continue
}
if categ.Label == "Development" {
develCateg = &categ
}
if l := utf8.RuneCountInString(categ.Label); l > maxLen {
maxLen = l
}
}
fmt.Fprintln(Stdout)
for _, categ := range helpCategories {
// Other and Development will come last
if categ.Other || categ.Label == "Development" || len(categ.Commands) == 0 {
continue
}
fmt.Fprintf(Stdout, "%*s: %s\n", maxLen+2, categ.Label, strings.Join(categ.Commands, ", "))
}
// ... Other
if len(otherCommands) > 0 {
fmt.Fprintf(Stdout, "%*s: %s\n", maxLen+2, "... Other", strings.Join(otherCommands, ", "))
}
// Development last
if develCateg != nil && len(develCateg.Commands) > 0 {
fmt.Fprintf(Stdout, "%*s: %s\n", maxLen+2, "Development", strings.Join(develCateg.Commands, ", "))
}
printHelpFooter()
}
// this is "snap help --all"
func printLongHelp(parser *flags.Parser) {
printHelpHeader(snapHelpAllIntro)
maxLen := 0
for _, categ := range helpCategories {
for _, command := range categ.Commands {
if l := len(command); l > maxLen {
maxLen = l
}
}
for _, command := range categ.AllOnlyCommands {
if l := len(command); l > maxLen {
maxLen = l
}
}
}
// flags doesn't have a LookupCommand?
commands := parser.Commands()
cmdLookup := make(map[string]*flags.Command, len(commands))
for _, cmd := range commands {
cmdLookup[cmd.Name] = cmd
}
listCmds := func(cmds []string) {
for _, name := range cmds {
cmd := cmdLookup[name]
if cmd == nil {
fmt.Fprintf(Stderr, "??? Cannot find command %q mentioned in help categories, please report!\n", name)
} else {
fmt.Fprintf(Stdout, " %*s %s\n", -maxLen, name, cmd.ShortDescription)
}
}
}
for _, categ := range helpCategories {
fmt.Fprintln(Stdout)
fmt.Fprintf(Stdout, " %s (%s):\n", categ.Label, categ.Description)
listCmds(categ.Commands)
listCmds(categ.AllOnlyCommands)
}
printHelpAllFooter()
}