-
Notifications
You must be signed in to change notification settings - Fork 158
/
main.go
100 lines (82 loc) 路 2.46 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
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/slok/sloth/cmd/sloth/commands"
"github.com/slok/sloth/internal/info"
"github.com/slok/sloth/internal/log"
loglogrus "github.com/slok/sloth/internal/log/logrus"
)
// Run runs the main application.
func Run(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error {
app := kingpin.New("sloth", "Easy SLO generator.")
app.DefaultEnvars()
config := commands.NewRootConfig(app)
// Setup commands (registers flags).
generateCmd := commands.NewGenerateCommand(app)
kubeCtrlCmd := commands.NewKubeControllerCommand(app)
validateCmd := commands.NewValidateCommand(app)
versionCmd := commands.NewVersionCommand(app)
cmds := map[string]commands.Command{
generateCmd.Name(): generateCmd,
kubeCtrlCmd.Name(): kubeCtrlCmd,
validateCmd.Name(): validateCmd,
versionCmd.Name(): versionCmd,
}
// Parse commandline.
cmdName, err := app.Parse(args[1:])
if err != nil {
return fmt.Errorf("invalid command configuration: %w", err)
}
// Set up global dependencies.
config.Stdin = stdin
config.Stdout = stdout
config.Stderr = stderr
config.Logger = getLogger(*config)
// Execute command.
err = cmds[cmdName].Run(ctx, *config)
if err != nil {
return fmt.Errorf("%q command failed: %w", cmdName, err)
}
return nil
}
// getLogger returns the application logger.
func getLogger(config commands.RootConfig) log.Logger {
if config.NoLog {
return log.Noop
}
// If not logger disabled use logrus logger.
logrusLog := logrus.New()
logrusLog.Out = config.Stderr // By default logger goes to stderr (so it can split stdout prints).
logrusLogEntry := logrus.NewEntry(logrusLog)
if config.Debug {
logrusLogEntry.Logger.SetLevel(logrus.DebugLevel)
}
// Log format.
switch config.LoggerType {
case commands.LoggerTypeDefault:
logrusLogEntry.Logger.SetFormatter(&logrus.TextFormatter{
ForceColors: !config.NoColor,
DisableColors: config.NoColor,
})
case commands.LoggerTypeJSON:
logrusLogEntry.Logger.SetFormatter(&logrus.JSONFormatter{})
}
logger := loglogrus.NewLogrus(logrusLogEntry).WithValues(log.Kv{
"version": info.Version,
})
logger.Debugf("Debug level is enabled") // Will log only when debug enabled.
return logger
}
func main() {
ctx := context.Background()
err := Run(ctx, os.Args, os.Stdin, os.Stdout, os.Stderr)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
os.Exit(1)
}
}