-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (60 loc) · 1.28 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
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"github.com/alexflint/go-arg"
)
type mainCmd struct {
Ask *askCmd `arg:"subcommand"`
Config *configCmd `arg:"subcommand"`
Chat *chatCmd `arg:"subcommand"`
ConfigName string `arg:"-c,--config,env:HLP_CONFIG" help:"name of the configuration set"`
}
func (args *mainCmd) SetupConfig() (config, error) {
var err error
cfg, err := ReadConfig(args.ConfigName)
if err != nil {
return config{}, fmt.Errorf("failed fetching configs: %w", err)
}
return cfg, nil
}
func (args *mainCmd) Execute(ctx context.Context) error {
config, err := args.SetupConfig()
if err != nil {
return err
}
switch {
case args.Ask != nil:
err = args.Ask.Execute(ctx, &config)
case args.Config != nil:
err = args.Config.Execute(ctx, &config)
case args.Chat != nil:
err = args.Chat.Execute(ctx, &config)
default:
err = writeHelp(args, os.Stderr)
}
return err
}
func run() error {
var args mainCmd
ctx := context.Background()
arg.MustParse(&args)
if err := args.Execute(ctx); err != nil {
return err
}
return nil
}
func main() {
if err := run(); err != nil {
var errtype *terminateSilentlyError
if errors.As(err, &errtype) {
os.Exit(0)
}
log.Printf("error: %v", err)
os.Exit(1)
}
os.Exit(0)
}