Initial project
This commit is contained in:
commit
e715024cbd
23 files changed
+1547
No files matched your search
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
Copyright © 2026 Craig Williams <craig@avsdev.uk>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type JobSearchConfigLogging struct {
|
||||
Level string
|
||||
}
|
||||
|
||||
// Default configuration
|
||||
func defaultConfigLogging() JobSearchConfigLogging {
|
||||
return JobSearchConfigLogging{
|
||||
Level: "warn",
|
||||
}
|
||||
}
|
||||
|
||||
// Converts configuration to a map
|
||||
func (conf JobSearchConfigLogging) Map() map[string]any {
|
||||
return map[string]any{
|
||||
"level": conf.Level,
|
||||
}
|
||||
}
|
||||
|
||||
// Converts configuration to a flat dotted format
|
||||
func (conf JobSearchConfigLogging) Flatten() map[string]any {
|
||||
confMap := conf.Map()
|
||||
retMap := map[string]any{}
|
||||
for k, v := range confMap {
|
||||
retMap["logging."+k] = v
|
||||
}
|
||||
return retMap
|
||||
}
|
||||
|
||||
// Returns the expected type of a configuration key value
|
||||
func (c JobSearchConfigLogging) TypeOf(key string) (reflect.Kind, error) {
|
||||
switch key {
|
||||
case "level":
|
||||
return reflect.String, nil
|
||||
default:
|
||||
return reflect.Invalid, errors.New("unknown configuration key")
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the value of a configuration key
|
||||
func (c JobSearchConfigLogging) ValueOf(key string) (any, error) {
|
||||
switch key {
|
||||
case "level":
|
||||
return c.Level, nil
|
||||
default:
|
||||
return "", errors.New("unknown configuration key")
|
||||
}
|
||||
}
|
||||
|
||||
// Sets the value of a configuration key
|
||||
func (c *JobSearchConfigLogging) SetValue(key string, value any) error {
|
||||
switch key {
|
||||
case "level":
|
||||
newLevel := value.(string)
|
||||
switch newLevel {
|
||||
case "debug", "info", "warn", "error":
|
||||
c.Level = value.(string)
|
||||
default:
|
||||
return errors.New(
|
||||
"invalid log level. Must be one of: debug, info, warn, error")
|
||||
}
|
||||
default:
|
||||
return errors.New("unknown configuration key")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ConfigLevelToLogLevel(level string) slog.Level {
|
||||
switch level {
|
||||
case "debug":
|
||||
return slog.LevelDebug
|
||||
case "info":
|
||||
return slog.LevelInfo
|
||||
case "warn":
|
||||
return slog.LevelWarn
|
||||
case "error":
|
||||
return slog.LevelError
|
||||
default:
|
||||
return 99
|
||||
}
|
||||
}
|
||||
|
||||
func (c *JobSearchConfigLogging) LogLevel() slog.Level {
|
||||
return ConfigLevelToLogLevel(c.Level)
|
||||
}
|
||||
Reference in new issue
Block a user