aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/BurntSushi/xdg/xdg.go
blob: fddceb90b607111cb07ca93bd43af2fd48fb5c55 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package xdg

import (
	"fmt"
	"go/build"
	"io/ioutil"
	"os"
	"path"
	"strings"
)

// Paths determines which directories to search. The first directory
// containing a matching file is used. Here is the order:
//
// Override is always checked first.
//
// Directories specified in the XDG spec are searched after "XDGSuffix" is
// appended.
//
// For configuration files, these are:
//
//	$XDG_CONFIG_HOME (or $HOME/.config when not set)
//	Directories in $XDG_CONFIG_DIRS (or /etc/xdg when not set)
//
// For data files, these are:
//
//	$XDG_DATA_HOME (or $HOME/.local/share when not set)
//	Directories in $XDG_DATA_DIRS (or /usr/local/share:/usr/share when not set)
//
// For runtime files, these are:
//
//	$XDG_RUNTIME_DIR (or /tmp when not set; implementation defined)
//
// Finally, the directory specified by GoImportPath is searched in all
// source directories reported by the `go/build` package.
type Paths struct {
	// When non-empty, this will be the first directory searched.
	Override string

	// The suffix path appended to XDG directories.
	// i.e., "wingo" and NOT "/home/andrew/.config/wingo"
	XDGSuffix string

	// The path in which your data files live inside your project directory.
	// This should include your Go import path plus the directory containing
	// files in your repo. This is used as a last resort to find files.
	// (And it will only work if your package was installed using the GOPATH
	// environment.)
	//
	// N.B. XDGSuffix is not used here,
	// i.e., "github.com/BurntSushi/wingo/config"
	GoImportPath string
}

// MustPanic takes the return values of ConfigFile or DataFile, reads the file
// into a []byte, and returns the bytes.
//
// If the operation does not succeed, it panics.
func (ps Paths) MustPanic(fpath string, err error) []byte {
	if err != nil {
		panic(err)
	}
	bs, err := ioutil.ReadFile(fpath)
	if err != nil {
		panic(err)
	}
	return bs
}

// MustError is like MustPanic, but instead of panicing when something goes
// wrong, it prints the error to stderr and calls os.Exit(1).
func (ps Paths) MustError(fpath string, err error) []byte {
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not read %s: %s", fpath, err)
		os.Exit(1)
	}
	bs, err := ioutil.ReadFile(fpath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not read %s: %s", fpath, err)
		os.Exit(1)
	}
	return bs
}

// ConfigFile returns a file path containing the configuration file
// specified. If one cannot be found, an error will be returned which
// contains a list of all file paths searched.
func (ps Paths) ConfigFile(name string) (string, error) {
	home := os.Getenv("HOME")
	xdgHome := os.Getenv("XDG_CONFIG_HOME")
	xdgDirs := os.Getenv("XDG_CONFIG_DIRS")

	// We're going to accumulate a list of directories for places to inspect
	// for configuration files. Basically, this includes following the
	// xdg basedir spec for the XDG_CONFIG_HOME and XDG_CONFIG_DIRS environment
	// variables.
	try := make([]string, 0)

	// from override
	if len(ps.Override) > 0 {
		try = append(try, ps.Override)
	}

	// XDG_CONFIG_HOME
	if len(xdgHome) > 0 && strings.HasPrefix(xdgHome, "/") {
		try = append(try, path.Join(xdgHome, ps.XDGSuffix))
	} else if len(home) > 0 {
		try = append(try, path.Join(home, ".config", ps.XDGSuffix))
	}

	// XDG_CONFIG_DIRS
	if len(xdgDirs) > 0 {
		for _, p := range strings.Split(xdgDirs, ":") {
			// XDG basedir spec does not allow relative paths
			if !strings.HasPrefix(p, "/") {
				continue
			}
			try = append(try, path.Join(p, ps.XDGSuffix))
		}
	} else {
		try = append(try, path.Join("/", "etc", "xdg", ps.XDGSuffix))
	}

	// Add directories from GOPATH. Last resort.
	for _, dir := range build.Default.SrcDirs() {
		d := path.Join(dir, ps.GoImportPath)
		try = append(try, d)
	}

	return searchPaths(try, name)
}

// DataFile returns a file path containing the data file
// specified. If one cannot be found, an error will be returned which
// contains a list of all file paths searched.
func (ps Paths) DataFile(name string) (string, error) {
	home := os.Getenv("HOME")
	xdgHome := os.Getenv("XDG_DATA_HOME")
	xdgDirs := os.Getenv("XDG_DATA_DIRS")

	// We're going to accumulate a list of directories for places to inspect
	// for data files. Basically, this includes following the
	// xdg basedir spec for the XDG_DATA_HOME and XDG_DATA_DIRS environment
	// variables.
	try := make([]string, 0)

	// from override
	if len(ps.Override) > 0 {
		try = append(try, ps.Override)
	}

	// XDG_DATA_HOME
	if len(xdgHome) > 0 && strings.HasPrefix(xdgHome, "/") {
		try = append(try, path.Join(xdgHome, ps.XDGSuffix))
	} else if len(home) > 0 {
		try = append(try, path.Join(home, ".local", "share", ps.XDGSuffix))
	}

	// XDG_CONFIG_DIRS
	if len(xdgDirs) > 0 {
		for _, p := range strings.Split(xdgDirs, ":") {
			// XDG basedir spec does not allow relative paths
			if !strings.HasPrefix(p, "/") {
				continue
			}
			try = append(try, path.Join(p, ps.XDGSuffix))
		}
	} else {
		try = append(try, path.Join("/", "usr", "local", "share", ps.XDGSuffix))
		try = append(try, path.Join("/", "usr", "share", ps.XDGSuffix))
	}

	// Add directories from GOPATH. Last resort.
	for _, dir := range build.Default.SrcDirs() {
		d := path.Join(dir, ps.GoImportPath)
		try = append(try, d)
	}

	return searchPaths(try, name)
}

// RuntimeFile returns a file path containing the runtime file
// specified. If one cannot be found, an error will be returned which
// contains a list of all file paths searched.
func (ps Paths) RuntimeFile(name string) (string, error) {
	xdgRuntime := os.Getenv("XDG_RUNTIME_DIR")

	try := make([]string, 0)

	// from override
	if len(ps.Override) > 0 {
		try = append(try, ps.Override)
	}

	// XDG_RUNTIME_DIR
	if len(xdgRuntime) > 0 {
		try = append(try, path.Join(xdgRuntime, ps.XDGSuffix))
	} else {
		try = append(try, path.Join(os.TempDir(), ps.XDGSuffix))
	}

	// Add directories from GOPATH. Last resort.
	for _, dir := range build.Default.SrcDirs() {
		d := path.Join(dir, ps.GoImportPath)
		try = append(try, d)
	}

	return searchPaths(try, name)
}

func searchPaths(paths []string, suffix string) (string, error) {
	// Now use the first one and keep track of the ones we've tried.
	tried := make([]string, 0, len(paths))
	for _, dir := range paths {
		if len(dir) == 0 {
			continue
		}

		fpath := path.Join(dir, suffix)
		if exists(fpath) {
			return fpath, nil
		} else {
			tried = append(tried, fpath)
		}
	}

	// Show the user where we've looked for config files...
	triedStr := strings.Join(tried, ", ")
	return "", fmt.Errorf("Could not find a '%s' file. Tried "+
		"the following paths: %s", suffix, triedStr)
}

func exists(p string) bool {
	_, err := os.Stat(p)
	return err == nil || os.IsExist(err)
}