Switch to toml for configuration

This commit is contained in:
ayntgl 2022-01-19 23:34:18 +04:00
parent 3a7e0b4c82
commit 0d4d612111
5 changed files with 39 additions and 39 deletions

View file

@ -1,8 +1,9 @@
package config
import (
"encoding/json"
"os"
"github.com/BurntSushi/toml"
)
type GeneralConfig struct {
@ -31,8 +32,39 @@ type Config struct {
General GeneralConfig `json:"general"`
}
func New() *Config {
return &Config{
func Load() Config {
configPath, err := os.UserConfigDir()
if err != nil {
panic(err)
}
configPath += "/discordo.toml"
c := Config{}
// If the configuration file does not exist, create and write the default configuration to the file.
if _, err = os.Stat(configPath); os.IsNotExist(err) {
f, err := os.Create(configPath)
if err != nil {
panic(err)
}
defer f.Close()
c = newDefaultConfig()
err = toml.NewEncoder(f).Encode(c)
if err != nil {
panic(err)
}
} else {
_, err = toml.DecodeFile(configPath, &c)
if err != nil {
panic(err)
}
}
return c
}
func newDefaultConfig() Config {
return Config{
General: GeneralConfig{
UserAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0",
FetchMessagesLimit: 50,
@ -54,37 +86,3 @@ func New() *Config {
},
}
}
func (c *Config) Load() {
configPath, err := os.UserConfigDir()
if err != nil {
panic(err)
}
configPath += "/discordo.json"
f, err := os.OpenFile(configPath, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
panic(err)
}
fi, err := f.Stat()
if err != nil {
panic(err)
}
// If the size of the file is zero (the file is empty), write the default configuration to the file.
if fi.Size() == 0 {
e := json.NewEncoder(f)
e.SetIndent("", "\t")
c = New()
err = e.Encode(c)
if err != nil {
panic(err)
}
} else {
err = json.NewDecoder(f).Decode(c)
if err != nil {
panic(err)
}
}
}

2
go.mod
View file

@ -3,7 +3,7 @@ module github.com/ayntgl/discordo
go 1.16
require (
github.com/BurntSushi/toml v0.4.1
github.com/BurntSushi/toml v1.0.0
github.com/atotto/clipboard v0.1.4
github.com/ayntgl/discordgo v0.23.3-0.20211016111152-3b0092664e53
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1

2
go.sum
View file

@ -40,6 +40,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=
github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=

View file

@ -11,7 +11,6 @@ import (
func main() {
app := ui.NewApp()
app.Config.Load()
token := os.Getenv("DISCORDO_TOKEN")
if token == "" {

View file

@ -33,6 +33,7 @@ func NewApp() *App {
MessageInputField: tview.NewInputField(),
Session: s,
Config: config.Load(),
SelectedMessage: -1,
}
}