Change storage paths to follow OS conventions; migrate folder (#2955)

This commit is contained in:
Matthew Holt 2019-12-31 16:47:35 -07:00
parent fdabac51a8
commit 984d384d14
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
5 changed files with 202 additions and 15 deletions

View file

@ -20,6 +20,7 @@ import (
"runtime"
"github.com/mholt/certmagic"
"go.uber.org/zap"
)
// StorageConverter is a type that can convert itself
@ -31,10 +32,28 @@ type StorageConverter interface {
CertMagicStorage() (certmagic.Storage, error)
}
// homeDir returns the best guess of the current user's home
// HomeDir returns the best guess of the current user's home
// directory from environment variables. If unknown, "." (the
// current directory) is returned instead.
func homeDir() string {
// current directory) is returned instead, except GOOS=android,
// which returns "/sdcard".
func HomeDir() string {
home := homeDirUnsafe()
if home == "" && runtime.GOOS == "android" {
home = "/sdcard"
}
if home == "" {
home = "."
}
return home
}
// homeDirUnsafe is a low-level function that returns
// the user's home directory from environment
// variables. Careful: if it cannot be determined, an
// empty string is returned. If not accounting for
// that case, use HomeDir() instead; otherwise you
// may end up using the root of the file system.
func homeDirUnsafe() string {
home := os.Getenv("HOME")
if home == "" && runtime.GOOS == "windows" {
drive := os.Getenv("HOMEDRIVE")
@ -44,21 +63,96 @@ func homeDir() string {
home = os.Getenv("USERPROFILE")
}
}
if home == "" {
home = "."
if home == "" && runtime.GOOS == "plan9" {
home = os.Getenv("home")
}
return home
}
// dataDir returns a directory path that is suitable for storage.
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
func dataDir() string {
baseDir := filepath.Join(homeDir(), ".local", "share")
if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" {
baseDir = xdgData
// AppConfigDir returns the directory where to store user's config.
//
// If XDG_CONFIG_HOME is set, it returns: $XDG_CONFIG_HOME/caddy.
// Otherwise, os.UserConfigDir() is used; if successful, it appends
// "Caddy" (Windows & Mac) or "caddy" (every other OS) to the path.
// If it returns an error, the fallback path "./caddy" is returned.
//
// The config directory is not guaranteed to be different from
// AppDataDir().
//
// Unlike os.UserConfigDir(), this function prefers the
// XDG_CONFIG_HOME env var on all platforms, not just Unix.
//
// Ref: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
func AppConfigDir() string {
basedir := os.Getenv("XDG_CONFIG_HOME")
if basedir == "" {
var err error
basedir, err = os.UserConfigDir()
if err != nil {
Log().Warn("unable to determine directory for user configuration; falling back to current directory", zap.Error(err))
return "./caddy"
}
}
return filepath.Join(baseDir, "caddy")
subdir := "caddy"
switch runtime.GOOS {
case "windows", "darwin":
subdir = "Caddy"
}
return filepath.Join(basedir, subdir)
}
// TODO: Consider using Go 1.13's os.UserConfigDir() (https://golang.org/pkg/os/#UserConfigDir)
// if we are going to store the last-loaded config anywhere
// AppDataDir returns a directory path that is suitable for storing
// application data on disk. It uses the environment for finding the
// best place to store data, and appends a "caddy" or "Caddy" (depending
// on OS and environment) subdirectory.
//
// For a base directory path:
// If XDG_DATA_HOME is set, it returns: $XDG_DATA_HOME/caddy; otherwise,
// on Windows it returns: %AppData%/Caddy,
// on Mac: $HOME/Library/Application Support/Caddy,
// on Plan9: $home/lib/caddy,
// on Android: $HOME/caddy,
// and on everything else: $HOME/.local/share/caddy.
//
// If a data directory cannot be determined, it returns "./caddy"
// (this is not ideal, and the environment should be fixed).
//
// The data directory is not guaranteed to be different from AppConfigDir().
//
// Ref: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
func AppDataDir() string {
if basedir := os.Getenv("XDG_DATA_HOME"); basedir != "" {
return filepath.Join(basedir, "caddy")
}
switch runtime.GOOS {
case "windows":
appData := os.Getenv("AppData")
if appData != "" {
return filepath.Join(appData, "Caddy")
}
case "darwin":
home := homeDirUnsafe()
if home != "" {
return filepath.Join(home, "Library", "Application Support", "Caddy")
}
case "plan9":
home := homeDirUnsafe()
if home != "" {
return filepath.Join(home, "lib", "caddy")
}
case "android":
home := homeDirUnsafe()
if home != "" {
return filepath.Join(home, "caddy")
}
default:
home := homeDirUnsafe()
if home != "" {
return filepath.Join(home, ".local", "share", "caddy")
}
}
return "./caddy"
}
// ConfigAutosavePath is the default path to which the last config will be persisted.
var ConfigAutosavePath = filepath.Join(AppConfigDir(), "autosave.json")