forked from viewscreen/viewscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
65 lines (54 loc) · 984 Bytes
/
config.go
File metadata and controls
65 lines (54 loc) · 984 Bytes
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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"sync"
)
type Config struct {
sync.RWMutex
filename string
// Settings
Ratio float64 `json:"ratio"`
}
func NewConfig(filename string) (*Config, error) {
filename = filepath.Join(downloadDir, filename)
c := &Config{filename: filename}
b, err := ioutil.ReadFile(filename)
// Default for new config
if os.IsNotExist(err) {
c.Ratio = 1.5
return c, c.Save()
}
if err != nil {
return nil, err
}
// Open existing config
if err := json.Unmarshal(b, c); err != nil {
return nil, err
}
return c, nil
}
func (c *Config) Get() Config {
c.RLock()
defer c.RUnlock()
return Config{
Ratio: c.Ratio,
}
}
func (c *Config) SetRatio(n float64) error {
c.Lock()
c.Ratio = n
c.Unlock()
return c.Save()
}
func (c *Config) Save() error {
c.RLock()
defer c.RUnlock()
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
return Overwrite(c.filename, b, 0644)
}