forked from canonical/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_help.go
275 lines (249 loc) · 7.72 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
// Copyright (c) 2014-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"
)
var shortHelpHelp = "Show help about a command"
var longHelpHelp = `
The help command displays information about 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 "pebble foo
// --help", active is foo), or nil in the toplevel.
if parser.Command.Active == nil {
// this means *either* a bare 'pebble --help',
// *or* 'pebble --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 = "Show this help message"
hlp.Hidden = true
return nil
}
type cmdHelp struct {
All bool `long:"all"`
Manpage bool `long:"man" hidden:"true"`
Positional struct {
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{
"all": "Show a short summary of all commands",
"man": "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 pebble 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("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 := "pebble help"
if x := cmd.parser.Command.Active; x != nil && x.Name != "help" {
sug = "pebble help " + x.Name
}
return fmt.Errorf("unknown command %q, see '%s'.", subname, sug)
}
// this makes "pebble help foo" work the same as "pebble 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
Description string
Commands []string
}
// helpCategories helps us by grouping commands
var helpCategories = []helpCategory{{
Label: "Run",
Description: "run pebble",
Commands: []string{"run", "help", "version"},
}, {
Label: "Plan",
Description: "view and change configuration",
Commands: []string{"add", "plan"},
}, {
Label: "Services",
Description: "manage services",
Commands: []string{"services", "logs", "checks", "start", "restart", "signal", "stop", "replan"},
}, {
Label: "Files",
Description: "work with files and execute commands",
Commands: []string{"rm", "exec"},
}, {
Label: "Changes",
Description: "manage changes and their tasks",
Commands: []string{"changes", "tasks"},
}, {
Label: "Warnings",
Description: "manage warnings",
Commands: []string{"warnings", "okay"},
}}
var (
longPebbleDescription = strings.TrimSpace(`
Pebble lets you control services and perform management actions on
the system that is running them.
`)
pebbleUsage = "Usage: pebble <command> [<options>...]"
pebbleHelpCategoriesIntro = "Commands can be classified as follows:"
pebbleHelpAllFooter = "Set the PEBBLE environment variable to override the configuration directory \n" +
"(which defaults to " + defaultPebbleDir + "). Set PEBBLE_SOCKET to override \n" +
"the Unix socket used for the API (defaults to $PEBBLE/.pebble.socket).\n" +
"\n" +
"For more information about a command, run 'pebble help <command>'."
pebbleHelpFooter = "For a short summary of all commands, run 'pebble help --all'."
)
func printHelpHeader() {
fmt.Fprintln(Stdout, longPebbleDescription)
fmt.Fprintln(Stdout)
fmt.Fprintln(Stdout, pebbleUsage)
fmt.Fprintln(Stdout)
fmt.Fprintln(Stdout, pebbleHelpCategoriesIntro)
}
func printHelpAllFooter() {
fmt.Fprintln(Stdout)
fmt.Fprintln(Stdout, pebbleHelpAllFooter)
}
func printHelpFooter() {
printHelpAllFooter()
fmt.Fprintln(Stdout, pebbleHelpFooter)
}
// this is called when the Execute returns a flags.Error with ErrCommandRequired
func printShortHelp() {
printHelpHeader()
fmt.Fprintln(Stdout)
maxLen := 0
for _, categ := range helpCategories {
if l := utf8.RuneCountInString(categ.Label); l > maxLen {
maxLen = l
}
}
for _, categ := range helpCategories {
fmt.Fprintf(Stdout, "%*s: %s\n", maxLen+2, categ.Label, strings.Join(categ.Commands, ", "))
}
printHelpFooter()
}
// this is "pebble help --all"
func printLongHelp(parser *flags.Parser) {
printHelpHeader()
maxLen := 0
for _, categ := range helpCategories {
for _, command := range categ.Commands {
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
}
for _, categ := range helpCategories {
fmt.Fprintln(Stdout)
fmt.Fprintf(Stdout, " %s (%s):\n", categ.Label, categ.Description)
for _, name := range categ.Commands {
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)
}
}
}
printHelpAllFooter()
}