Handle settings
This commit is contained in:
parent
9b91d1857f
commit
d79e31f673
7 changed files with 208 additions and 56 deletions
54
model/settings.go
Normal file
54
model/settings.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package reveil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Settings represents the settings panel.
|
||||
type Settings struct {
|
||||
Language string `json:"language"`
|
||||
GongInterval time.Duration `json:"gong_interval"`
|
||||
WeatherDelay time.Duration `json:"weather_delay"`
|
||||
WeatherAction string `json:"weather_action"`
|
||||
}
|
||||
|
||||
// ExistsSettings checks if the settings file can by found at the given path.
|
||||
func ExistsSettings(settingsPath string) bool {
|
||||
_, err := os.Stat(settingsPath)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
// ReadSettings parses the file at the given location.
|
||||
func ReadSettings(path string) (*Settings, error) {
|
||||
var s Settings
|
||||
if fd, err := os.Open(path); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jdec := json.NewDecoder(fd)
|
||||
|
||||
if err := jdec.Decode(&s); err != nil {
|
||||
return &s, err
|
||||
}
|
||||
|
||||
return &s, nil
|
||||
}
|
||||
}
|
||||
|
||||
// SaveSettings saves settings at the given location.
|
||||
func SaveSettings(path string, s interface{}) error {
|
||||
if fd, err := os.Create(path); err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jenc := json.NewEncoder(fd)
|
||||
|
||||
if err := jenc.Encode(s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue