소스 검색

feat: add config flag to specify different path (#292)

Co-authored-by: ayn2op <119342035+ayn2op@users.noreply.github.com>
Ohkthx 3 년 전
부모
커밋
401c0635cf
5개의 변경된 파일45개의 추가작업 그리고 8개의 파일을 삭제
  1. 2 0
      README.md
  2. 37 4
      internal/config/config.go
  3. 4 2
      main.go
  4. 1 1
      man/discordo.1
  5. 1 1
      man/discordo.md

+ 2 - 0
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

+ 37 - 4
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 {
+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.")
+	}
+
+	// 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
+	}
+
+	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.
-	path = filepath.Join(path, Name)
-	err = os.MkdirAll(path, os.ModePerm)
+	err = os.MkdirAll(dir, os.ModePerm)
 	if err != nil {
 		return err
 	}
 
-	path = filepath.Join(path, "config.yml")
+	path = filepath.Join(dir, file)
 	_, err = os.Stat(path)
 	if os.IsNotExist(err) {
 		f, err := os.Create(path)

+ 4 - 2
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)
 	}
 

+ 1 - 1
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

+ 1 - 1
man/discordo.md

@@ -9,7 +9,7 @@ NAME
 SYNOPSIS
 --------
 
-`discordo` [`--token "TOKEN"`]
+`discordo` [`--token "TOKEN"`] [`--config "OPTIONAL/PATH/HERE.yml"`]
 
 DESCRIPTION
 -----------