Skip to content

Commit

Permalink
Adhere to XDG's configuration specification (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
torrca authored Aug 3, 2023
1 parent fb31ac5 commit 14c5358
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/types/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const DefaultK3dInternalHostRecord = "host.k3d.internal"
const DefaultImageVolumeMountPath = "/k3d/images"

// DefaultConfigDirName defines the name of the config directory (where we'll e.g. put the kubeconfigs)
const DefaultConfigDirName = ".k3d" // should end up in $HOME/
const DefaultConfigDirName = ".config/k3d" // should end up in $XDG_CONFIG_HOME

// DefaultKubeconfigPrefix defines the default prefix for kubeconfig files
const DefaultKubeconfigPrefix = DefaultObjectNamePrefix + "-kubeconfig"
Expand Down
17 changes: 11 additions & 6 deletions pkg/util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ import (
"path"

homedir "github.com/mitchellh/go-homedir"

"github.com/k3d-io/k3d/v5/pkg/types"
)

// GetConfigDirOrCreate will return the base path of the k3d config directory or create it if it doesn't exist yet
// k3d's config directory will be $HOME/.k3d (Unix)
// k3d's config directory will be $XDG_CONFIG_HOME (Unix)
func GetConfigDirOrCreate() (string, error) {
// build the path
homeDir, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("failed to get user's home directory: %w", err)
configDir := os.Getenv("XDG_CONFIG_HOME")
if len(configDir) == 0 {
// build the path
homeDir, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("failed to get user's home directory: %w", err)
}
configDir = path.Join(homeDir, types.DefaultConfigDirName)
}
configDir := path.Join(homeDir, ".k3d")

// create directories if necessary
if err := createDirIfNotExists(configDir); err != nil {
Expand Down

0 comments on commit 14c5358

Please sign in to comment.