-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
113 lines (95 loc) · 3.19 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package config
import (
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
const (
defaultLatitude = "41.26"
defaultLongitude = "-81.86"
defaultBlockTemplate = `{{.Forecast.Temperature}}°F {{.Forecast.ShortForecast}} {{.Forecast.ProbabilityOfPrecipitation.Value}}% <span foreground="red">{{.Alert.Properties.Event}}</span>`
defaultTimeToShow = 30
defaultAlertTemplate = `{{.Alert.Properties.Event}}`
defaultDetailTemplate = `{{.Alert.Properties.Description}}`
defaultAlertTitle = "Weather Alert"
defaultDetailTitle = "Weather Details"
latitudeVariable = "location.latitude"
longitudeVariable = "location.longitude"
blockTemplateVariable = "block.template"
alertPopupTemplateVariable = "alertPopup.template"
alertPopupTimeToShowVariable = "alertPopup.timeToShow"
detailPopupTemplateVariable = "detailPopup.template"
detailPopupTimeToShowVariable = "detailPopup.timeToShow"
buttonVariable = "BLOCK_BUTTON"
defaultBlockButton = "-1"
alertTitleVariable = "alertPopup.title"
detailTitleVariable = "detailPopup.title"
intervalVariable = "block.interval"
defaultInterval = 30
configName = "config.yml"
)
type Location struct {
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
}
type Block struct {
Template string `json:"template"`
Interval int `json:"interval"`
Button int `json:"button"`
}
type Popup struct {
Template string `json:"template"`
TimeToShow int `json:"timeToShow"`
Title string `json:"title"`
}
type Config struct {
CheckInterval int `json:"interval"`
Location Location `json:"location"`
Block Block `json:"block"`
AlertPopup Popup `json:"alertPopup"`
DetailPopup Popup `json:"detailPopup"`
}
var MyConfig Config
var ConfigLocation string = `$HOME/.config/i3persistantweatherblock`
func SetupConfig(changeChannel chan<- struct{}) error {
setDefaults()
setEnv()
err := SetConfigFile()
if err != nil {
return err
}
err = viper.Unmarshal(&MyConfig)
if err != nil {
return err
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
changeChannel <- struct{}{}
})
return nil
}
func setDefaults() {
viper.SetDefault(latitudeVariable, defaultLatitude)
viper.SetDefault(longitudeVariable, defaultLongitude)
viper.SetDefault(blockTemplateVariable, defaultBlockTemplate)
viper.SetDefault(alertPopupTemplateVariable, defaultAlertTemplate)
viper.SetDefault(alertPopupTimeToShowVariable, defaultTimeToShow)
viper.SetDefault(detailPopupTemplateVariable, defaultDetailTemplate)
viper.SetDefault(detailPopupTimeToShowVariable, defaultTimeToShow)
viper.SetDefault(buttonVariable, defaultBlockButton)
viper.SetDefault(alertTitleVariable, defaultAlertTitle)
viper.SetDefault(detailTitleVariable, defaultDetailTitle)
viper.SetDefault(intervalVariable, defaultInterval)
}
func setEnv() {
viper.AutomaticEnv()
}
func SetConfigFile() error {
viper.SetConfigName(configName)
viper.SetConfigType("yaml")
viper.AddConfigPath(ConfigLocation)
err := viper.ReadInConfig()
return err
}
func IsButtonPressed() bool {
return viper.GetString(buttonVariable) != "-1"
}