diff --git a/README.md b/README.md index e0898c0..0d4d49e 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ sudo mv ./discordo /usr/local/bin 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. +Optionally, you can specify an alternative configuration file location with the `config` command-line flag to the executable (eg: `--config OPTIONAL/PATH/HERE.yml`). + Similarly, a log file is created on first start-up at `$HOME/.cache/discordo/logs.txt` on Unix, `$HOME/Library/Caches/discordo/logs.txt` on Darwin, and `%LocalAppData%/discordo/logs.txt` on Windows. ## Disclaimer diff --git a/internal/config/config.go b/internal/config/config.go index 09fdb40..0f64b7e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,6 +1,7 @@ package config import ( + "errors" "os" "path/filepath" @@ -37,20 +38,52 @@ func defConfig() Config { } } -func Load() error { - path, err := os.UserConfigDir() - if err != nil { - return err +func getPath(optionalPath string) (string, error) { + // Trigger an error if config flag used but is empty. + if optionalPath == "" { + return "", errors.New("Optional path cannot be empty.") } - // Create the configuration directory if it does not exist already. - path = filepath.Join(path, Name) - err = os.MkdirAll(path, os.ModePerm) + // Use the path provided by flags. + if optionalPath != "none" { + return optionalPath, nil + } + + // Use the default for the OS. + path, err := os.UserConfigDir() if err != nil { - return err + return "", err + } + + path = filepath.Join(path, Name) + if err != nil { + return "", err } path = filepath.Join(path, "config.yml") + if err != nil { + return "", err + } + + return path, nil +} + +func Load(optionalPath string) error { + path, err := getPath(optionalPath) + if err != nil { + return err + } + + // Split the directory from the configuration file. + dir, file := filepath.Split(path) + + // Create the configuration directory if it does not exist already. + err = os.MkdirAll(dir, os.ModePerm) + if err != nil { + return err + } + + path = filepath.Join(dir, file) _, err = os.Stat(path) if os.IsNotExist(err) { f, err := os.Create(path) diff --git a/main.go b/main.go index fe3deba..69ae5f1 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,8 @@ import ( ) var ( - token string + token string + configPath string discordState *State @@ -24,6 +25,7 @@ var ( func init() { t, _ := keyring.Get(config.Name, "token") flag.StringVar(&token, "token", t, "The authentication token.") + flag.StringVar(&configPath, "config", "none", "Optional alternative configuration file.") path, err := os.UserCacheDir() if err != nil { @@ -49,7 +51,7 @@ func init() { func main() { flag.Parse() - if err := config.Load(); err != nil { + if err := config.Load(configPath); err != nil { log.Fatal(err) } diff --git a/man/discordo.1 b/man/discordo.1 index 7015573..d4404e5 100644 --- a/man/discordo.1 +++ b/man/discordo.1 @@ -6,7 +6,7 @@ .SH SYNOPSIS .PP -\fB\fCdiscordo\fR [\fB\fC--token "TOKEN"\fR] +\fB\fCdiscordo\fR [\fB\fC--token "TOKEN"\fR] [\fB\fC--config "OPTIONAL/PATH/HERE.yml"\fR] .SH DESCRIPTION .PP diff --git a/man/discordo.md b/man/discordo.md index 2b22a11..2eb788a 100644 --- a/man/discordo.md +++ b/man/discordo.md @@ -9,7 +9,7 @@ NAME SYNOPSIS -------- -`discordo` [`--token "TOKEN"`] +`discordo` [`--token "TOKEN"`] [`--config "OPTIONAL/PATH/HERE.yml"`] DESCRIPTION -----------