-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
90 lines (78 loc) · 1.85 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"errors"
"flag"
"fmt"
"math/rand"
"os"
ff "github.com/peterbourgon/ff/v3"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"moul.io/climan"
"moul.io/motd"
"moul.io/srand"
"moul.io/u"
"moul.io/zapconfig"
)
func main() {
if err := run(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
}
os.Exit(1)
}
}
var opts struct {
Debug bool
rootLogger *zap.Logger
}
func run(args []string) error {
// parse CLI
root := &climan.Command{
Name: "golang-repo-template",
ShortUsage: "golang-repo-template [global flags] <subcommand> [flags] [args]",
ShortHelp: "More info on https://moul.io/golang-repo-template.",
FlagSetBuilder: func(fs *flag.FlagSet) { fs.BoolVar(&opts.Debug, "debug", opts.Debug, "debug mode") },
Exec: doRoot,
FFOptions: []ff.Option{ff.WithEnvVarPrefix("golang-repo-template")},
// Subcommands: []*climan.Command{},
// LongHelp: "",
}
if err := root.Parse(args); err != nil {
return fmt.Errorf("parse error: %w", err)
}
// init runtime
{
// prng
rand.Seed(srand.Fast())
// concurrency
// runtime.GOMAXPROCS(1)
// logger
config := zapconfig.New().SetPreset("light-console")
if opts.Debug {
config = config.SetLevel(zapcore.DebugLevel)
} else {
config = config.SetLevel(zapcore.InfoLevel)
}
var err error
opts.rootLogger, err = config.Build()
if err != nil {
return fmt.Errorf("logger init: %w", err)
}
}
// run
if err := root.Run(context.Background()); err != nil {
return fmt.Errorf("%w", err)
}
return nil
}
func doRoot(_ context.Context, args []string) error {
if len(args) > 0 {
return flag.ErrHelp
}
opts.rootLogger.Debug("init", zap.Strings("args", args), zap.Any("opts", opts))
fmt.Print(motd.Default())
fmt.Println(u.PrettyJSON(args))
return nil
}