generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (94 loc) · 2.74 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"fmt"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
"github.com/go-logr/logr"
"github.com/urfave/cli/v2"
)
var (
// these variables are populated by Goreleaser when releasing
version = "unknown"
commit = "-dirty-"
date = time.Now().Format("2006-01-02")
appName = "appuio-odoo-adapter"
appLongName = "Adapter for APPUiO Cloud with Odoo as implementation"
// envPrefix is the global prefix to use for the keys in environment variables
envPrefix = "OA"
)
func main() {
ctx, stop, app := newApp()
defer stop()
err := app.RunContext(ctx, os.Args)
// If required flags aren't set, it will return with error before we could set up logging
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func newApp() (context.Context, context.CancelFunc, *cli.App) {
logInstance := &atomic.Value{}
logInstance.Store(logr.Discard())
app := &cli.App{
Name: appName,
Usage: appLongName,
Version: fmt.Sprintf("%s, revision=%s, date=%s", version, commit, date),
Compiled: compilationDate(),
EnableBashCompletion: true,
Before: setupLogging,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"verbose", "d"},
Usage: "sets the log level to debug",
EnvVars: envVars("DEBUG"),
},
&cli.StringFlag{
Name: "log-format",
Usage: "sets the log format (values: [json, console])",
EnvVars: envVars("LOG_FORMAT"),
DefaultText: "console",
},
},
Commands: []*cli.Command{
newSyncCommand(),
newinvoiceCommand(),
},
ExitErrHandler: func(context *cli.Context, err error) {
if err != nil {
AppLogger(context).Error(err, "fatal error")
cli.HandleExitCoder(cli.Exit("", 1))
}
},
}
// There is logr.NewContext(...) which returns a context that carries the logger instance.
// However, since we are configuring and replacing this logger after starting up and parsing the flags,
// we'll store a thread-safe atomic reference.
parentCtx := context.WithValue(context.Background(), loggerContextKey{}, logInstance)
ctx, stop := signal.NotifyContext(parentCtx, syscall.SIGINT, syscall.SIGTERM)
return ctx, stop, 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 compilationDate() time.Time {
compiled, err := time.Parse(time.RFC3339, date)
if err != nil {
// an empty Time{} causes cli.App to guess it from binary's file timestamp.
return time.Time{}
}
return compiled
}