-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
96 lines (83 loc) · 2.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
package config
import (
"fmt"
"log"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
const (
AppName = "watch-go"
staticBackupFolder = "Backup Files"
)
var (
cfg config
File string
Debug bool
General = &cfg.General
FileSystemCfg = &cfg.FileSystem
)
type config struct {
General struct {
Worker int `yaml:"worker"`
WorkerBuffer int `yaml:"worker_buffer"`
EventBuffer int `yaml:"event_buffer"`
Verbose bool `yaml:"verbose"`
ErrorLog string `yaml:"error_log"`
InfoLog string `yaml:"info_log"`
PidFile string `yaml:"pid_file"`
} `yaml:"general"`
FileSystem FileSystemConfig `yaml:"file_system"`
}
type FileSystemConfig struct {
Paths []string `yaml:"paths"`
Compress CompressConfig `yaml:"compress"`
MaxFileSize int64 `yaml:"max_file_size"`
Backup struct {
HardDrivePath string `yaml:"hard_drive_path"`
Prefix []string `yaml:"prefix"`
} `yaml:"backup"`
}
type CompressConfig struct {
Enabled bool `yaml:"enabled"`
Quality int `yaml:"quality"`
}
// LoadConfig Read and parse config file.
func LoadConfig(configFile string) error {
filename, err := filepath.Abs(configFile)
if err != nil {
log.Printf("[%s] error: %s fail find config\n", AppName, err)
return err
}
yamlFile, err := os.ReadFile(filename)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, &cfg)
if err != nil {
log.Printf("[%s] error: %s parse from file %s\n", AppName, err, filename)
return err
}
log.Printf("load settings √\n")
return nil
}
// ReloadConfig parse yml config.
func ReloadConfig() error {
filename, err := filepath.Abs(File)
if err != nil {
return fmt.Errorf("can not be reloaded, filepath Abs error: %s", err.Error())
}
yamlFile, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("can not be reloaded, can not read yaml-File: %s", err.Error())
}
err = yaml.Unmarshal(yamlFile, &cfg)
if err != nil {
return fmt.Errorf("[%s] error: %s parse from file %s\n", AppName, err, filename)
}
log.Printf("Config file re-load: %s", filename)
return nil
}
func GetStaticBackupFolder() string {
return staticBackupFolder
}