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

new config searching path #79

Merged
merged 12 commits into from
Oct 14, 2024
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ one of these locations:
more details [here](https://en.opensuse.org/openSUSE:Packaging_UsrEtc).
1. `/etc/kuberlr.conf`
1. `$HOME/.kuberlr/kuberlr.conf`
1. `$KUBERLR_CFG`

The configuration files are read in the order written above and merged together.
Configuration files can override the values defined by the previous ones, or
Expand Down
7 changes: 2 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"os"
"path/filepath"

"github.com/spf13/viper"

Expand All @@ -21,7 +20,7 @@ type Cfg struct {
// directories.
func NewCfg() *Cfg {
return &Cfg{
Paths: configPaths,
Paths: configFiles,
}
}

Expand Down Expand Up @@ -58,9 +57,7 @@ func (c *Cfg) GetKubeMirrorURL() (string, error) {
return v.GetString("KubeMirrorUrl"), nil
}

func mergeConfig(v *viper.Viper, extraConfigPath string) error {
cfgFile := filepath.Join(extraConfigPath, "kuberlr.conf")

func mergeConfig(v *viper.Viper, cfgFile string) error {
_, err := os.Stat(cfgFile)
if err != nil {
if os.IsNotExist(err) {
Expand Down
19 changes: 16 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func TestOnlySystemConfigExists(t *testing.T) {
}

c := Cfg{
Paths: []string{td.FakeUsrEtc, td.FakeEtc, td.FakeHome},
Paths: []string{
filepath.Join(td.FakeUsrEtc, "kuberlr.conf"),
filepath.Join(td.FakeEtc, "kuberlr.conf"),
filepath.Join(td.FakeHome, "kuberlr.conf"),
},
}

v, err := c.Load()
Expand Down Expand Up @@ -86,7 +90,11 @@ func TestHomeConfigOverridesSystemOne(t *testing.T) {
}

c := Cfg{
Paths: []string{td.FakeUsrEtc, td.FakeEtc, td.FakeHome},
Paths: []string{
filepath.Join(td.FakeUsrEtc, "kuberlr.conf"),
filepath.Join(td.FakeEtc, "kuberlr.conf"),
filepath.Join(td.FakeHome, "kuberlr.conf"),
},
}

v, err := c.Load()
Expand Down Expand Up @@ -132,7 +140,12 @@ AllowDownload = true
}

c := Cfg{
Paths: []string{td.FakeUsrEtc, td.FakeEtc, td.FakeHome},
Paths: []string{
filepath.Join(td.FakeUsrEtc, "kuberlr.conf"),
filepath.Join(td.FakeEtc, "kuberlr.conf"),
filepath.Join(td.FakeHome, "kuberlr.conf"),
os.Getenv("__FAKE_ENV__"),
},
}

v, err := c.Load()
Expand Down
10 changes: 6 additions & 4 deletions internal/config/paths_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package config

import (
"os"
"path/filepath"

"github.com/flavio/kuberlr/internal/common"
)

var configPaths = []string{ //nolint: gochecknoglobals // arrays cannot be go constants
"/usr/etc/",
"/etc/",
filepath.Join(common.HomeDir(), ".kuberlr"),
var configFiles = []string{ //nolint: gochecknoglobals // arrays cannot be go constants
"/usr/etc/kuberlr.conf",
"/etc/kuberlr.conf",
filepath.Join(common.HomeDir(), ".kuberlr", "kuberlr.conf"),
os.Getenv("KUBERLR_CFG"),
}
12 changes: 7 additions & 5 deletions internal/config/paths_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package config

import (
"github.com/flavio/kuberlr/internal/common"
"os"
"path/filepath"

"github.com/flavio/kuberlr/internal/common"
)

var configPaths = []string{
filepath.Join(os.Getenv("APPDATA"), "kuberlr"),
filepath.Join(os.Getenv("PROGRAMDATA"), "kuberlr"),
filepath.Join(common.HomeDir(), ".kuberlr"),
var configFiles = []string{
filepath.Join(os.Getenv("APPDATA"), "kuberlr", "kuberlr.conf"),
filepath.Join(os.Getenv("PROGRAMDATA"), "kuberlr", "kuberlr.conf"),
filepath.Join(common.HomeDir(), ".kuberlr", "kuberlr.conf"),
os.Getenv("KUBERLR_CFG"),
}
Loading