Skip to content
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
17 changes: 4 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions pkg/lib/constants/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions pkg/lib/constants/consts_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading