~tsileo/blobstash

ref: b178995e346d blobstash/pkg/config/pathutil/pathutil.go -rw-r--r-- 1.7 KiB
b178995eThomas Sileo vendor: update deps 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*

Package pathutil implements helpers to retrieve config/cache/var directories.

Follow XDG Base Directory Specification:
http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html

*/
package pathutil // import "a4.io/blobstash/pkg/config/pathutil"

import (
	"os"
	"path/filepath"
)

// ConfigDir find the best config directory
// following XDG Base Directory Specification
// http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
// These environment variable are checked in this order:
// - $BLOBSTASH_CONFIG_DIR
// - $XDG_CONFIG_HOME
// And will fallback to:
// - $HOME/.config/
func ConfigDir() string {
	if dir := os.Getenv("BLOBSTASH_CONFIG_DIR"); dir != "" {
		return dir
	}
	if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" {
		return filepath.Join(dir, "blobdb")
	}
	return filepath.Join(os.Getenv("HOME"), ".config", "blobstash")
}

// VarDir find the best var directory
// These environment variable are checked in this order:
// - $BLOBSTASH_CONFIG_DIR
// And will fallback to:
// - $HOME/var/
func VarDir() string {
	if dir := os.Getenv("BLOBSTASH_VAR_DIR"); dir != "" {
		return dir
	}
	return filepath.Join(os.Getenv("HOME"), "var", "blobstash")
}

// CacheDir return current user cache directory
// following XDG Base Directory Specification
// http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
// These environment variable are checked in this order:
// - $BLOBSTASH_CACHE_DIR
// - $XDG_CACHE_HOME
// And will fallback to:
// - $HOME/.cache/
func CacheDir() string {
	if dir := os.Getenv("BLOBSTASH_CACHE_DIR"); dir != "" {
		return dir
	}
	if dir := os.Getenv("XDG_CACHE_HOME"); dir != "" {
		return filepath.Join(dir, "blobstash")
	}
	return filepath.Join(os.Getenv("HOME"), ".cache", "blobstash")
}