mirror of
https://github.com/neilotoole/sq.git
synced 2024-12-19 06:01:36 +03:00
f0aa65791b
* CHANGELOG text clarification * Dialing in config/options * Yet more dialing in of config/options * Refactor output writers * YAML output for more commands
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
// Package userlogdir has a single function, UserLogDir, that returns
|
|
// an OS-specific path for storing user logs.
|
|
package userlogdir
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"runtime"
|
|
)
|
|
|
|
// UserLogDir returns the default root directory to use for user-specific
|
|
// log data. Users should create their own application-specific subdirectory
|
|
// within this one and use that.
|
|
//
|
|
// On Unix systems, it returns $XDG_CACHE_HOME as specified by
|
|
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if
|
|
// non-empty, else $HOME/.cache.
|
|
// On Darwin, it returns $HOME/Library/Logs.
|
|
// On Windows, it returns %LocalAppData%.
|
|
// On Plan 9, it returns $home/lib/cache.
|
|
//
|
|
// If the location cannot be determined (for example, $HOME is not defined),
|
|
// then it will return an error.
|
|
func UserLogDir() (string, error) {
|
|
var dir string
|
|
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
dir = os.Getenv("LocalAppData")
|
|
if dir == "" {
|
|
return "", errors.New("%LocalAppData% is not defined")
|
|
}
|
|
|
|
case "darwin", "ios":
|
|
dir = os.Getenv("HOME")
|
|
if dir == "" {
|
|
return "", errors.New("$HOME is not defined")
|
|
}
|
|
dir += "/Library/Logs"
|
|
|
|
case "plan9":
|
|
dir = os.Getenv("home")
|
|
if dir == "" {
|
|
return "", errors.New("$home is not defined")
|
|
}
|
|
dir += "/lib/cache"
|
|
|
|
default: // Unix
|
|
dir = os.Getenv("XDG_CACHE_HOME")
|
|
if dir == "" {
|
|
dir = os.Getenv("HOME")
|
|
if dir == "" {
|
|
return "", errors.New("neither $XDG_CACHE_HOME nor $HOME are defined")
|
|
}
|
|
dir += "/.cache"
|
|
}
|
|
}
|
|
|
|
return dir, nil
|
|
}
|