telephant/config.go

61 lines
1.2 KiB
Go
Raw Normal View History

2017-08-29 05:09:17 +00:00
package main
import (
"log"
"os"
"github.com/BurntSushi/toml"
)
// Account holds the settings for one account
type Account struct {
2019-05-01 15:15:56 +00:00
Instance string
ClientID string
ClientSecret string
Token string
2017-08-29 05:09:17 +00:00
}
2019-05-09 14:33:26 +00:00
// Config holds telephant's config settings
2017-08-29 05:09:17 +00:00
type Config struct {
Account []Account
Theme string
Style string
PositionX int
PositionY int
Width int
Height int
FirstRun bool
2017-08-29 05:09:17 +00:00
}
// LoadConfig returns the current config as a Config struct
2019-05-09 14:52:56 +00:00
func LoadConfig(configFile string) Config {
2017-08-29 05:09:17 +00:00
_, err := os.Stat(configFile)
if err != nil {
2019-05-09 14:52:56 +00:00
SaveConfig(configFile, Config{
2019-05-10 13:47:17 +00:00
Theme: "Material",
Style: "Dark",
FirstRun: true,
2019-05-11 00:51:09 +00:00
Account: []Account{{}},
2017-08-29 05:09:17 +00:00
})
//log.Fatal("Config file is missing, but a template was created for you! Please edit ", configFile)
2017-08-29 05:09:17 +00:00
}
var config Config
if _, err := toml.DecodeFile(configFile, &config); err != nil {
2017-08-29 14:05:51 +00:00
log.Fatal("Could not decode config file: ", err)
2017-08-29 05:09:17 +00:00
}
return config
}
// SaveConfig stores the current config
2019-05-09 14:52:56 +00:00
func SaveConfig(configFile string, config Config) {
2017-08-29 05:09:17 +00:00
f, err := os.Create(configFile)
if err != nil {
2017-08-29 14:05:51 +00:00
log.Fatal("Could not open config file: ", err)
2017-08-29 05:09:17 +00:00
}
if err := toml.NewEncoder(f).Encode(config); err != nil {
2017-08-29 14:05:51 +00:00
log.Fatal("Could not encode config: ", err)
2017-08-29 05:09:17 +00:00
}
}