Skip to content

Commit

Permalink
feat: normalize Xdg achannarasappa#40
Browse files Browse the repository at this point in the history
  • Loading branch information
tutilus committed Jul 6, 2024
1 parent d7d0b2a commit b421d11
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
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: 27 additions & 13 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,36 @@ 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")

if err = vc.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
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)
}

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()
return v.ConfigFileUsed(), nil

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

return "", fmt.Errorf("invalid config: %w", err)
}
}

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

func getRefreshInterval(optionsRefreshInterval int, configRefreshInterval int) int {
Expand Down
15 changes: 9 additions & 6 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,24 @@ 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"
It("should read the config file from disk", func() {
inputConfigHome, _ := homedir.Dir()
inputConfigHome += "/.config/ticker"
os.Setenv("XDG_CONFIG_HOME", inputConfigHome)
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.Create(inputConfigHome + "/ticker.yaml")
afero.WriteFile(depLocal.Fs, inputConfigHome+"/ticker.yaml", []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

0 comments on commit b421d11

Please sign in to comment.