From b6b04439a9e53419d05499409d2f4a65f1210bc6 Mon Sep 17 00:00:00 2001 From: Razvan Azamfirei Date: Wed, 31 May 2023 08:06:40 -0400 Subject: [PATCH] add environmental variable --- README.md | 4 +- cmd/root.go | 2 +- config/parser.go | 77 +++++++++++++-------------- docs/content/configuration/_index.md | 12 +++-- docs/content/getting-started/usage.md | 16 +++--- 5 files changed, 55 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index eea594c..3aa548a 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Usage: gh dash [flags] Flags: - -c, --config string use this configuration file (default is $XDG_CONFIG_HOME/gh-dash/config.yml) + -c, --config string use this configuration file (default is $GH_DASH_CONFIG, or if not set, $XDG_CONFIG_HOME/gh-dash/config.yml) --debug passing this flag will allow writing debug output to debug.log -h, --help help for gh-dash ``` @@ -91,7 +91,7 @@ A section is defined by a: - title - shown in the TUI - filters - how the repo's PRs should be filtered - these are plain [github filters](https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests) -All configuration is provided within a `config.yml` file under the extension's directory (either `$XDG_CONFIG_HOME/gh-dash` or `~/.config/gh-dash/` or your OS config dir) +All configuration is provided within a `config.yml` file under the extension's directory (either `$XDG_CONFIG_HOME/gh-dash` or `~/.config/gh-dash/` or your OS config dir) or `$GH_DASH_CONFIG`. An example `config.yml` file contains: diff --git a/cmd/root.go b/cmd/root.go index f7a55f4..763d6b7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -89,7 +89,7 @@ func init() { "config", "c", "", - "use this configuration file (default is $XDG_CONFIG_HOME/gh-dash/config.yml)", + "use this configuration file (default is $GH_DASH_CONFIG, or if not set, \n$XDG_CONFIG_HOME/gh-dash/config.yml)", ) rootCmd.MarkFlagFilename("config", "yaml", "yml") diff --git a/config/parser.go b/config/parser.go index ec05249..8f5fe4d 100644 --- a/config/parser.go +++ b/config/parser.go @@ -300,8 +300,6 @@ func (parser ConfigParser) createConfigFileIfMissing(configFilePath string) erro } func (parser ConfigParser) getExistingConfigFile() (*string, error) { - var err error - var dashConfigFile string homeDir, err := os.UserHomeDir() if err != nil { return nil, err @@ -312,63 +310,62 @@ func (parser ConfigParser) getExistingConfigFile() (*string, error) { xdgConfigDir = filepath.Join(homeDir, DEFAULT_XDG_CONFIG_DIRNAME) } - dashConfigFile = filepath.Join(xdgConfigDir, DashDir, ConfigYmlFileName) - if _, err := os.Stat(dashConfigFile); err == nil { - return &dashConfigFile, nil - } - - dashConfigFile = filepath.Join(xdgConfigDir, DashDir, ConfigYamlFileName) - if _, err := os.Stat(dashConfigFile); err == nil { - return &dashConfigFile, nil - } - userConfigDir, err := os.UserConfigDir() if err != nil { return nil, err } - dashConfigFile = filepath.Join(userConfigDir, DashDir, ConfigYmlFileName) - if _, err := os.Stat(dashConfigFile); err == nil { - return &dashConfigFile, nil + configPaths := []string{ + os.Getenv("GH_DASH_CONFIG"), // If GH_DASH_CONFIG is empty, the os.Stat call fails + filepath.Join(xdgConfigDir, DashDir, ConfigYmlFileName), + filepath.Join(xdgConfigDir, DashDir, ConfigYamlFileName), + filepath.Join(userConfigDir, DashDir, ConfigYmlFileName), + filepath.Join(userConfigDir, DashDir, ConfigYamlFileName), } - dashConfigFile = filepath.Join(userConfigDir, DashDir, ConfigYamlFileName) - if _, err := os.Stat(dashConfigFile); err == nil { - return &dashConfigFile, nil + // Check if each config file exists, return the first one that does + for _, configPath := range configPaths { + if configPath == "" { + continue // Skip checking if path is empty + } + if _, err := os.Stat(configPath); err == nil { + return &configPath, nil + } } return nil, nil } -func (parser ConfigParser) getDefaultConfigFileOrCreateIfMissing() (string, error) { - var err error - existingConfigFile, err := parser.getExistingConfigFile() - if err != nil { - return "", err - } - if existingConfigFile != nil { - return *existingConfigFile, nil - } +func (parser ConfigParser) getDefaultConfigFileOrCreateIfMissing() (string, error) { + var configFilePath string - configDir := os.Getenv("XDG_CONFIG_HOME") - if configDir == "" { - homeDir, err := os.UserHomeDir() - if err != nil { - return "", err + ghDashConfig := os.Getenv("GH_DASH_CONFIG") + if ghDashConfig != "" { + configFilePath = ghDashConfig + } else { + configDir := os.Getenv("XDG_CONFIG_HOME") + if configDir == "" { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + configDir = filepath.Join(homeDir, DEFAULT_XDG_CONFIG_DIRNAME) } - configDir = filepath.Join(homeDir, DEFAULT_XDG_CONFIG_DIRNAME) + + dashConfigDir := filepath.Join(configDir, DashDir) + configFilePath = filepath.Join(dashConfigDir, ConfigYmlFileName) } - dashConfigDir := filepath.Join(configDir, DashDir) - err = os.MkdirAll(dashConfigDir, os.ModePerm) - if err != nil { - return "", configError{parser: parser, configDir: configDir, err: err} + // Ensure directory exists before attempting to create file + configDir := filepath.Dir(configFilePath) + if _, err := os.Stat(configDir); os.IsNotExist(err) { + if err = os.MkdirAll(configDir, os.ModePerm); err != nil { + return "", configError{parser: parser, configDir: configDir, err: err} + } } - configFilePath := filepath.Join(dashConfigDir, ConfigYmlFileName) - err = parser.createConfigFileIfMissing(configFilePath) - if err != nil { + if err := parser.createConfigFileIfMissing(configFilePath); err != nil { return "", configError{parser: parser, configDir: configDir, err: err} } diff --git a/docs/content/configuration/_index.md b/docs/content/configuration/_index.md index 3ec8df4..43132c1 100644 --- a/docs/content/configuration/_index.md +++ b/docs/content/configuration/_index.md @@ -13,16 +13,18 @@ cascade: `gh-dash` has extensive configuration options. -You can use the default configuration file or use the [`--config`][01] flag to specify an alternate -configuration. +You can use the default configuration file, use the [`--config`][01] flag or +`$GH_DASH_CONFIG` to specify an alternate configuration. If you don't specify the `--config` flag, `gh-dash` uses the default configuration. If the default configuration file doesn't already exist, `gh-dash` creates it. The location of the default configuration file depends on your system: -1. If `$XDG_CONFIG_HOME` is a non-empty string, the default path is - `$XDG_CONFIG_HOME/gh-dash/config.yml`. -1. If `$XDG_CONFIG_HOME` isn't set, then: +1. If `$GH_DASH_CONFIG` is a non-empty string, `gh-dash` will use this file for + its configuration. +1. If `$GH_DASH_CONFIG` isn't set and `$XDG_CONFIG_HOME` is a non-empty string, + the default path is `$XDG_CONFIG_HOME/gh-dash/config.yml`. +1. If neither `$GH_DASH_CONFIG` or `$XDG_CONFIG_HOME` are set, then: - On Linux and macOS systems, the default path is `$HOME/gh-dash/config.yml`. - On Windows systems, the default path is `%USERPROFILE%\gh-dash\config.yml`. diff --git a/docs/content/getting-started/usage.md b/docs/content/getting-started/usage.md index 92c17d7..c7d9b2f 100644 --- a/docs/content/getting-started/usage.md +++ b/docs/content/getting-started/usage.md @@ -23,7 +23,7 @@ To use `gh-dash`, follow these steps after you've [installed it][01]: gh dash [flags] Flags: - -c, --config string use this configuration file (default is $XDG_CONFIG_HOME/gh-dash/config.yml) + -c, --config string use this configuration file (default is $GH_DASH_CONFIG, or if not set, $XDG_CONFIG_HOME/gh-dash/config.yml) --debug passing this flag will allow writing debug output to debug.log -h, --help help for gh-dash ``` @@ -41,15 +41,15 @@ gh dash --config path/to/configuration/file.yml | Aliases | Type | Default | | :------ | :----: | :------------------------------------ | -| `-c` | String | `$XDG_CONFIG_HOME/gh-dash-config.yml` | +| `-c` | String | `$GH_DASH_CONFIG` or if not set, `$XDG_CONFIG_HOME/gh-dash-config.yml` | -If you don't specify this flag, `gh-dash` uses the default configuration. If the default -configuration file doesn't already exist, `gh-dash` creates it. The location of the default -configuration file depends on your system: +If you don't specify this flag, `gh-dash` uses the default configuration. If the file doesn't exist, gh-dash will create it. The location of the default configuration file depends on your system: -1. If `$XDG_CONFIG_HOME` is a non-empty string, the default path is - `$XDG_CONFIG_HOME/gh-dash/config.yml`. -1. If `$XDG_CONFIG_HOME` isn't set, then: +1. If `$GH_DASH_CONFIG` is a non-empty string, `gh-dash` will use this file for + its configuration. +1. If `$GH_DASH_CONFIG` isn't set and `$XDG_CONFIG_HOME` is a non-empty string, + the default path is `$XDG_CONFIG_HOME/gh-dash/config.yml`. +1. If neither `$GH_DASH_CONFIG` or `$XDG_CONFIG_HOME` are set, then: - On Linux and macOS systems, the default path is `$HOME/gh-dash/config.yml`. - On Windows systems, the default path is `%USERPROFILE%\gh-dash\config.yml`.