Skip to content

Commit

Permalink
add environmental variable
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanazamfirei authored and dlvhdr committed Jun 14, 2023
1 parent b2e9d3e commit b6b0443
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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
```
Expand All @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Expand Up @@ -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")

Expand Down
77 changes: 37 additions & 40 deletions config/parser.go
Expand Up @@ -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
Expand All @@ -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}
}

Expand Down
12 changes: 7 additions & 5 deletions docs/content/configuration/_index.md
Expand Up @@ -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`.

Expand Down
16 changes: 8 additions & 8 deletions docs/content/getting-started/usage.md
Expand Up @@ -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
```
Expand All @@ -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`.

Expand Down

0 comments on commit b6b0443

Please sign in to comment.