package config import ( "bytes" "errors" "io/ioutil" "log" "os" "github.com/BurntSushi/toml" ) // Config main type type Config struct { Features Features Web Web Twitter Twitter Telegram Telegram Database Database } // Twitter related config type Twitter struct { ConsumerKey string ConsumerSecret string AccessKey string AccessSecret string MagicHashtag string } // Telegram related config type Telegram struct { APIKey string } // Database related config type Database struct { Dialect string Connection string } // Web related config type Web struct { BindUnix bool Bind string WebDir string } // Features related config type Features struct { Bot bool Web bool } // C holds the loaded configuration var C Config // LoadDefaults puts default values into C func LoadDefaults() { C.Features.Bot = true C.Features.Web = true C.Web.BindUnix = false C.Web.Bind = ":8080" C.Web.WebDir = "./web" C.Twitter.AccessKey = "ACCESSKEY" C.Twitter.AccessSecret = "ACCESSSECRET" C.Twitter.ConsumerKey = "CONSUMERKEY" C.Twitter.ConsumerSecret = "CONSUMERSECRET" C.Twitter.MagicHashtag = "#DB640" C.Telegram.APIKey = "APIKEY" C.Database.Dialect = "sqlite3" C.Database.Connection = ":memory:" } // LoadConfig loads the configuration from given path func LoadConfig(path string) error { LoadDefaults() _, err := toml.DecodeFile(path, &C) if errors.Is(err, os.ErrNotExist) { log.Printf("Could not find file \"%s\", using defaults!", path) LoadDefaults() err = WriteConfig(path) return err } return err } // WriteConfig writes the configuration to the given path func WriteConfig(path string) error { buf := new(bytes.Buffer) err := toml.NewEncoder(buf).Encode(C) if err != nil { return err } err = ioutil.WriteFile(path, buf.Bytes(), 0644) return err }