-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
76 lines (69 loc) · 1.57 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"os"
"github.com/baalimago/go_away_boilerplate/pkg/ancli"
"github.com/baalimago/go_away_boilerplate/pkg/shutdown"
"github.com/baalimago/wd-41/cmd"
)
func printHelp(command cmd.Command, err error, printUsage cmd.UsagePrinter) int {
var notValidArg cmd.ArgNotFoundError
if errors.As(err, ¬ValidArg) {
ancli.PrintErr(err.Error())
printUsage()
} else if errors.Is(err, cmd.ErrNoArgs) {
printUsage()
} else if errors.Is(err, cmd.ErrHelpful) {
if command != nil {
fmt.Println(command.Help())
} else {
printUsage()
}
return 0
} else {
ancli.PrintfErr("unknown error: %v", err.Error())
}
return 1
}
func run(ctx context.Context, args []string, parseArgs cmd.ArgParser) int {
command, err := parseArgs(args)
if err != nil {
return printHelp(command, err, cmd.PrintUsage)
}
fs := command.Flagset()
var cmdArgs []string
if len(args) > 2 {
cmdArgs = args[2:]
}
err = fs.Parse(cmdArgs)
if err != nil {
ancli.PrintfErr("failed to parse flagset: %v", err.Error())
return 1
}
err = command.Setup()
if err != nil {
ancli.PrintfErr("failed to setup command: %v", err.Error())
return 1
}
err = command.Run(ctx)
if err != nil {
ancli.PrintfErr("failed to run %v", err.Error())
return 1
}
return 0
}
func main() {
ancli.Newline = true
ancli.SlogIt = true
ancli.SetupSlog()
ctx, cancel := context.WithCancel(context.Background())
exitCodeChan := make(chan int, 1)
go func() {
exitCodeChan <- run(ctx, os.Args, cmd.Parse)
cancel()
}()
shutdown.MonitorV2(ctx, cancel)
os.Exit(<-exitCodeChan)
}