-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
89 lines (77 loc) · 1.8 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
package main
import (
"fmt"
"os"
"time"
"github.com/ccremer/plogr"
"github.com/urfave/cli/v2"
)
var (
// These will be populated by Goreleaser
version = "unknown"
commit = "-dirty-"
date = time.Now().Format("2006-01-02")
appName = "paperless-cli"
appLongName = "CLI tool to interact with paperless-ngx remote API "
// envPrefix is the global prefix to use for the keys in environment variables
envPrefix = "PAPERLESS_"
)
func main() {
app := NewApp()
err := app.Run(os.Args)
if err != nil {
plogr.DefaultErrorPrinter.Println(err.Error())
os.Exit(1)
}
}
func NewApp() *cli.App {
app := &cli.App{
Name: appName,
Usage: appLongName,
Version: fmt.Sprintf("%s, revision=%s, date=%s", version, commit, date),
Before: before(loadConfigFileFn, setupLogging),
Flags: []cli.Flag{
newLogLevelFlag(),
newConfigFileFlag(),
},
Commands: []*cli.Command{
&newUploadCommand().Command,
&newBulkDownloadCommand().Command,
&newConsumeCommand().Command,
&newInitCommand().Command,
},
}
return app
}
// env combines envPrefix with given suffix delimited by underscore.
func env(suffix string) string {
return envPrefix + suffix
}
// envVars combines envPrefix with each given suffix delimited by underscore.
func envVars(suffixes ...string) []string {
arr := make([]string, len(suffixes))
for i := range suffixes {
arr[i] = env(suffixes[i])
}
return arr
}
func before(actions ...cli.BeforeFunc) cli.BeforeFunc {
return func(ctx *cli.Context) error {
for _, fn := range actions {
if err := fn(ctx); err != nil {
return err
}
}
return nil
}
}
func actions(actions ...cli.ActionFunc) cli.ActionFunc {
return func(ctx *cli.Context) error {
for _, action := range actions {
if err := action(ctx); err != nil {
return err
}
}
return nil
}
}