-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
63 lines (53 loc) · 1.48 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
package main
import (
"os"
"path/filepath"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// Config holds the parsed config from config.yml
type Config struct {
Watch struct {
Path string `yaml:"path"` // Absolute path to the file to watch
} `yaml:"watch"`
Destination struct {
Path string `yaml:"path"` // Absolute path to the file to watch
} `yaml:"destination"`
Wait int `yaml:"wait"` // Number in seconds of how long to wait after the last write to copy the file
}
func getConfig() Config {
// Open config file
f, err := os.Open("config.yml")
if err != nil {
logrus.Fatalf("Error opening config.yml: %s", err)
}
defer f.Close()
// Parse yaml
var cfg Config
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
logrus.Fatalf("Error decoding config.yml: %s", err)
}
// Normalize watch path
abs, err := filepath.Abs(cfg.Watch.Path)
if err != nil {
logrus.Fatalf("Error getting watch absolute path: %s", err)
}
cfg.Watch.Path = abs
// Normalize destination path
abs, err = filepath.Abs(cfg.Destination.Path)
if err != nil {
logrus.Fatalf("Error getting destination absolute path: %s", err)
}
cfg.Destination.Path = abs
logrus.Print("Configuration loaded successfully")
return cfg
}
func setupLogging() {
f, err := os.OpenFile("tiddly-saver.log", os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
logrus.Fatalf("Error opening log file: %s", err)
}
logrus.SetOutput(f)
}