This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (99 loc) · 2.95 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
107
108
109
110
111
112
113
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 = "provider-postgresql"
appLongName = "a Crossplane provider for AppCat managed PostgreSQL instances"
// envPrefix is the global prefix to use for the keys in environment variables
envPrefix = "PROVIDER"
)
func init() {
// Remove `-v` short option from --version flag
cli.VersionFlag.(*cli.BoolFlag).Aliases = nil
}
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.IntFlag{
Name: "log-level", Aliases: []string{"v"}, EnvVars: envVars("LOG_LEVEL"),
Usage: "number of the log level verbosity",
Value: 0,
},
&cli.StringFlag{
Name: "log-format", EnvVars: envVars("LOG_FORMAT"),
Usage: "sets the log format (values: [json, console])",
DefaultText: "console",
},
},
Commands: []*cli.Command{
newOperatorCommand(),
},
}
hasSubcommands := len(app.Commands) > 0
app.Action = rootAction(hasSubcommands)
// 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
}
func rootAction(hasSubcommands bool) func(context *cli.Context) error {
return func(context *cli.Context) error {
if hasSubcommands {
return cli.ShowAppHelp(context)
}
return LogMetadata(context)
}
}
// 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
}