-
Notifications
You must be signed in to change notification settings - Fork 53
/
config.go
219 lines (205 loc) · 7.15 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
var DefaultConfig = Config{
Version: 1,
Skip: ptr([]string{}),
Keep: ptr([]string{}),
TimeFields: ptr([]string{"time", "ts", "@timestamp", "timestamp", "Timestamp"}),
MessageFields: ptr([]string{"message", "msg", "Body"}),
LevelFields: ptr([]string{"level", "lvl", "loglevel", "severity", "SeverityText"}),
SortLongest: ptr(true),
SkipUnchanged: ptr(true),
Truncates: ptr(true),
LightBg: ptr(false),
ColorMode: ptr("auto"),
TruncateLength: ptr(15),
TimeFormat: ptr(time.Stamp),
Interrupt: ptr(false),
SkipCheckForUpdates: ptr(false),
Palette: nil,
}
func GetDefaultConfigFilepath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("$HOME not set, can't determine a config file path")
}
configDirpath := filepath.Join(home, ".config", "humanlog")
configFilepath := filepath.Join(configDirpath, "config.json")
dfi, err := os.Stat(configDirpath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("config dir %q can't be read: %v", configDirpath, err)
}
if errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(configDirpath, 0700); err != nil {
return "", fmt.Errorf("config dir %q can't be created: %v", configDirpath, err)
}
} else if !dfi.IsDir() {
return "", fmt.Errorf("config dir %q isn't a directory", configDirpath)
}
ffi, err := os.Stat(configFilepath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("can't stat config file: %v", err)
}
if errors.Is(err, os.ErrNotExist) {
// do nothing
} else if !ffi.Mode().IsRegular() {
return "", fmt.Errorf("config file %q isn't a regular file", configFilepath)
}
return configFilepath, nil
}
func ReadConfigFile(path string, dflt *Config) (*Config, error) {
configFile, err := os.Open(path)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("opening config file %q: %v", path, err)
}
return dflt, nil
}
defer configFile.Close()
var cfg Config
if err := json.NewDecoder(configFile).Decode(&cfg); err != nil {
return nil, fmt.Errorf("decoding config file: %v", err)
}
return cfg.populateEmpty(dflt), nil
}
type Config struct {
Version int `json:"version"`
Skip *[]string `json:"skip"`
Keep *[]string `json:"keep"`
TimeFields *[]string `json:"time-fields"`
MessageFields *[]string `json:"message-fields"`
LevelFields *[]string `json:"level-fields"`
SortLongest *bool `json:"sort-longest"`
SkipUnchanged *bool `json:"skip-unchanged"`
Truncates *bool `json:"truncates"`
LightBg *bool `json:"light-bg"`
ColorMode *string `json:"color-mode"`
TruncateLength *int `json:"truncate-length"`
TimeFormat *string `json:"time-format"`
TimeZone *string `json:"time-zone"`
Palette *TextPalette `json:"palette"`
Interrupt *bool `json:"interrupt"`
SkipCheckForUpdates *bool `json:"skip_check_updates"`
ExperimentalFeatures *Features `json:"experimental_features"`
}
type Features struct {
ReleaseChannel *string `json:"release_channel"`
SendLogsToCloud *bool `json:"send_logs_to_cloud"`
ServeLocalhost *ServeLocalhost `json:"serve_localhost"`
}
type ServeLocalhost struct {
Port int `json:"port"`
Engine string `json:"engine"`
Cfg map[string]interface{} `json:"engine_config"`
}
func (cfg Config) populateEmpty(other *Config) *Config {
cpcfg := cfg
out := &cpcfg
if out.Skip == nil && out.Keep == nil {
// skip and keep are mutually exclusive, so these are
// either both set by default, or not at all
out.Skip = other.Skip
out.Keep = other.Keep
}
if other.TimeFields != nil {
if out.TimeFields == nil {
out.TimeFields = ptr(make([]string, 0, len(*other.TimeFields)))
}
*out.TimeFields = append(*out.TimeFields, *other.TimeFields...)
}
if out.MessageFields == nil && other.MessageFields != nil {
if out.MessageFields == nil {
out.MessageFields = ptr(make([]string, 0, len(*other.MessageFields)))
}
*out.MessageFields = append(*out.MessageFields, *other.MessageFields...)
}
if out.LevelFields == nil && other.LevelFields != nil {
if out.LevelFields == nil {
out.LevelFields = ptr(make([]string, 0, len(*other.LevelFields)))
}
*out.LevelFields = append(*out.LevelFields, *other.LevelFields...)
}
if out.SortLongest == nil && other.SortLongest != nil {
out.SortLongest = other.SortLongest
}
if out.SkipUnchanged == nil && other.SkipUnchanged != nil {
out.SkipUnchanged = other.SkipUnchanged
}
if out.Truncates == nil && other.Truncates != nil {
out.Truncates = other.Truncates
}
if out.LightBg == nil && other.LightBg != nil {
out.LightBg = other.LightBg
}
if out.ColorMode == nil && other.ColorMode != nil {
out.ColorMode = other.ColorMode
}
if out.TruncateLength == nil && other.TruncateLength != nil {
out.TruncateLength = other.TruncateLength
}
if out.TimeFormat == nil && other.TimeFormat != nil {
out.TimeFormat = other.TimeFormat
}
if out.TimeZone == nil && other.TimeZone != nil {
out.TimeZone = other.TimeZone
}
if out.Palette == nil && other.Palette != nil {
out.Palette = other.Palette
}
if out.Interrupt == nil && other.Interrupt != nil {
out.Interrupt = other.Interrupt
}
if out.SkipCheckForUpdates == nil && other.SkipCheckForUpdates != nil {
out.SkipCheckForUpdates = other.SkipCheckForUpdates
}
if out.ExperimentalFeatures == nil && other.ExperimentalFeatures != nil {
out.ExperimentalFeatures = other.ExperimentalFeatures
}
return out
}
type TextPalette struct {
KeyColor []string `json:"key"`
ValColor []string `json:"val"`
TimeLightBgColor []string `json:"time_light_bg"`
TimeDarkBgColor []string `json:"time_dark_bg"`
MsgLightBgColor []string `json:"msg_light_bg"`
MsgAbsentLightBgColor []string `json:"msg_absent_light_bg"`
MsgDarkBgColor []string `json:"msg_dark_bg"`
MsgAbsentDarkBgColor []string `json:"msg_absent_dark_bg"`
DebugLevelColor []string `json:"debug_level"`
InfoLevelColor []string `json:"info_level"`
WarnLevelColor []string `json:"warn_level"`
ErrorLevelColor []string `json:"error_level"`
PanicLevelColor []string `json:"panic_level"`
FatalLevelColor []string `json:"fatal_level"`
UnknownLevelColor []string `json:"unknown_level"`
}
type ColorMode int
const (
ColorModeOff ColorMode = iota
ColorModeOn
ColorModeAuto
)
func GrokColorMode(colorMode string) (ColorMode, error) {
switch strings.ToLower(colorMode) {
case "on", "always", "force", "true", "yes", "1":
return ColorModeOn, nil
case "off", "never", "false", "no", "0":
return ColorModeOff, nil
case "auto", "tty", "maybe", "":
return ColorModeAuto, nil
default:
return ColorModeAuto, fmt.Errorf("'%s' is not a color mode (try 'on', 'off' or 'auto')", colorMode)
}
}
func ptr[T any](v T) *T {
return &v
}