-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
89 lines (79 loc) · 2.88 KB
/
root.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 cmd
import (
"os"
"slices"
"strings"
"github.com/mattn/go-colorable"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var (
cfg = struct {
LogLevel string
LogFormat string
LogCaller bool
}{}
validLogLevels = []string{"trace", "debug", "info", "warn", "error"}
validLogFormats = []string{"plain", "color", "json"}
)
func rootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: `rewrite-app`,
Short: `rewrite-app is a application to apply code refactoring recipes (e. g. openrewrite) to a codebase.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// log format
if !slices.Contains(validLogFormats, cfg.LogFormat) {
log.Error().Str("current", cfg.LogFormat).Strs("valid", validLogFormats).Msg("invalid log format specified")
os.Exit(1)
}
var logContext zerolog.Context
if cfg.LogFormat == "plain" {
logContext = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: true}).With().Timestamp()
} else if cfg.LogFormat == "color" {
colorableOutput := colorable.NewColorableStdout()
logContext = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: colorableOutput, NoColor: false}).With().Timestamp()
} else if cfg.LogFormat == "json" {
logContext = zerolog.New(os.Stderr).Output(os.Stderr).With().Timestamp()
}
if cfg.LogCaller {
logContext = logContext.Caller()
}
log.Logger = logContext.Logger()
// log time format
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
// log level
if !slices.Contains(validLogLevels, cfg.LogLevel) {
log.Error().Str("current", cfg.LogLevel).Strs("valid", validLogLevels).Msg("invalid log level specified")
os.Exit(1)
}
if cfg.LogLevel == "trace" {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
} else if cfg.LogLevel == "debug" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else if cfg.LogLevel == "info" {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
} else if cfg.LogLevel == "warn" {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
} else if cfg.LogLevel == "error" {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
}
// logging config
log.Debug().Str("log-level", cfg.LogLevel).Str("log-format", cfg.LogFormat).Bool("log-caller", cfg.LogCaller).Msg("configured logging")
},
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
os.Exit(0)
},
}
cmd.PersistentFlags().StringVar(&cfg.LogLevel, "log-level", "info", "log level - allowed: "+strings.Join(validLogLevels, ","))
cmd.PersistentFlags().StringVar(&cfg.LogFormat, "log-format", "color", "log format - allowed: "+strings.Join(validLogFormats, ","))
cmd.PersistentFlags().BoolVar(&cfg.LogCaller, "log-caller", false, "include caller in log functions")
cmd.AddCommand(runCmd())
cmd.AddCommand(versionCmd())
return cmd
}
// Execute executes the root command.
func Execute() error {
return rootCmd().Execute()
}