-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
62 lines (51 loc) · 1.19 KB
/
config.go
File metadata and controls
62 lines (51 loc) · 1.19 KB
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
package main
import (
"github.com/awesome-gocui/gocui"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
)
const (
ResultErr = "ERR"
OkBgColor = gocui.Attribute(41)
OkFgColor = gocui.ColorBlack
WarnBgColor = gocui.ColorYellow
WarnFgColor = gocui.ColorBlack
ErrBgColor = gocui.ColorRed
ErrFgColor = gocui.ColorBlack
defaultThreshold = float32(0.3)
)
type Config struct {
Items []*Item `yaml:"items"`
}
type Item struct {
Label string `yaml:"label,omitempty"`
Script string `yaml:"script"`
Unit string `yaml:"unit,omitempty"`
Threshold float32 `yaml:"threshold,omitempty"`
//Color ui.Color `yaml:"color,omitempty"`
}
func NewConfig(path string) *Config {
config := readFile(path)
config.setDefaults()
return config
}
func readFile(location string) *Config {
yamlFile, err := ioutil.ReadFile(location)
if err != nil {
log.Fatalf("Failed to read config file: %s", location)
}
config := new(Config)
err = yaml.Unmarshal(yamlFile, config)
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
return config
}
func (config *Config) setDefaults() {
for _, item := range config.Items {
if item.Threshold == 0 {
item.Threshold = defaultThreshold
}
}
}