-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
73 lines (60 loc) · 1.46 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v2"
)
type Config struct {
MaxDownloads int `yaml:"max_downloads"`
DownloadDir string `yaml:"download_dir"`
DatabaseFile string `yaml:"database_file"`
LogTime bool `yaml:"log_time"`
LogLevel LogLevel `yaml:"log_level"`
}
type LogLevel zapcore.Level
var configPath = flag.String("config", "/etc/podcaster/config.yaml", "Path of the config file")
func (s *LogLevel) UnmarshalYAML(unmarshal func(interface{}) error) error {
var lvl string
if err := unmarshal(&lvl); err != nil {
return err
}
level := zap.InfoLevel // default
switch strings.ToLower(lvl) {
case "debug":
level = zap.DebugLevel
case "info":
level = zap.InfoLevel
case "warn":
level = zap.WarnLevel
case "error":
level = zap.ErrorLevel
default:
fmt.Printf("unknown log level in config (%s), fallback to info level\n", lvl)
}
*s = LogLevel(level)
return nil
}
func LoadConfig() *Config {
var config = Config{
MaxDownloads: 10,
DownloadDir: "./downloads",
DatabaseFile: "./podcasts.db",
LogTime: true,
LogLevel: LogLevel(zap.DebugLevel),
}
bytes, err := os.ReadFile(*configPath)
if err != nil {
fmt.Println("reading config file failed, using defaults: ", err)
return &config
}
err = yaml.Unmarshal(bytes, &config)
if err != nil {
fmt.Println("parsing config file failed: ", err)
return &config
}
return &config
}