forked from maded2/plotng
/
plotConfig.go
50 lines (46 loc) · 1.12 KB
/
plotConfig.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
package internal
import (
"encoding/json"
"log"
"os"
"sync"
"time"
)
type Config struct {
TargetDirectory []string
TempDirectory []string
NumberOfParallelPlots int
Fingerprint string
StaggeringDelay int
ShowPlotLog bool
}
type PlotConfig struct {
ConfigPath string
CurrentConfig *Config
LastMod time.Time
Lock sync.RWMutex
}
func (pc *PlotConfig) ProcessConfig() {
if fs, err := os.Lstat(pc.ConfigPath); err != nil {
log.Printf("Failed to open config file [%s]: %s\n", pc.ConfigPath, err)
} else {
if pc.LastMod != fs.ModTime() {
if f, err := os.Open(pc.ConfigPath); err != nil {
log.Printf("Failed to open config file [%s]: %s\n", pc.ConfigPath, err)
} else {
decoder := json.NewDecoder(f)
var newConfig Config
if err := decoder.Decode(&newConfig); err != nil {
log.Printf("Failed to process config file [%s]: %s\n", pc.ConfigPath, err)
} else {
pc.Lock.Lock()
pc.CurrentConfig = &newConfig
pc.Lock.Unlock()
log.Printf("New configuration loaded")
}
f.Close()
}
pc.LastMod = fs.ModTime()
}
}
}