Skip to content

Instantly share code, notes, and snippets.

@nwisemanII
Last active March 25, 2024 17:53
Show Gist options
  • Save nwisemanII/b3d75336b7dffd2fc77ba520eb0adb57 to your computer and use it in GitHub Desktop.
Save nwisemanII/b3d75336b7dffd2fc77ba520eb0adb57 to your computer and use it in GitHub Desktop.
// xdg_direnv.go
// Package xdg is a minimal implementation of the XDG specification.
//
// https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
package xdg
import (
"path/filepath"
)
// DataDir returns the data folder for the application
func DataDir(env map[string]string, programName string) string {
// READ an exact directory Location (Helps Windows processes not rely on XDG_DATA_HOME
if env["DIRENV_DATA_DIR"] != "" {
return filepath.Join(env["DIRENV_DATA_DIR"], programName)
} else if env["XDG_DATA_HOME"] != "" {
return filepath.Join(env["XDG_DATA_HOME"], programName)
} else if env["HOME"] != "" {
return filepath.Join(env["HOME"], ".local", "share", programName)
}
// In theory we could also read /etc/passwd and look for the home based on
// the process' UID
return ""
}
// ConfigDir returns the config folder for the application
//
// The XDG_CONFIG_DIRS case is not being handled
func ConfigDir(env map[string]string, programName string) string {
// READ an exact directory Location ( Helps Windows processes not rely on XDG_CONFIG_HOME )
if env["DIRENV_CONFIG_DIR"] != "" {
return filepath.Join(env["DIRENV_CONFIG_DIR"])
} else if env["XDG_CONFIG_HOME"] != "" {
return filepath.Join(env["XDG_CONFIG_HOME"], programName)
} else if env["HOME"] != "" {
return filepath.Join(env["HOME"], ".config", programName)
}
// In theory we could also read /etc/passwd and look for the home based on
// the process' UID
return ""
}
// CacheDir returns the cache directory for the application
func CacheDir(env map[string]string, programName string) string {
// READ an exact directory Location ( Helps Windows processes not rely on XDG_CACHE_HOME )
if env["DIRENV_CACHE_DIR"] != "" {
return filepath.Join(env["DIRENV_CACHE_DIR"], programName)
} else if env["XDG_CACHE_HOME"] != "" {
return filepath.Join(env["XDG_CACHE_HOME"], programName)
} else if env["HOME"] != "" {
return filepath.Join(env["HOME"], ".cache", programName)
}
// In theory we could also read /etc/passwd and look for the home based on
// the process' UID
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment