config.go 950 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package config

import (
	"encoding/json"
	"io/ioutil"
	"log"
)

const ConfigFile = "./config.json"

type BotConfig struct {
12 13 14 15 16
	TelegramToken         string
	KnownIds              map[int]bool
	Password              string
	Player                string
	PlayerCommandLineArgs []string
17 18 19 20 21 22 23 24 25
}

func LoadConfig() BotConfig {
	configFileData, _ := ioutil.ReadFile(ConfigFile)

	var config BotConfig
	err := json.Unmarshal(configFileData, &config)
	if err != nil {
		StoreConfig(BotConfig{
26 27 28 29
			TelegramToken:         "token",
			KnownIds:              make(map[int]bool),
			Player:                "omxplayer",
			PlayerCommandLineArgs: make([]string, 0),
30 31 32 33 34 35 36 37 38 39 40 41 42
		})
		log.Fatalf("Unable to load configfile <%s>. Writing empty file.\n", err.Error())
	}
	return config
}

func StoreConfig(config BotConfig) {
	m, _ := json.Marshal(config)
	err := ioutil.WriteFile(ConfigFile, m, 0666)
	if err != nil {
		log.Printf("Unable to save configfile <%s>\n", err.Error())
	}
}