telephant/config.go

72 lines
1.4 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
/*
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
*/
Instance string
Username string
Password string
ClientID string
ClientSecret string
2017-08-29 05:09:17 +00:00
}
// Config holds chirp's config settings
type Config struct {
Account []Account
Style string
}
const (
configFile = "chirp.conf"
)
// LoadConfig returns the current config as a Config struct
func LoadConfig() Config {
_, err := os.Stat(configFile)
if err != nil {
SaveConfig(Config{
Style: "Material",
Account: []Account{
{
2019-05-01 15:15:56 +00:00
Instance: "your instance",
Username: "your username",
Password: "your password",
ClientID: "your client id",
ClientSecret: "your client secret",
2017-08-29 05:09:17 +00:00
},
},
})
log.Fatal("Config file is missing, but a template was created for you! Please edit ", configFile)
}
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
func SaveConfig(config Config) {
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
}
}