-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
41 lines (34 loc) · 884 Bytes
/
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
package main
import (
"os"
"io"
"github.com/qiangxue/go-env"
"gopkg.in/yaml.v2"
"drive-sync/log"
)
type Config struct {
Folder string `yaml:"folder" env:"FOLDER"`
Schedule string `yaml:"schedule" env:"SCHEDULE"`
TargetFolderID string `yaml:"targetFolderID" env:"TARGET_FOLDER_ID"`
}
func LoadConfig(filename string, logger log.Logger) (*Config, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
return nil, err
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
// load from environment variables prefixed with "APP_"
if err = env.New("APP_", logger.Infof).Load(&config); err != nil {
return nil, err
}
return &config, nil
}