Store config in platform-specific path

This commit is contained in:
Christian Muehlhaeuser 2019-05-09 16:52:56 +02:00
parent 2d12ecac44
commit c876c23019
No known key found for this signature in database
GPG key ID: 3CF9FA45CA1EBB7E
2 changed files with 14 additions and 9 deletions

View file

@ -28,15 +28,11 @@ type Config struct {
Style string
}
const (
configFile = "telephant.conf"
)
// LoadConfig returns the current config as a Config struct
func LoadConfig() Config {
func LoadConfig(configFile string) Config {
_, err := os.Stat(configFile)
if err != nil {
SaveConfig(Config{
SaveConfig(configFile, Config{
Style: "Material",
Account: []Account{Account{}},
})
@ -52,7 +48,7 @@ func LoadConfig() Config {
}
// SaveConfig stores the current config
func SaveConfig(config Config) {
func SaveConfig(configFile string, config Config) {
f, err := os.Create(configFile)
if err != nil {
log.Fatal("Could not open config file: ", err)

View file

@ -10,6 +10,7 @@ import (
"github.com/therecipe/qt/qml"
"github.com/therecipe/qt/quickcontrols2"
gap "github.com/muesli/go-app-paths"
"github.com/muesli/telephant/accounts/mastodon"
)
@ -203,7 +204,15 @@ func main() {
setupQmlBridges()
// load config
config = LoadConfig()
scope := gap.NewScope(gap.User, "fribbledom.com", "telephant")
configDir, err := scope.ConfigPath("")
if err != nil {
panic(err)
}
os.MkdirAll(configDir, 0700)
configFile, err := scope.ConfigPath("telephant.conf")
config = LoadConfig(configFile)
if config.Style == "" {
config.Style = "Material"
}
@ -214,5 +223,5 @@ func main() {
// save config
config.Style = configBridge.Style()
SaveConfig(config)
SaveConfig(configFile, config)
}