-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
97 lines (82 loc) · 2.27 KB
/
config.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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var (
supportedConfigFormats = []string{".json", ".yaml", ".yml", ".toml"}
LogFieldModuleConfig = []any{"module", "config"}
LogLevels = []string{zerolog.LevelTraceValue, zerolog.LevelDebugValue, zerolog.LevelInfoValue, zerolog.LevelWarnValue, zerolog.LevelErrorValue, zerolog.LevelFatalValue, zerolog.LevelPanicValue}
DefaultLogLevel = zerolog.InfoLevel
)
type configFile string
func (c configFile) AfterApply(cli *CLI) error {
ext := filepath.Ext(c.String())
if !slices.Contains(supportedConfigFormats, ext) {
return fmt.Errorf("unsupported config file format %q. Supported formats: %s", ext, strings.Join(supportedConfigFormats, ", "))
}
return nil
}
func (c configFile) String() string {
return string(c)
}
type Config struct {
logger zerolog.Logger
}
type Option func(*Config) error
type Globals struct {
ConfigFile configFile `short:"c" help:"Config file location. Supported formats: ${supported_formats}" default:"default.yaml" type:"path"`
LogLevel string `short:"l" help:"Logging level. One of: ${log_levels}" default:"${default_log_level}"`
config *Config
}
type CLI struct {
Globals
kong.Plugins
}
// NewConfig creates a new Config
func NewConfig(opts ...Option) (*Config, error) {
c := &Config{
logger: log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.TimeOnly,
NoColor: false,
},
),
}
var allErrors error
for _, opt := range opts {
if err := opt(c); err != nil {
allErrors = errors.Join(allErrors, err)
}
}
return c, allErrors
}
// Logger returns the config logger
func (c *Config) Logger() *zerolog.Logger {
return &c.logger
}
// WithLogLevel sets the log level Option
func WithLogLevel(level string) Option {
return func(c *Config) error {
parsedLevel, err := zerolog.ParseLevel(level)
if err != nil {
c.Logger().Warn().Fields(LogFieldModuleConfig).
Msgf("Unknown log level %q. Defaulting to %q", level, DefaultLogLevel)
parsedLevel = DefaultLogLevel
}
if parsedLevel == zerolog.NoLevel {
parsedLevel = DefaultLogLevel
}
c.logger = c.Logger().Level(parsedLevel)
return nil
}
}