From 8eaaa4b25646d09abd205411700a8595bcf90be0 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Tue, 19 Mar 2024 16:56:45 +0800 Subject: [PATCH 01/12] add config path --- internal/config/config.go | 17 +++++++++++++++++ internal/config/paths_unix.go | 1 + internal/config/paths_windows.go | 1 + 3 files changed, 19 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index 5d86e57..9e2819f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,6 +11,20 @@ import ( const DefaultTimeout = 5 +func ThisExecutableDir() string { + execPath, err := os.Executable() + if err != nil { + return nil + } + + execPath, err := filepath.EvalSymlinks(execPath) + if err != nil { + return nil + } + + return filepath.Dir(execPath) +} + // Cfg is used to retrieve the configuration of kuberlr. type Cfg struct { Paths []string @@ -59,6 +73,9 @@ func (c *Cfg) GetKubeMirrorURL() (string, error) { } func mergeConfig(v *viper.Viper, extraConfigPath string) error { + if extraConfigPath == nil { + return nil + } cfgFile := filepath.Join(extraConfigPath, "kuberlr.conf") _, err := os.Stat(cfgFile) diff --git a/internal/config/paths_unix.go b/internal/config/paths_unix.go index c16a864..07855e4 100755 --- a/internal/config/paths_unix.go +++ b/internal/config/paths_unix.go @@ -12,5 +12,6 @@ import ( var configPaths = []string{ //nolint: gochecknoglobals // arrays cannot be go constants "/usr/etc/", "/etc/", + ThisExecutableDir(), filepath.Join(common.HomeDir(), ".kuberlr"), } diff --git a/internal/config/paths_windows.go b/internal/config/paths_windows.go index a71e89a..1de429c 100755 --- a/internal/config/paths_windows.go +++ b/internal/config/paths_windows.go @@ -12,5 +12,6 @@ import ( var configPaths = []string{ filepath.Join(os.Getenv("APPDATA"), "kuberlr"), filepath.Join(os.Getenv("PROGRAMDATA"), "kuberlr"), + ThisExecutableDir(), filepath.Join(common.HomeDir(), ".kuberlr"), } From de7f8c3214c0f8febd21bfc6fd32e5421be4d827 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Tue, 19 Mar 2024 18:07:43 +0800 Subject: [PATCH 02/12] fix ThisExecutableDir() --- internal/config/config.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 9e2819f..0c86165 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -14,12 +14,12 @@ const DefaultTimeout = 5 func ThisExecutableDir() string { execPath, err := os.Executable() if err != nil { - return nil + return "" } - execPath, err := filepath.EvalSymlinks(execPath) + execPath, err = filepath.EvalSymlinks(execPath) if err != nil { - return nil + return "" } return filepath.Dir(execPath) @@ -73,7 +73,7 @@ func (c *Cfg) GetKubeMirrorURL() (string, error) { } func mergeConfig(v *viper.Viper, extraConfigPath string) error { - if extraConfigPath == nil { + if len(s) == 0 { return nil } cfgFile := filepath.Join(extraConfigPath, "kuberlr.conf") From 81fdea7a88f4b7b908b7d62dc63f2c1e419fd6d5 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Tue, 19 Mar 2024 20:03:49 +0800 Subject: [PATCH 03/12] fix typo --- internal/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/config/config.go b/internal/config/config.go index 0c86165..053297c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -73,7 +73,7 @@ func (c *Cfg) GetKubeMirrorURL() (string, error) { } func mergeConfig(v *viper.Viper, extraConfigPath string) error { - if len(s) == 0 { + if len(extraConfigPath) == 0 { return nil } cfgFile := filepath.Join(extraConfigPath, "kuberlr.conf") From e70b3d15756adff138d5eb7267b1eaac26d87d3c Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Fri, 4 Oct 2024 18:26:33 +0800 Subject: [PATCH 04/12] load config from $KUBERLR_CFG --- internal/config/config.go | 19 ++----------------- internal/config/paths_unix.go | 8 ++++---- internal/config/paths_windows.go | 8 ++++---- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 053297c..984789f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,20 +11,6 @@ import ( const DefaultTimeout = 5 -func ThisExecutableDir() string { - execPath, err := os.Executable() - if err != nil { - return "" - } - - execPath, err = filepath.EvalSymlinks(execPath) - if err != nil { - return "" - } - - return filepath.Dir(execPath) -} - // Cfg is used to retrieve the configuration of kuberlr. type Cfg struct { Paths []string @@ -76,16 +62,15 @@ func mergeConfig(v *viper.Viper, extraConfigPath string) error { if len(extraConfigPath) == 0 { return nil } - cfgFile := filepath.Join(extraConfigPath, "kuberlr.conf") - _, err := os.Stat(cfgFile) + _, err := os.Stat(extraConfigPath) if err != nil { if os.IsNotExist(err) { return nil } return err } - v.SetConfigFile(cfgFile) + v.SetConfigFile(extraConfigPath) return v.MergeInConfig() } diff --git a/internal/config/paths_unix.go b/internal/config/paths_unix.go index 07855e4..02f5ca3 100755 --- a/internal/config/paths_unix.go +++ b/internal/config/paths_unix.go @@ -10,8 +10,8 @@ import ( ) var configPaths = []string{ //nolint: gochecknoglobals // arrays cannot be go constants - "/usr/etc/", - "/etc/", - ThisExecutableDir(), - filepath.Join(common.HomeDir(), ".kuberlr"), + "/usr/etc/kuberlr.conf", + "/etc/kuberlr.conf", + filepath.Join(common.HomeDir(), ".kuberlr", "kuberlr.conf"), + os.Getenv("KUBERLR_CFG"); } diff --git a/internal/config/paths_windows.go b/internal/config/paths_windows.go index 1de429c..b56a06f 100755 --- a/internal/config/paths_windows.go +++ b/internal/config/paths_windows.go @@ -10,8 +10,8 @@ import ( ) var configPaths = []string{ - filepath.Join(os.Getenv("APPDATA"), "kuberlr"), - filepath.Join(os.Getenv("PROGRAMDATA"), "kuberlr"), - ThisExecutableDir(), - filepath.Join(common.HomeDir(), ".kuberlr"), + 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"), } From 9442d7ff34a7db85e9d5622ab6d8018010f0f9fa Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Fri, 4 Oct 2024 18:30:34 +0800 Subject: [PATCH 05/12] fix typo --- internal/config/paths_unix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/config/paths_unix.go b/internal/config/paths_unix.go index 02f5ca3..1d7ddee 100755 --- a/internal/config/paths_unix.go +++ b/internal/config/paths_unix.go @@ -13,5 +13,5 @@ var configPaths = []string{ //nolint: gochecknoglobals // arrays cannot be go co "/usr/etc/kuberlr.conf", "/etc/kuberlr.conf", filepath.Join(common.HomeDir(), ".kuberlr", "kuberlr.conf"), - os.Getenv("KUBERLR_CFG"); + os.Getenv("KUBERLR_CFG"), } From 52d26c0ea0c698044054cd6004b7e92b6f6bc904 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Fri, 4 Oct 2024 18:34:35 +0800 Subject: [PATCH 06/12] import os --- internal/config/paths_unix.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/config/paths_unix.go b/internal/config/paths_unix.go index 1d7ddee..a9e5d2c 100755 --- a/internal/config/paths_unix.go +++ b/internal/config/paths_unix.go @@ -4,9 +4,9 @@ package config import ( - "path/filepath" - "github.com/flavio/kuberlr/internal/common" + "os" + "path/filepath" ) var configPaths = []string{ //nolint: gochecknoglobals // arrays cannot be go constants From 39cd2c049802d9212b2d417495e570acf3d86918 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Fri, 4 Oct 2024 18:36:08 +0800 Subject: [PATCH 07/12] remove unused import --- internal/config/config.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/config/config.go b/internal/config/config.go index 984789f..45d88ae 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,7 +2,6 @@ package config import ( "os" - "path/filepath" "github.com/spf13/viper" From 466de0673dde296e5b8a2409e003d18ca8f96778 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Sat, 5 Oct 2024 02:24:18 +0800 Subject: [PATCH 08/12] fix config_test --- internal/config/config_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 3df4de0..30d273d 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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() @@ -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() From e24fdc7cd833a1dfa2aaa75748b32f5e936b849b Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Sat, 5 Oct 2024 02:29:53 +0800 Subject: [PATCH 09/12] add missing comma --- internal/config/config_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 30d273d..d0e57cc 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -60,7 +60,7 @@ func TestOnlySystemConfigExists(t *testing.T) { Paths: []string{ filepath.Join(td.FakeUsrEtc, "kuberlr.conf"), filepath.Join(td.FakeEtc, "kuberlr.conf"), - filepath.Join(td.FakeHome, "kuberlr.conf") + filepath.Join(td.FakeHome, "kuberlr.conf"), }, } @@ -93,7 +93,7 @@ func TestHomeConfigOverridesSystemOne(t *testing.T) { Paths: []string{ filepath.Join(td.FakeUsrEtc, "kuberlr.conf"), filepath.Join(td.FakeEtc, "kuberlr.conf"), - filepath.Join(td.FakeHome, "kuberlr.conf") + filepath.Join(td.FakeHome, "kuberlr.conf"), }, } @@ -140,7 +140,11 @@ 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"), + }, } v, err := c.Load() From 12e48a21d89a88b5bb9ca22716783c2b18379a34 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Sat, 5 Oct 2024 02:50:36 +0800 Subject: [PATCH 10/12] fix imports style --- internal/config/paths_unix.go | 3 ++- internal/config/paths_windows.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/config/paths_unix.go b/internal/config/paths_unix.go index a9e5d2c..67435d5 100755 --- a/internal/config/paths_unix.go +++ b/internal/config/paths_unix.go @@ -4,9 +4,10 @@ package config import ( - "github.com/flavio/kuberlr/internal/common" "os" "path/filepath" + + "github.com/flavio/kuberlr/internal/common" ) var configPaths = []string{ //nolint: gochecknoglobals // arrays cannot be go constants diff --git a/internal/config/paths_windows.go b/internal/config/paths_windows.go index b56a06f..e88c525 100755 --- a/internal/config/paths_windows.go +++ b/internal/config/paths_windows.go @@ -4,9 +4,10 @@ package config import ( - "github.com/flavio/kuberlr/internal/common" "os" "path/filepath" + + "github.com/flavio/kuberlr/internal/common" ) var configPaths = []string{ From 88ba34e3a5ad4acf47a4e97cd77f19f0e97ce8a5 Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Mon, 7 Oct 2024 15:38:49 +0800 Subject: [PATCH 11/12] fix issues from comments --- README.md | 1 + internal/config/config.go | 12 ++++-------- internal/config/paths_unix.go | 2 +- internal/config/paths_windows.go | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 30ed344..442955c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/internal/config/config.go b/internal/config/config.go index 45d88ae..85a62e8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,7 +20,7 @@ type Cfg struct { // directories. func NewCfg() *Cfg { return &Cfg{ - Paths: configPaths, + Paths: configFiles, } } @@ -57,19 +57,15 @@ func (c *Cfg) GetKubeMirrorURL() (string, error) { return v.GetString("KubeMirrorUrl"), nil } -func mergeConfig(v *viper.Viper, extraConfigPath string) error { - if len(extraConfigPath) == 0 { - return nil - } - - _, err := os.Stat(extraConfigPath) +func mergeConfig(v *viper.Viper, cfgFile string) error { + _, err := os.Stat(cfgFile) if err != nil { if os.IsNotExist(err) { return nil } return err } - v.SetConfigFile(extraConfigPath) + v.SetConfigFile(cfgFile) return v.MergeInConfig() } diff --git a/internal/config/paths_unix.go b/internal/config/paths_unix.go index 67435d5..8d990d2 100755 --- a/internal/config/paths_unix.go +++ b/internal/config/paths_unix.go @@ -10,7 +10,7 @@ import ( "github.com/flavio/kuberlr/internal/common" ) -var configPaths = []string{ //nolint: gochecknoglobals // arrays cannot be go constants +var configFiles = []string{ //nolint: gochecknoglobals // arrays cannot be go constants "/usr/etc/kuberlr.conf", "/etc/kuberlr.conf", filepath.Join(common.HomeDir(), ".kuberlr", "kuberlr.conf"), diff --git a/internal/config/paths_windows.go b/internal/config/paths_windows.go index e88c525..24114db 100755 --- a/internal/config/paths_windows.go +++ b/internal/config/paths_windows.go @@ -10,7 +10,7 @@ import ( "github.com/flavio/kuberlr/internal/common" ) -var configPaths = []string{ +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"), From e719abbe1f52259c466e088f45f1ac1e4e92dfea Mon Sep 17 00:00:00 2001 From: "James Z.M. Gao" Date: Mon, 7 Oct 2024 15:51:08 +0800 Subject: [PATCH 12/12] add test when KUBERLR_CFG is unset --- internal/config/config_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index d0e57cc..0f8163e 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -144,6 +144,7 @@ AllowDownload = true filepath.Join(td.FakeUsrEtc, "kuberlr.conf"), filepath.Join(td.FakeEtc, "kuberlr.conf"), filepath.Join(td.FakeHome, "kuberlr.conf"), + os.Getenv("__FAKE_ENV__"), }, }