From 63bece879db2cf520a7ece3d9e2cd1c96d0f4b72 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Thu, 22 Dec 2022 00:41:20 +0530 Subject: [PATCH] Update README with new location --- README.md | 4 ++-- config/config.go | 22 ++++++++++++++++++---- main.go | 10 ++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 403223d..9f97826 100644 --- a/README.md +++ b/README.md @@ -72,9 +72,9 @@ sudo mv ./discordo /usr/local/bin ### Configuration -A default configuration file is created on first start-up at `$HOME/.config/discordo.yml` on Unix, `$HOME/Library/Application Support/discordo.yml` on Darwin, and `%AppData%/discordo.yml` on Windows. The configuration file path can be customized using the `config` command-line flag (eg: `--config ~/myconfigs/discordo.yml`). +A default configuration file is created on first start-up at `$HOME/.config/discordo/config.yml` on Unix, `$HOME/Library/Application Support/discordo/config.yml` on Darwin, and `%AppData%/discordo/config.yml` on Windows. -Similarly, a log file is created on first start-up at `$HOME/.cache/discordo.log` on Unix, `$HOME/Library/Caches/discordo.log` on Darwin, and `%LocalAppData%/discordo.yml` on Windows. The log file path can be customized using the `log` command-line flag. +Similarly, a log file is created on first start-up at `$HOME/.cache/discordo/log.txt` on Unix, `$HOME/Library/Caches/discordo/log.txt` on Darwin, and `%LocalAppData%/discordo/log.txt` on Windows. ## Disclaimer diff --git a/config/config.go b/config/config.go index ed1b865..792a334 100644 --- a/config/config.go +++ b/config/config.go @@ -87,17 +87,19 @@ func New() *Config { } func (cfg *Config) Load() error { - path, _ := os.UserConfigDir() + path := DirPath() // Create the configuration directory if it does not exist already. - path = filepath.Join(path, Name) err := os.MkdirAll(path, os.ModePerm) if err != nil { return err } - path = filepath.Join(path, "config.yml") // Open the configuration file with create and read-write flag. - f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, os.ModePerm) + f, err := os.OpenFile( + filepath.Join(path, "config.yml"), + os.O_CREATE|os.O_RDWR, + os.ModePerm, + ) if err != nil { return err } @@ -115,3 +117,15 @@ func (cfg *Config) Load() error { return yaml.NewDecoder(f).Decode(&cfg) } + +// DirPath returns the path of the configuration directory. +func DirPath() string { + path, _ := os.UserConfigDir() + return filepath.Join(path, Name) +} + +// LogDirPath returns the path of the log directory. +func LogDirPath() string { + path, _ := os.UserCacheDir() + return filepath.Join(path, Name) +} diff --git a/main.go b/main.go index 098bce4..7e7c5a8 100644 --- a/main.go +++ b/main.go @@ -16,15 +16,17 @@ var tokenFlag string func init() { flag.StringVar(&tokenFlag, "token", "", "The authentication token.") - path, _ := os.UserCacheDir() - path = filepath.Join(path, config.Name) + path := config.LogDirPath() err := os.MkdirAll(path, os.ModePerm) if err != nil { log.Fatal(err) } - path = filepath.Join(path, "logs.txt") - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm) + f, err := os.OpenFile( + filepath.Join(path, "logs.txt"), + os.O_CREATE|os.O_WRONLY, + os.ModePerm, + ) if err != nil { log.Fatal(err) }