-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
283 lines (224 loc) · 7.1 KB
/
app.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
// SPDX-FileCopyrightText: 2025 The Cipher Host Team <[email protected]>
//
// SPDX-License-Identifier: MIT
package cmdkit
import (
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"runtime/debug"
"sort"
"go.cipher.host/cmdkit/internal/xflag"
)
// ErrUnknownCommand is returned when a command is not found.
const ErrUnknownCommand Error = "unknown command"
// App represents a command-line application. It serves as the top-level
// container for application state and coordinates the execution of subcommands
// while managing global flags and configuration.
type App struct {
// OutWriter receives normal output. When initialized from New, defaults to
// [os.Stdout].
//
// Configurable primarily for testing and special output handling.
//
// [os.Stdout]: https://pkg.go.dev/os#Stdout
OutWriter io.Writer
// ErrWriter receives error output and help text. When initialized from New,
// defaults to [os.Stderr].
//
// Separated from OutWriter to follow Unix output conventions.
//
// [os.Stderr]: https://pkg.go.dev/os#Stderr
ErrWriter io.Writer
// Flags holds the global flag set. These flags apply to all subcommands and
// are parsed before command-specific flags.
Flags *flag.FlagSet
// Before is called before any subcommand execution, allowing for global
// setup like configuration loading or resource initialization.
Before func() error
// After is called after subcommand execution, regardless of success or
// failure. Useful for cleanup and resource release.
After func() error
// Name is the application name. When initialized from New, if not provided,
// defaults to [os.Args[0]].
//
// [os.Args[0]]: https://pkg.go.dev/os#Args
Name string
// Description explains the application's purpose. Shown in help text to
// guide users.
Description string
// Version indicates the application version. When initialized from new, if
// not provided, defaults to [debug.ReadBuildInfo].
//
// [debug.ReadBuildInfo]: https://pkg.go.dev/runtime/debug#ReadBuildInfo
Version string
// Examples provides usage examples shown in help text. Each example should
// be a complete command line.
Examples []string
// Commands holds the registered subcommands.
Commands []*Command
// shorthands maps short flag names, e.g. "-h", to their long versions, e.g.
// "--help". This enables Unix-style short flags while maintaining clean
// flag names.
shorthands []xflag.Shorthand
// helpFlag indicates whether to show helpFlag text.
helpFlag bool
// versionFlag indicates whether to show version information.
versionFlag bool
}
// New creates a new App instance with sane defaults.
//
// Name resolution:
// - Uses provided name if non-empty.
// - Falls back to base name of [os.Args[0]] if name is empty.
//
// Version resolution:
// - Uses provided version if non-empty.
// - Attempts to read version from Go module information.
// - Falls back to "unknown" if no version information is available.
//
// [os.Args[0]]: https://pkg.go.dev/os#Args
func New(name, description, version string) *App {
if name == "" {
name = filepath.Base(os.Args[0])
}
if version == "" {
build, ok := debug.ReadBuildInfo()
if ok {
version = build.Main.Version
} else {
version = "unknown"
}
}
app := &App{
Name: filepath.Base(name),
Description: description,
Version: version,
OutWriter: os.Stdout,
ErrWriter: os.Stderr,
Flags: flag.NewFlagSet(name, flag.ContinueOnError),
helpFlag: false,
}
app.Flags.BoolVar(&app.helpFlag, "help", false, "show help")
app.Flags.BoolVar(&app.helpFlag, "h", false, "show help")
app.AddShorthand("help", "h")
app.Flags.BoolVar(&app.versionFlag, "version", false, "show version")
app.Flags.BoolVar(&app.versionFlag, "v", false, "show version")
app.AddShorthand("version", "v")
app.Flags.Usage = app.ShowHelp
return app
}
// AddCommand registers a new subcommand with the application. Commands are
// stored in registration order, which affects help text display.
func (a *App) AddCommand(cmd *Command) {
cmd.app = a
a.Commands = append(a.Commands, cmd)
}
// AddShorthand creates a Unix-style short form for a flag. This enables the
// common pattern where flags have both long and short forms (e.g., "--help" and
// "-h").
//
// You should register both the long and short forms with flag.TypeVar-kind of
// methods, e.g. [flag.StringVar] before using this method.
//
// [flag.StringVar]: https://pkg.go.dev/flag#FlagSet.StringVar
func (a *App) AddShorthand(name, shorthand string) {
if f := a.Flags.Lookup(name); f != nil {
a.shorthands = append(a.shorthands, xflag.Shorthand{
Name: name,
Shorthand: shorthand,
Value: f.Value,
})
}
}
// Run executes the application with the provided arguments, typically
// [os.Args[1:]].
//
// [os.Args[1:]]: https://pkg.go.dev/os#Args
func (a *App) Run(args []string) error {
a.Flags.SetOutput(a.ErrWriter)
if err := a.Flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return fmt.Errorf("%w", err)
}
if a.helpFlag {
a.ShowHelp()
return nil
}
if a.versionFlag {
a.ShowVersion()
return nil
}
if a.Before != nil {
if err := a.Before(); err != nil {
return fmt.Errorf("%w", err)
}
}
defer func() {
if a.After != nil {
_ = a.After() //nolint:errcheck // not much we can do here
}
}()
if a.Flags.NArg() == 0 {
a.ShowHelp()
return nil
}
cmdName := a.Flags.Arg(0)
for _, cmd := range a.Commands {
if cmd.Name == cmdName {
return cmd.Run(a.Flags.Args()[1:])
}
}
return fmt.Errorf("%w: %q", ErrUnknownCommand, cmdName)
}
// Errorf writes a formatted error message to the application's error output. It
// prefixes the message with the application name to provide context in error
// messages.
func (a *App) Errorf(format string, args ...any) {
fmt.Fprintf(a.ErrWriter, "%s: %s\n", a.Name, fmt.Sprintf(format, args...))
}
// ShowVersion displays the application's version.
func (a *App) ShowVersion() {
fmt.Fprintf(a.ErrWriter, "%s %s\n", a.Name, a.Version)
}
// ShowHelp displays the application's help text.
func (a *App) ShowHelp() {
fmt.Fprintf(a.ErrWriter, "Usage: %s [global flags] command [command flags] [arguments...]\n\n", a.Name)
if a.Description != "" {
fmt.Fprintf(a.ErrWriter, "%s\n\n", a.Description)
}
if len(a.Commands) > 0 {
fmt.Fprintf(a.ErrWriter, "Commands:\n")
var (
names = make([]string, 0, len(a.Commands))
cmdMap = make(map[string]*Command, len(a.Commands))
)
for _, cmd := range a.Commands {
names = append(names, cmd.Name)
cmdMap[cmd.Name] = cmd
}
sort.Strings(names)
for _, name := range names {
cmd := cmdMap[name]
fmt.Fprintf(a.ErrWriter, " %-12s %s\n", name, cmd.Description)
}
fmt.Fprintln(a.ErrWriter)
}
fmt.Fprintf(a.ErrWriter, "Global Flags:\n")
a.Flags.SetOutput(a.ErrWriter)
xflag.PrintFlags(a.ErrWriter, a.Flags, a.shorthands)
fmt.Fprintln(a.ErrWriter)
if len(a.Examples) > 0 {
fmt.Fprintf(a.ErrWriter, "Examples:\n")
for _, example := range a.Examples {
fmt.Fprintf(a.ErrWriter, " $ %s\n", example)
}
fmt.Fprintln(a.ErrWriter)
}
fmt.Fprintf(a.ErrWriter, "Use \"%s [command] --help\" for more information about a command.\n", a.Name)
}