dnote/pkg/cli/dirs/dirs.go
Sung Won Cho e9f3b080d5
Use XDG base directory (#527)
* Create platform specific directory definitions

* Fix CLI integration test

* Rename dirs to paths and get config path

* Namespace

* Fix initialization of dirs

* Simplify and change description

* Simplify

* Fix build flag

* Bump sqlite version

* Bump xgo
2021-01-03 12:11:22 +11:00

49 lines
968 B
Go

// Package dirs provides base directory definitions for the system
package dirs
import (
"os"
"os/user"
"github.com/pkg/errors"
)
var (
// Home is the home directory of the user
Home string
// ConfigHome is the full path to the directory in which user-specific
// configurations should be written.
ConfigHome string
// DataHome is the full path to the directory in which user-specific data
// files should be written.
DataHome string
// CacheHome is the full path to the directory in which user-specific
// non-essential cached data should be writte
CacheHome string
)
func init() {
Reload()
}
// Reload reloads the directory definitions
func Reload() {
initDirs()
}
func getHomeDir() string {
usr, err := user.Current()
if err != nil {
panic(errors.Wrap(err, "getting home dir"))
}
return usr.HomeDir
}
func readPath(envName, defaultPath string) string {
if dir := os.Getenv(envName); dir != "" {
return dir
}
return defaultPath
}