From 63b082be9c4308ae4732197a5007e6b00ca42444 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Thu, 21 Mar 2024 13:26:22 +0100 Subject: [PATCH] remove unused 'path' after loading config (#13) not used in the code Signed-off-by: Xavier Coulon --- pkg/configuration/configuration.go | 24 +++++++------- pkg/configuration/configuration_test.go | 43 +++---------------------- 2 files changed, 15 insertions(+), 52 deletions(-) diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index 376b7e2..8938952 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -25,34 +25,34 @@ type SandboxUserConfig struct { } // Load reads in config file and ENV variables if set. -func Load(term ioutils.Terminal) (SandboxUserConfig, string, error) { +func Load(term ioutils.Terminal) (SandboxUserConfig, error) { path := ConfigFileFlag if path == "" { // Find home directory. home, err := homedir.Dir() if err != nil { - return SandboxUserConfig{}, "", errs.Wrap(err, "unable to read home directory") + return SandboxUserConfig{}, errs.Wrap(err, "unable to read home directory") } path = filepath.Join(home, ".ksctl.yaml") if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { if _, err := os.Stat(filepath.Join(home, ".sandbox.yaml")); err != nil && !os.IsNotExist(err) { - return SandboxUserConfig{}, "", err + return SandboxUserConfig{}, err } else if err == nil { path = filepath.Join(home, ".sandbox.yaml") term.Println("The default location of ~/.sandbox.yaml file is deprecated. Rename it to ~/.ksctl.yaml") } } else if err != nil { - return SandboxUserConfig{}, "", err + return SandboxUserConfig{}, err } } info, err := os.Stat(path) if err != nil { - return SandboxUserConfig{}, "", errs.Wrapf(err, "unable to read the file '%s'", path) + return SandboxUserConfig{}, errs.Wrapf(err, "unable to read the file '%s'", path) } if info.IsDir() { - return SandboxUserConfig{}, "", fmt.Errorf("the '%s' is not file but a directory", path) + return SandboxUserConfig{}, fmt.Errorf("the '%s' is not file but a directory", path) } if Verbose { @@ -61,13 +61,13 @@ func Load(term ioutils.Terminal) (SandboxUserConfig, string, error) { bytes, err := os.ReadFile(path) if err != nil { - return SandboxUserConfig{}, "", err + return SandboxUserConfig{}, err } sandboxUserConfig := SandboxUserConfig{} if err := yaml.Unmarshal(bytes, &sandboxUserConfig); err != nil { - return SandboxUserConfig{}, "", err + return SandboxUserConfig{}, err } - return sandboxUserConfig, path, nil + return sandboxUserConfig, nil } const HostName = "host" @@ -110,7 +110,7 @@ type ClusterNamespaces map[string]string // LoadClusterAccessDefinition loads ClusterAccessDefinition object from the config file and checks that all required parameters are set func LoadClusterAccessDefinition(term ioutils.Terminal, clusterName string) (ClusterAccessDefinition, error) { - sandboxUserConfig, _, err := Load(term) + sandboxUserConfig, err := Load(term) if err != nil { return ClusterAccessDefinition{}, err } @@ -156,13 +156,12 @@ type ClusterConfig struct { ClusterName string Token string SandboxNamespace string - PathToConfigFile string } // LoadClusterConfig loads ClusterConfig object from the config file and checks that all required parameters are set // as well as the token for the given name func LoadClusterConfig(term ioutils.Terminal, clusterName string) (ClusterConfig, error) { - sandboxUserConfig, path, err := Load(term) + sandboxUserConfig, err := Load(term) if err != nil { return ClusterConfig{}, err } @@ -196,7 +195,6 @@ func LoadClusterConfig(term ioutils.Terminal, clusterName string) (ClusterConfig ClusterName: clusterName, Token: clusterDef.Token, SandboxNamespace: sandboxNamespace, - PathToConfigFile: path, }, nil } diff --git a/pkg/configuration/configuration_test.go b/pkg/configuration/configuration_test.go index 0caeb6e..6c3aa36 100644 --- a/pkg/configuration/configuration_test.go +++ b/pkg/configuration/configuration_test.go @@ -59,7 +59,6 @@ func TestLoadClusterConfig(t *testing.T) { assert.Equal(t, "cool-server.com", cfg.ServerName) assert.Len(t, cfg.AllClusterNames, 1) assert.Contains(t, cfg.AllClusterNames, utils.CamelCaseToKebabCase(clusterName)) - assert.True(t, strings.HasPrefix(cfg.PathToConfigFile, os.TempDir())) assert.Contains(t, term.Output(), fmt.Sprintf("Using config file: '%s'", configuration.ConfigFileFlag)) assert.Contains(t, term.Output(), fmt.Sprintf("Using '%s' configuration for '%s' cluster running at '%s' and in namespace '%s'", cfg.ClusterName, cfg.ServerName, cfg.ServerAPI, cfg.SandboxNamespace)) @@ -238,46 +237,13 @@ func TestLoad(t *testing.T) { configuration.Verbose = true // when - sandboxUserConfig, path, err := configuration.Load(term) + sandboxUserConfig, err := configuration.Load(term) // then require.NoError(t, err) expectedConfig := NewSandboxUserConfig(Host(), Member()) assert.Equal(t, expectedConfig, sandboxUserConfig) assert.Contains(t, term.Output(), "Using config file") - assert.True(t, strings.HasPrefix(path, os.TempDir())) - - t.Run("reload again the same", func(t *testing.T) { - // given - term := NewFakeTerminal() - - // when - sandboxUserConfig, theSamePath, err := configuration.Load(term) - - // then - require.NoError(t, err) - assert.Equal(t, expectedConfig, sandboxUserConfig) - assert.Contains(t, term.Output(), "Using config file") - assert.True(t, strings.HasPrefix(path, os.TempDir())) - assert.Equal(t, path, theSamePath) - }) - - t.Run("load a new one", func(t *testing.T) { - // given - term := NewFakeTerminal() - SetFileConfig(t, Host(), Member()) - - // when - sandboxUserConfig, newPath, err := configuration.Load(term) - - // then - require.NoError(t, err) - expectedConfig := NewSandboxUserConfig(Host(), Member()) - assert.Equal(t, expectedConfig, sandboxUserConfig) - assert.Contains(t, term.Output(), "Using config file") - assert.True(t, strings.HasPrefix(path, os.TempDir())) - assert.NotEqual(t, path, newPath) - }) }) t.Run("without verbose messages", func(t *testing.T) { @@ -287,14 +253,13 @@ func TestLoad(t *testing.T) { configuration.Verbose = false // when - sandboxUserConfig, path, err := configuration.Load(term) + sandboxUserConfig, err := configuration.Load(term) // then require.NoError(t, err) expectedConfig := NewSandboxUserConfig(Host(), Member()) assert.Equal(t, expectedConfig, sandboxUserConfig) assert.NotContains(t, term.Output(), "Using config file") - assert.True(t, strings.HasPrefix(path, os.TempDir())) }) } @@ -306,7 +271,7 @@ func TestLoadFails(t *testing.T) { configuration.ConfigFileFlag = "/tmp/should-not-exist.yaml" // when - _, _, err := configuration.Load(term) + _, err := configuration.Load(term) // then require.Error(t, err) @@ -318,7 +283,7 @@ func TestLoadFails(t *testing.T) { configuration.ConfigFileFlag = os.TempDir() // when - _, _, err := configuration.Load(term) + _, err := configuration.Load(term) // then require.Error(t, err)