Skip to content

Commit

Permalink
remove unused 'path' after loading config (#13)
Browse files Browse the repository at this point in the history
not used in the code

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Mar 21, 2024
1 parent 650c3ec commit 63b082b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 52 deletions.
24 changes: 11 additions & 13 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -196,7 +195,6 @@ func LoadClusterConfig(term ioutils.Terminal, clusterName string) (ClusterConfig
ClusterName: clusterName,
Token: clusterDef.Token,
SandboxNamespace: sandboxNamespace,
PathToConfigFile: path,
}, nil
}

Expand Down
43 changes: 4 additions & 39 deletions pkg/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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) {
Expand All @@ -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()))
})

}
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 63b082b

Please sign in to comment.