-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
65 lines (55 loc) · 1.81 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
package main
import (
"os"
"github.com/alecthomas/kong"
kitLog "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/posener/complete"
"github.com/usdigitalresponse/grants-ingest/cli/grants-ingest/ffisImport"
"github.com/usdigitalresponse/grants-ingest/cli/grants-ingest/purgeData"
"github.com/usdigitalresponse/grants-ingest/internal/log"
"github.com/willabides/kongplete"
)
type Globals struct {
Log struct {
Level string `enum:"debug,info,warn,error" help:"Log level (debug|info|warn|error)" default:"info"`
JSON bool `help:"Outputs JSON-formatted logs"`
} `embed:"" prefix:"log-"`
}
func (g Globals) AfterApply(app *kong.Kong, logger *log.Logger) error {
var newLogger log.Logger
if g.Log.JSON {
newLogger = kitLog.NewJSONLogger(app.Stderr)
} else {
newLogger = kitLog.NewLogfmtLogger(app.Stderr)
}
logLevel := level.ParseDefault(g.Log.Level, level.InfoValue())
newLogger = level.NewFilter(newLogger, level.Allow(logLevel))
*logger = newLogger
return nil
}
type CLI struct {
Globals
FFISImport ffisImport.Cmd `cmd:"ffis-import" help:"Import FFIS spreadsheets to S3."`
Purge purgeData.Cmd `cmd:"purge" help:"Purge data from various locations."`
Completion kongplete.InstallCompletions `cmd:"" help:"Install shell completions"`
}
func main() {
var logger log.Logger
parser := kong.Must(&CLI{Globals: Globals{}},
kong.Name("grants-ingest"),
kong.Description("CLI utility for the grants-ingest service."),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{Compact: true}),
kong.Bind(&logger),
)
kongplete.Complete(parser,
kongplete.WithPredictor("file", complete.PredictFiles("*")),
kongplete.WithPredictor("dir", complete.PredictDirs("*")),
)
cli, err := parser.Parse(os.Args[1:])
parser.FatalIfErrorf(err)
if err := cli.Run(); err != nil {
cli.Exit(1)
}
}