elastic / elastic/ecctl

Support $XDG_CONFIG_HOME for config file location

Open
#504 0 comments 5 reactions 0 assignees View on GitHub
Team:Delivery
Dominant language
Go
Stars
58
Forks
61
PR merge metrics
No merged PRs in 30d

Description

## 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](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).

## Possible Implementation

Caveat: I'm not a Golang developer.
It seem like Viper [supports this](https://github.com/spf13/viper/issues/1048), but not [implicitly](https://github.com/spf13/viper/issues/281).

Adding `viper.AddConfigPath("$XDG_CONFIG_HOME/ecctl")` to `func setupViper` [should do the trick](https://github.com/elastic/ecctl/blob/c1b5789/cmd/root.go#L135):

```diff
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).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.