Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support $XDG_CONFIG_HOME for config file location #504

Open
gtback opened this issue Aug 20, 2021 · 0 comments
Open

Support $XDG_CONFIG_HOME for config file location #504

gtback opened this issue Aug 20, 2021 · 0 comments

Comments

@gtback
Copy link
Member

gtback commented Aug 20, 2021

Overview

ecctl should be able to read a config file located at $XDG_CONFIG_HOME/ecctl/config.{json,yaml,toml,hcl} (by convention, $HOME/.config/ecctl/config.{json,yaml,toml,hcl}) in line with the XDG Base Directory Specification.

Possible Implementation

Caveat: I'm not a Golang developer.
It seem like Viper supports this, but not implicitly.

Adding viper.AddConfigPath("$XDG_CONFIG_HOME/ecctl") to func setupViper should do the trick:

diff --git a/cmd/root.go b/cmd/root.go
index 1a50581..d5cc47c 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -137,6 +137,7 @@ func setupViper(v *viper.Viper) {
        v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
        v.AutomaticEnv()
        v.AddConfigPath(ecctlHomePath)         // adding home directory as first search path
+       v.AddConfigPath("$XDG_CONFIG_HOME/ecctl")
        v.SetConfigName(v.GetString("config")) // name of config file (without extension)

        // If a config file is found, read it in.

A more thorough implementation might be to split up ecctlHomePath to different variables that could also be used in case the tool needs to store user data or cache data ($XDG_DATA_HOME and $XDG_CACHE_HOME, respectively), rather than making the assumption they'll all be in the same ~/.ecctl directory.

Testing

  • Test that if $XDG_CONFIG_HOME is defined, ecctl init writes the config file in that directory.
  • Test that if $XDG_CONFIG_HOME is defined and $XDG_CONFIG_HOME/ecctl/config.json exists, it is used for configuration values.
  • Test that if both $XDG_CONFIG_HOME/ecctl/config.json and $HOME/.ecctl/config.json exist, the latter takes priority (I think? It depends on the implementation and which is given higher precedence)
  • Test that if $XDG_CONFIG_HOME is defined and $XDG_CONFIG_HOME/ecctl/config.json doesn't exist, it falls back to $HOME/.ecctl/config.json if it exists, otherwise the missing ecctl config file message is diplayed.

Context

I try to keep my $HOME directory free of extraneous dotfiles/directories.

Your Environment

macOS (but any Linux/Unix variant will be similarly affected).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant