Skip to content

Commit

Permalink
config: use $XDG_CONFIG_HOME if set on macOS
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft committed Jul 29, 2024
1 parent a6ca4d2 commit 05fd577
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions config/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,51 @@ func (r *requiredDir) Dir() string {
var (
configBaseDir = requiredDir{
dir: func() (string, error) {
// colima home explicit config
dir := os.Getenv("COLIMA_HOME")
if _, err := os.Stat(dir); err == nil {
return dir, nil
}

dir, err := os.UserHomeDir()
// user home directory
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
dir = filepath.Join(dir, ".colima")
// colima's config directory based on home directory
dir = filepath.Join(homeDir, ".colima")
// validate existence of colima's config directory
_, err = os.Stat(dir)
// TODO: remove macOS when QEMU_SYSTEM_ARCH is handled properly upstream.
if err == nil || util.MacOS() {

// extra xdg config directory
xdgDir, xdg := os.LookupEnv("XDG_CONFIG_HOME")

if err == nil {
// ~/.colima is found but xdg dir is set
if xdg {
logrus.Warnln("found ~/.colima, ignoring $XDG_CONFIG_HOME...")
logrus.Warnln("delete ~/.colima to use $XDG_CONFIG_HOME as config directory")
logrus.Warnf("or run `mv ~/.colima \"%s\"`", filepath.Join(xdgDir, "colima"))
}
return dir, nil
} else {
// ~/.colima is missing and xdg dir is set
if xdg {
return filepath.Join(xdgDir, "colima"), nil
}
}
// else
dir = os.Getenv("XDG_CONFIG_HOME")
if dir != "" {
return filepath.Join(dir, "colima"), nil

// macOS users are accustomed to ~/.colima
if util.MacOS() {
return dir, nil
}
// else

// other environments fall back to user config directory
dir, err = os.UserConfigDir()
if err != nil {
return "", err
}

return filepath.Join(dir, "colima"), nil
},
}
Expand Down

0 comments on commit 05fd577

Please sign in to comment.