discordo/config/pkg.go
ayntgl a7cb969fc6
feat: add command-line flags for token and config (#144)
* feat: add command-line flags for token and config

* docs(README): mention token and config command-line flag

* Remove env var
2022-03-17 16:00:10 +04:00

56 lines
946 B
Go

package config
import (
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
type Config struct {
General GeneralConfig `toml:"general"`
Theme ThemeConfig `toml:"theme"`
Keybindings KeybindingsConfig `toml:"keybindings"`
}
func New() *Config {
return &Config{
General: newGeneralConfig(),
Theme: newThemeConfig(),
Keybindings: newKeybindingsConfig(),
}
}
func (c *Config) Load(path string) {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
panic(err)
}
if _, err = os.Stat(path); os.IsNotExist(err) {
f, err := os.Create(path)
if err != nil {
panic(err)
}
err = toml.NewEncoder(f).Encode(c)
if err != nil {
panic(err)
}
} else {
_, err = toml.DecodeFile(path, &c)
if err != nil {
panic(err)
}
}
}
func DefaultPath() string {
path, err := os.UserConfigDir()
if err != nil {
panic(err)
}
path += "/discordo/config.toml"
return path
}