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
|
package runner
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"go.sudomsg.com/kit/logging"
)
const (
ExitSuccess = 0
ExitFailure = 1
ExitPanic = 2
ExitUsage = 3
)
type UsageError struct {
Err error
}
func (e UsageError) Error() string {
return e.Err.Error()
}
func (e UsageError) Unwrap() error {
return e.Err
}
func RunWithArgs(args []string, cmd Command) {
ctx := context.Background()
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
defer func() {
if err := recover(); err != nil {
logging.RecoverAndLog(ctx, "Panicked", err)
os.Exit(ExitPanic)
}
}()
if err := runCmd(ctx, cmd, args); err != nil {
var ue UsageError
if errors.As(err, &ue) {
os.Exit(ExitUsage)
}
slog.Log(ctx, slog.LevelError, "Program Terminated", "error", err)
os.Exit(ExitFailure)
}
}
func Run(cmd Command) {
RunWithArgs(os.Args, cmd)
}
type Command struct {
Name string
Description string
Run func(ctx context.Context, cs *CommandSet, args []string) error
}
func runCmd(ctx context.Context, cmd Command, args []string) error {
name := "command"
if len(args) > 0 && args[0] != "" {
name = args[0]
args = args[1:]
}
if cmd.Name != "" {
name = cmd.Name
}
fs := flag.NewFlagSet(name, flag.ContinueOnError)
c := &CommandSet{
FlagSet: fs,
description: cmd.Description,
}
c.Usage = func() {
fmt.Fprintf(fs.Output(), "Usage of %s:\n", fs.Name())
c.PrintDefaults()
}
c.AddSubcommand("help", "Print out the help", func(ctx context.Context, cs *CommandSet, args []string) error {
c.Usage()
return nil
})
return cmd.Run(ctx, c, args)
}
type CommandSet struct {
*flag.FlagSet
cmds []Command
description string
}
var ErrInvalidSubcommand = errors.New("invalid subcommand")
func (c *CommandSet) Run(ctx context.Context, args []string) error {
if err := c.Parse(args); err != nil {
return err
}
args = c.Args()
if len(args) == 0 {
fmt.Fprintf(c.Output(), "Missing Subcommand")
c.Usage()
return UsageError{Err: ErrInvalidSubcommand}
}
for _, sc := range c.cmds {
if sc.Name == args[0] {
return runCmd(ctx, sc, args)
}
}
fmt.Fprintf(c.Output(), "Invalid Subcommand: %s", args[0])
c.Usage()
return UsageError{Err: fmt.Errorf("invalid subcommand: %s", args[0])}
}
func (c *CommandSet) AddSubcommand(name string, usage string, run func(ctx context.Context, cs *CommandSet, args []string) error) {
c.cmds = append(c.cmds, Command{Name: name, Description: usage, Run: run})
}
func (c *CommandSet) PrintDefaults() {
c.FlagSet.PrintDefaults()
if c.description != "" {
fmt.Fprintf(c.Output(), "\n%s\n", c.description)
}
if len(c.cmds) > 0 {
fmt.Fprintf(c.Output(), "\nSubcommands:\n")
for _, sc := range c.cmds {
fmt.Fprintf(c.Output(), " %s\t%s\n", sc.Name, sc.Description)
}
}
}
func (c *CommandSet) Parse(args []string) error {
if err := c.FlagSet.Parse(args); err != nil {
return UsageError{Err: err}
}
return nil
}
|