diff --git a/cmd/root.go b/cmd/root.go index 33346a65d..cf3062b04 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -188,20 +188,11 @@ func getConfigHome(opts *rootOptions) (string, error) { return absHome, nil } - envHome := os.Getenv(constants.KitopsHomeEnvVar) - if envHome != "" { - output.Debugf("Using config directory from environment variable: %s", envHome) - absHome, err := filepath.Abs(envHome) - if err != nil { - return "", fmt.Errorf("failed to get absolute path for %s: %w", constants.KitopsHomeEnvVar, err) - } - return absHome, nil - } - - defaultHome, err := constants.DefaultConfigPath() + configHome, err := constants.ConfigPath() if err != nil { return "", err } - output.Debugf("Using default config directory: %s", defaultHome) - return defaultHome, nil + + output.Debugf("Using config directory: %s", configHome) + return configHome, nil } diff --git a/pkg/lib/constants/consts.go b/pkg/lib/constants/consts.go index 85b2b272c..30e8fad7f 100644 --- a/pkg/lib/constants/consts.go +++ b/pkg/lib/constants/consts.go @@ -81,6 +81,21 @@ func IsDefaultKitfileName(filename string) bool { return false } +// ConfigPath returns the configured root storage directory. KITOPS_HOME takes +// precedence over the platform-specific default path. +func ConfigPath() (string, error) { + envHome := os.Getenv(KitopsHomeEnvVar) + if envHome != "" { + absHome, err := filepath.Abs(envHome) + if err != nil { + return "", fmt.Errorf("failed to get absolute path for %s: %w", KitopsHomeEnvVar, err) + } + return absHome, nil + } + + return DefaultConfigPath() +} + // DefaultConfigPath returns the default configuration and cache directory for the CLI. // This is platform-dependent, using // - $XDG_DATA_HOME/kitops on Linux, with fall back to $HOME/.local/share/kitops diff --git a/pkg/lib/constants/consts_test.go b/pkg/lib/constants/consts_test.go new file mode 100644 index 000000000..b797e36d6 --- /dev/null +++ b/pkg/lib/constants/consts_test.go @@ -0,0 +1,47 @@ +// Copyright 2024 The KitOps Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// SPDX-License-Identifier: Apache-2.0 + +package constants + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestConfigPathUsesKitopsHome(t *testing.T) { + t.Setenv(KitopsHomeEnvVar, "custom-kitops-home") + + configPath, err := ConfigPath() + require.NoError(t, err) + + expected, err := filepath.Abs("custom-kitops-home") + require.NoError(t, err) + assert.Equal(t, expected, configPath) +} + +func TestConfigPathFallsBackToDefaultConfigPath(t *testing.T) { + t.Setenv(KitopsHomeEnvVar, "") + + configPath, err := ConfigPath() + require.NoError(t, err) + + defaultPath, err := DefaultConfigPath() + require.NoError(t, err) + assert.Equal(t, defaultPath, configPath) +}