Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace XDG Directory configuration to standard os.UserConfigDir #2826

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 7 additions & 27 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package caddy
import (
"os"
"path/filepath"
"runtime"

"github.com/mholt/certmagic"
)
Expand All @@ -31,34 +30,15 @@ type StorageConverter interface {
CertMagicStorage() (certmagic.Storage, error)
}

// 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 {
home := os.Getenv("HOME")
if home == "" && runtime.GOOS == "windows" {
drive := os.Getenv("HOMEDRIVE")
path := os.Getenv("HOMEPATH")
home = drive + path
if drive == "" || path == "" {
home = os.Getenv("USERPROFILE")
}
}
if home == "" {
home = "."
}
return home
}

// dataDir returns a directory path that is suitable for storage.
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
// If the location cannot be determined, use current directory.
func dataDir() string {
baseDir := filepath.Join(homeDir(), ".local", "share")
if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" {
baseDir = xdgData
baseDir, err := os.UserConfigDir()
if err != nil {
baseDir, err = os.Getwd()
if err != nil {
panic("failed to locate current directory")
}
}
return filepath.Join(baseDir, "caddy")
}

// 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