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

feat: normalize Xdg achannarasappa#40 #273

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ groups:
* Symbols not on the watchlist that exists in `lots` will automatically be watched
* To add multiple (`quantity`, `unit_cost`) to the same `symbol`, write two `symbol` entries - see `ARKW` example above
* All properties in `.ticker.yaml` are optional
* `.ticker.yaml` can be set in user home directory, the current directory, or [XDG config home](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
* `.ticker.yaml` can be set in user home directory, the current directory, or [XDG config home](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) i.e `.config/ticker/ticker.yaml` or `${XDG_CONFIG_HOME}/ticker/ticker.yaml`

### Display Options

Expand Down
40 changes: 28 additions & 12 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/achannarasappa/ticker/v4/internal/cli/symbol"
Expand Down Expand Up @@ -187,23 +188,38 @@ func getConfigPath(fs afero.Fs, configPathOption string) (string, error) {
return configPathOption, nil
}

home, _ := homedir.Dir()
vc := viper.New()
vc.SetFs(fs)
vc.SetConfigType("yaml")
vc.AddConfigPath(filepath.Join(xdg.ConfigHome, "ticker"))
vc.SetConfigName("ticker")

v := viper.New()
v.SetFs(fs)
v.SetConfigType("yaml")
v.AddConfigPath(home)
v.AddConfigPath(".")
v.AddConfigPath(xdg.ConfigHome)
v.AddConfigPath(xdg.ConfigHome + "/ticker")
v.SetConfigName(".ticker")
err = v.ReadInConfig()
if err = vc.ReadInConfig(); err != nil {
var configFileNotFoundError *viper.ConfigFileNotFoundError

if !errors.As(err, &configFileNotFoundError) {
home, _ := homedir.Dir()

v := viper.New()
v.SetFs(fs)
v.SetConfigType("yaml")
v.AddConfigPath(home)
v.AddConfigPath(".")
v.SetConfigName(".ticker")

if err = v.ReadInConfig(); err != nil {
return "", fmt.Errorf("invalid config: %w", err)
}

return v.ConfigFileUsed(), nil

}

if err != nil {
return "", fmt.Errorf("invalid config: %w", err)

}

return v.ConfigFileUsed(), nil
return vc.ConfigFileUsed(), nil
}

func getRefreshInterval(optionsRefreshInterval int, configRefreshInterval int) int {
Expand Down
23 changes: 12 additions & 11 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ var _ = Describe("Cli", func() {
writeConfigFile(dep.Fs, c.InputConfigFileContents)
}
outputConfig, outputErr := cli.GetConfig(dep, c.InputConfigFilePath, c.InputOptions)
Expect(outputErr).To(c.AssertionErr)
outputCtx, outputErr := cli.GetContext(dep, outputConfig)
Expect(outputErr).To(c.AssertionErr)
Expect(outputCtx).To(c.AssertionCtx)
Expand Down Expand Up @@ -520,22 +521,22 @@ var _ = Describe("Cli", func() {
Expect(outputErr).To(BeNil())
})
})
When("there is a config file in the XDG config directory", func() {
XIt("should read the config file from disk", func() {
inputHome, _ := homedir.Dir()
inputConfigHome := inputHome + "/.config"
os.Setenv("XDG_CONFIG_HOME", inputConfigHome)

When("there is a config file in the XDG config default directory", func() {
It("should read the config file from disk", func() {
inputConfigHome, _ := homedir.Dir()
inputConfigHome += "/.config"
inputConfigPath := ""
depLocal.Fs.MkdirAll(inputConfigHome, 0755)
depLocal.Fs.Create(inputConfigHome + "/.ticker.yaml")
afero.WriteFile(depLocal.Fs, inputConfigHome+"/.ticker.yaml", []byte("watchlist:\n - ABNB"), 0644)
depLocal.Fs.MkdirAll(inputConfigHome+"/ticker", 0755)
configFile := inputConfigHome + "/ticker/ticker.yaml"
depLocal.Fs.Create(configFile)
afero.WriteFile(depLocal.Fs, configFile, []byte("watchlist:\n - AMD"), 0644)
outputConfig, outputErr := GetConfig(depLocal, inputConfigPath, cli.Options{})
os.Unsetenv("XDG_CONFIG_HOME")

Expect(outputConfig.Watchlist).To(Equal([]string{"ABNB"}))
Expect(outputConfig.Watchlist).To(Equal([]string{"AMD"}))
Expect(outputErr).To(BeNil())
})
})

})

When("there is an error reading the config file", func() {
Expand Down
Loading