feat: add config flag to specify different path (#292)
Co-authored-by: ayn2op <119342035+ayn2op@users.noreply.github.com>
This commit is contained in:
parent
a4152d4928
commit
401c0635cf
5 changed files with 49 additions and 12 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
6
main.go
6
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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ NAME
|
|||
SYNOPSIS
|
||||
--------
|
||||
|
||||
`discordo` [`--token "TOKEN"`]
|
||||
`discordo` [`--token "TOKEN"`] [`--config "OPTIONAL/PATH/HERE.yml"`]
|
||||
|
||||
DESCRIPTION
|
||||
-----------
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue