diff --git a/config/mod.go b/config/mod.go index f9e158f..69ef5f7 100644 --- a/config/mod.go +++ b/config/mod.go @@ -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) - } - } -} diff --git a/go.mod b/go.mod index f957157..6d2751a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3681728..face8b8 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index c43e50e..8db129d 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( func main() { app := ui.NewApp() - app.Config.Load() token := os.Getenv("DISCORDO_TOKEN") if token == "" { diff --git a/ui/app.go b/ui/app.go index 0a97bb1..11267b1 100644 --- a/ui/app.go +++ b/ui/app.go @@ -33,6 +33,7 @@ func NewApp() *App { MessageInputField: tview.NewInputField(), Session: s, + Config: config.Load(), SelectedMessage: -1, } }