| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
var DefaultConfig = Config{ |
| 14 |
Version: 1, |
| 15 |
Skip: ptr([]string{}), |
| 16 |
Keep: ptr([]string{}), |
| 17 |
TimeFields: ptr([]string{"time", "ts", "@timestamp", "timestamp", "Timestamp"}), |
| 18 |
MessageFields: ptr([]string{"message", "msg", "Body"}), |
| 19 |
LevelFields: ptr([]string{"level", "lvl", "loglevel", "severity", "SeverityText"}), |
| 20 |
SortLongest: ptr(true), |
| 21 |
SkipUnchanged: ptr(true), |
| 22 |
Truncates: ptr(true), |
| 23 |
LightBg: ptr(false), |
| 24 |
ColorMode: ptr("auto"), |
| 25 |
TruncateLength: ptr(15), |
| 26 |
TimeFormat: ptr(time.Stamp), |
| 27 |
Interrupt: ptr(false), |
| 28 |
SkipCheckForUpdates: ptr(false), |
| 29 |
Palette: nil, |
| 30 |
} |
| 31 |
|
| 32 |
func GetDefaultConfigFilepath() (string, error) { |
| 33 |
home, err := os.UserHomeDir() |
| 34 |
if err != nil { |
| 35 |
return "", fmt.Errorf("$HOME not set, can't determine a config file path") |
| 36 |
} |
| 37 |
configDirpath := filepath.Join(home, ".config", "humanlog") |
| 38 |
configFilepath := filepath.Join(configDirpath, "config.json") |
| 39 |
dfi, err := os.Stat(configDirpath) |
| 40 |
if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 41 |
return "", fmt.Errorf("config dir %q can't be read: %v", configDirpath, err) |
| 42 |
} |
| 43 |
if errors.Is(err, os.ErrNotExist) { |
| 44 |
if err := os.MkdirAll(configDirpath, 0700); err != nil { |
| 45 |
return "", fmt.Errorf("config dir %q can't be created: %v", configDirpath, err) |
| 46 |
} |
| 47 |
} else if !dfi.IsDir() { |
| 48 |
return "", fmt.Errorf("config dir %q isn't a directory", configDirpath) |
| 49 |
} |
| 50 |
ffi, err := os.Stat(configFilepath) |
| 51 |
if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 52 |
return "", fmt.Errorf("can't stat config file: %v", err) |
| 53 |
} |
| 54 |
if errors.Is(err, os.ErrNotExist) { |
| 55 |
|
| 56 |
} else if !ffi.Mode().IsRegular() { |
| 57 |
return "", fmt.Errorf("config file %q isn't a regular file", configFilepath) |
| 58 |
} |
| 59 |
return configFilepath, nil |
| 60 |
} |
| 61 |
|
| 62 |
func ReadConfigFile(path string, dflt *Config) (*Config, error) { |
| 63 |
configFile, err := os.Open(path) |
| 64 |
if err != nil { |
| 65 |
if !errors.Is(err, os.ErrNotExist) { |
| 66 |
return nil, fmt.Errorf("opening config file %q: %v", path, err) |
| 67 |
} |
| 68 |
return dflt, nil |
| 69 |
} |
| 70 |
defer configFile.Close() |
| 71 |
var cfg Config |
| 72 |
if err := json.NewDecoder(configFile).Decode(&cfg); err != nil { |
| 73 |
return nil, fmt.Errorf("decoding config file: %v", err) |
| 74 |
} |
| 75 |
return cfg.populateEmpty(dflt), nil |
| 76 |
} |
| 77 |
|
| 78 |
type Config struct { |
| 79 |
Version int `json:"version"` |
| 80 |
Skip *[]string `json:"skip"` |
| 81 |
Keep *[]string `json:"keep"` |
| 82 |
TimeFields *[]string `json:"time-fields"` |
| 83 |
MessageFields *[]string `json:"message-fields"` |
| 84 |
LevelFields *[]string `json:"level-fields"` |
| 85 |
SortLongest *bool `json:"sort-longest"` |
| 86 |
SkipUnchanged *bool `json:"skip-unchanged"` |
| 87 |
Truncates *bool `json:"truncates"` |
| 88 |
LightBg *bool `json:"light-bg"` |
| 89 |
ColorMode *string `json:"color-mode"` |
| 90 |
TruncateLength *int `json:"truncate-length"` |
| 91 |
TimeFormat *string `json:"time-format"` |
| 92 |
Palette *TextPalette `json:"palette"` |
| 93 |
Interrupt *bool `json:"interrupt"` |
| 94 |
SkipCheckForUpdates *bool `json:"skip_check_updates"` |
| 95 |
} |
| 96 |
|
| 97 |
func (cfg Config) populateEmpty(other *Config) *Config { |
| 98 |
out := *(&cfg) |
| 99 |
if out.Skip == nil && out.Keep == nil { |
| 100 |
|
| 101 |
|
| 102 |
out.Skip = other.Skip |
| 103 |
out.Keep = other.Keep |
| 104 |
} |
| 105 |
if other.TimeFields != nil { |
| 106 |
if out.TimeFields == nil { |
| 107 |
out.TimeFields = ptr(make([]string, 0, len(*other.TimeFields))) |
| 108 |
} |
| 109 |
*out.TimeFields = append(*out.TimeFields, *other.TimeFields...) |
| 110 |
} |
| 111 |
if out.MessageFields == nil && other.MessageFields != nil { |
| 112 |
if out.MessageFields == nil { |
| 113 |
out.MessageFields = ptr(make([]string, 0, len(*other.MessageFields))) |
| 114 |
} |
| 115 |
*out.MessageFields = append(*out.MessageFields, *other.MessageFields...) |
| 116 |
} |
| 117 |
if out.LevelFields == nil && other.LevelFields != nil { |
| 118 |
if out.LevelFields == nil { |
| 119 |
out.LevelFields = ptr(make([]string, 0, len(*other.LevelFields))) |
| 120 |
} |
| 121 |
*out.LevelFields = append(*out.LevelFields, *other.LevelFields...) |
| 122 |
} |
| 123 |
if out.SortLongest == nil && other.SortLongest != nil { |
| 124 |
out.SortLongest = other.SortLongest |
| 125 |
} |
| 126 |
if out.SkipUnchanged == nil && other.SkipUnchanged != nil { |
| 127 |
out.SkipUnchanged = other.SkipUnchanged |
| 128 |
} |
| 129 |
if out.Truncates == nil && other.Truncates != nil { |
| 130 |
out.Truncates = other.Truncates |
| 131 |
} |
| 132 |
if out.LightBg == nil && other.LightBg != nil { |
| 133 |
out.LightBg = other.LightBg |
| 134 |
} |
| 135 |
if out.ColorMode == nil && other.ColorMode != nil { |
| 136 |
out.ColorMode = other.ColorMode |
| 137 |
} |
| 138 |
if out.TruncateLength == nil && other.TruncateLength != nil { |
| 139 |
out.TruncateLength = other.TruncateLength |
| 140 |
} |
| 141 |
if out.TimeFormat == nil && other.TimeFormat != nil { |
| 142 |
out.TimeFormat = other.TimeFormat |
| 143 |
} |
| 144 |
if out.Palette == nil && other.Palette != nil { |
| 145 |
out.Palette = other.Palette |
| 146 |
} |
| 147 |
if out.Interrupt == nil && other.Interrupt != nil { |
| 148 |
out.Interrupt = other.Interrupt |
| 149 |
} |
| 150 |
if out.SkipCheckForUpdates == nil && other.SkipCheckForUpdates != nil { |
| 151 |
out.SkipCheckForUpdates = other.SkipCheckForUpdates |
| 152 |
} |
| 153 |
return &out |
| 154 |
} |
| 155 |
|
| 156 |
type TextPalette struct { |
| 157 |
KeyColor []string `json:"key"` |
| 158 |
ValColor []string `json:"val"` |
| 159 |
TimeLightBgColor []string `json:"time_light_bg"` |
| 160 |
TimeDarkBgColor []string `json:"time_dark_bg"` |
| 161 |
MsgLightBgColor []string `json:"msg_light_bg"` |
| 162 |
MsgAbsentLightBgColor []string `json:"msg_absent_light_bg"` |
| 163 |
MsgDarkBgColor []string `json:"msg_dark_bg"` |
| 164 |
MsgAbsentDarkBgColor []string `json:"msg_absent_dark_bg"` |
| 165 |
DebugLevelColor []string `json:"debug_level"` |
| 166 |
InfoLevelColor []string `json:"info_level"` |
| 167 |
WarnLevelColor []string `json:"warn_level"` |
| 168 |
ErrorLevelColor []string `json:"error_level"` |
| 169 |
PanicLevelColor []string `json:"panic_level"` |
| 170 |
FatalLevelColor []string `json:"fatal_level"` |
| 171 |
UnknownLevelColor []string `json:"unknown_level"` |
| 172 |
} |
| 173 |
|
| 174 |
type ColorMode int |
| 175 |
|
| 176 |
const ( |
| 177 |
ColorModeOff ColorMode = iota |
| 178 |
ColorModeOn |
| 179 |
ColorModeAuto |
| 180 |
) |
| 181 |
|
| 182 |
func GrokColorMode(colorMode string) (ColorMode, error) { |
| 183 |
switch strings.ToLower(colorMode) { |
| 184 |
case "on", "always", "force", "true", "yes", "1": |
| 185 |
return ColorModeOn, nil |
| 186 |
case "off", "never", "false", "no", "0": |
| 187 |
return ColorModeOff, nil |
| 188 |
case "auto", "tty", "maybe", "": |
| 189 |
return ColorModeAuto, nil |
| 190 |
default: |
| 191 |
return ColorModeAuto, fmt.Errorf("'%s' is not a color mode (try 'on', 'off' or 'auto')", colorMode) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func ptr[T any](v T) *T { |
| 196 |
return &v |
| 197 |
} |