-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
55 lines (45 loc) · 927 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"encoding/json"
"os"
)
var (
configPath = "config.json"
config AutomoverConfig = AutomoverConfig{
Watchlist: []AutomoverWatchedFolder{},
}
)
type AutomoverConfig struct {
Watchlist []AutomoverWatchedFolder `json:"watchlist"`
}
type AutomoverWatchedFolder struct {
WatchPath string `json:"watch_path"`
WatchPattern string `json:"watch_pattern"`
DestinationPath string `json:"destination_path"`
}
func loadConfig() error {
file, err := os.Open(configPath)
if err != nil {
return saveConfig()
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return err
}
return nil
}
func saveConfig() error {
file, err := os.Create(configPath)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(config)
if err != nil {
return err
}
return nil
}