Skip to content

Commit

Permalink
feat: viper for config
Browse files Browse the repository at this point in the history
  • Loading branch information
Selyss committed Oct 20, 2023
1 parent 8dca2aa commit 095c397
Show file tree
Hide file tree
Showing 3 changed files with 526 additions and 22 deletions.
37 changes: 15 additions & 22 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
package main

import (
"github.com/spf13/viper"
"log"
"os"
"path"
)

func getHomeConfig() (string, error) {

config, err := os.UserHomeDir()
if err != nil {
return "", err
}

return path.Join(config, ".cued.json"), nil
type Config struct {
Topics []string
}

func getUserConfig() (string, error) {
func GetConfig() ([]string, error) {

config, err := os.UserConfigDir()
if err != nil {
return "", err
}
home, err := os.UserHomeDir()

return path.Join(config, "cued", "cued.json"), nil
}
viper.SetConfigName("cued")
viper.SetConfigType("yml")
viper.AddConfigPath(config)
viper.AddConfigPath(home)
viper.AddConfigPath(".")
err = viper.ReadInConfig()

func GetConfig() (string, error) {
config, err := getUserConfig()
if err != nil {
return "", err
log.Fatalf("Error while reading config file %s", err)
}

config, err = getHomeConfig()
if err != nil {
return "", err
}
return viper.GetStringSlice("topics"), nil

}
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,29 @@ go 1.18
require (
github.com/akamensky/argparse v1.4.0
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/viper v1.17.0
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 095c397

Please sign in to comment.