forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.go
283 lines (248 loc) · 7.81 KB
/
paths.go
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package osutil
import (
"flag"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"camlistore.org/pkg/buildinfo"
)
// HomeDir returns the path to the user's home directory.
// It returns the empty string if the value isn't known.
func HomeDir() string {
failInTests()
if runtime.GOOS == "windows" {
return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
}
return os.Getenv("HOME")
}
// Username returns the current user's username, as
// reported by the relevant environment variable.
func Username() string {
if runtime.GOOS == "windows" {
return os.Getenv("USERNAME")
}
return os.Getenv("USER")
}
var cacheDirOnce sync.Once
func CacheDir() string {
cacheDirOnce.Do(makeCacheDir)
return cacheDir()
}
func cacheDir() string {
if d := os.Getenv("CAMLI_CACHE_DIR"); d != "" {
return d
}
failInTests()
switch runtime.GOOS {
case "darwin":
return filepath.Join(HomeDir(), "Library", "Caches", "Camlistore")
case "windows":
// Per http://technet.microsoft.com/en-us/library/cc749104(v=ws.10).aspx
// these should both exist. But that page overwhelms me. Just try them
// both. This seems to work.
for _, ev := range []string{"TEMP", "TMP"} {
if v := os.Getenv(ev); v != "" {
return filepath.Join(v, "Camlistore")
}
}
panic("No Windows TEMP or TMP environment variables found; please file a bug report.")
}
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
return filepath.Join(xdg, "camlistore")
}
return filepath.Join(HomeDir(), ".cache", "camlistore")
}
func makeCacheDir() {
err := os.MkdirAll(cacheDir(), 0700)
if err != nil {
log.Fatalf("Could not create cacheDir %v: %v", cacheDir(), err)
}
}
func CamliVarDir() string {
if d := os.Getenv("CAMLI_VAR_DIR"); d != "" {
return d
}
failInTests()
switch runtime.GOOS {
case "windows":
return filepath.Join(os.Getenv("APPDATA"), "Camlistore")
case "darwin":
return filepath.Join(HomeDir(), "Library", "Camlistore")
}
return filepath.Join(HomeDir(), "var", "camlistore")
}
func CamliBlobRoot() string {
return filepath.Join(CamliVarDir(), "blobs")
}
// RegisterConfigDirFunc registers a func f to return the Camlistore configuration directory.
// It may skip by returning the empty string.
func RegisterConfigDirFunc(f func() string) {
configDirFuncs = append(configDirFuncs, f)
}
var configDirFuncs []func() string
func CamliConfigDir() string {
if p := os.Getenv("CAMLI_CONFIG_DIR"); p != "" {
return p
}
for _, f := range configDirFuncs {
if v := f(); v != "" {
return v
}
}
failInTests()
return camliConfigDir()
}
func camliConfigDir() string {
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("APPDATA"), "Camlistore")
}
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
return filepath.Join(xdg, "camlistore")
}
return filepath.Join(HomeDir(), ".config", "camlistore")
}
func UserServerConfigPath() string {
return filepath.Join(CamliConfigDir(), "server-config.json")
}
func UserClientConfigPath() string {
return filepath.Join(CamliConfigDir(), "client-config.json")
}
// If set, flagSecretRing overrides the JSON config file
// ~/.config/camlistore/client-config.json
// (i.e. UserClientConfigPath()) "identitySecretRing" key.
var (
flagSecretRing string
secretRingFlagAdded bool
)
func AddSecretRingFlag() {
flag.StringVar(&flagSecretRing, "secret-keyring", "", "GnuPG secret keyring file to use.")
secretRingFlagAdded = true
}
// ExplicitSecretRingFile returns the path to the user's GPG secret ring
// file and true if it was ever set through the --secret-keyring flag or
// the CAMLI_SECRET_RING var. It returns "", false otherwise.
// Use of this function requires the program to call AddSecretRingFlag,
// and before flag.Parse is called.
func ExplicitSecretRingFile() (string, bool) {
if !secretRingFlagAdded {
panic("proper use of ExplicitSecretRingFile requires exposing flagSecretRing with AddSecretRingFlag")
}
if flagSecretRing != "" {
return flagSecretRing, true
}
if e := os.Getenv("CAMLI_SECRET_RING"); e != "" {
return e, true
}
return "", false
}
// DefaultSecretRingFile returns the path to the default GPG secret
// keyring. It is not influenced by any flag or CAMLI* env var.
func DefaultSecretRingFile() string {
return filepath.Join(camliConfigDir(), "identity-secring.gpg")
}
// identitySecretRing returns the path to the default GPG
// secret keyring. It is still affected by CAMLI_CONFIG_DIR.
func identitySecretRing() string {
return filepath.Join(CamliConfigDir(), "identity-secring.gpg")
}
// SecretRingFile returns the path to the user's GPG secret ring file.
// The value comes from either the --secret-keyring flag (if previously
// registered with AddSecretRingFlag), or the CAMLI_SECRET_RING environment
// variable, or the operating system default location.
func SecretRingFile() string {
if flagSecretRing != "" {
return flagSecretRing
}
if e := os.Getenv("CAMLI_SECRET_RING"); e != "" {
return e
}
return identitySecretRing()
}
// DefaultTLSCert returns the path to the default TLS certificate
// file that is used (creating if necessary) when TLS is specified
// without the cert file.
func DefaultTLSCert() string {
return filepath.Join(CamliConfigDir(), "tls.crt")
}
// DefaultTLSKey returns the path to the default TLS key
// file that is used (creating if necessary) when TLS is specified
// without the key file.
func DefaultTLSKey() string {
return filepath.Join(CamliConfigDir(), "tls.key")
}
// Find the correct absolute path corresponding to a relative path,
// searching the following sequence of directories:
// 1. Working Directory
// 2. CAMLI_CONFIG_DIR (deprecated, will complain if this is on env)
// 3. (windows only) APPDATA/camli
// 4. All directories in CAMLI_INCLUDE_PATH (standard PATH form for OS)
func FindCamliInclude(configFile string) (absPath string, err error) {
// Try to open as absolute / relative to CWD
_, err = os.Stat(configFile)
if err == nil {
return configFile, nil
}
if filepath.IsAbs(configFile) {
// End of the line for absolute path
return "", err
}
// Try the config dir
configDir := CamliConfigDir()
if _, err = os.Stat(filepath.Join(configDir, configFile)); err == nil {
return filepath.Join(configDir, configFile), nil
}
// Finally, search CAMLI_INCLUDE_PATH
p := os.Getenv("CAMLI_INCLUDE_PATH")
for _, d := range strings.Split(p, string(filepath.ListSeparator)) {
if _, err = os.Stat(filepath.Join(d, configFile)); err == nil {
return filepath.Join(d, configFile), nil
}
}
return "", os.ErrNotExist
}
// GoPackagePath returns the path to the provided Go package's
// source directory.
// pkg may be a path prefix without any *.go files.
// The error is os.ErrNotExist if GOPATH is unset or the directory
// doesn't exist in any GOPATH component.
func GoPackagePath(pkg string) (path string, err error) {
gp := os.Getenv("GOPATH")
if gp == "" {
return path, os.ErrNotExist
}
for _, p := range filepath.SplitList(gp) {
dir := filepath.Join(p, "src", filepath.FromSlash(pkg))
fi, err := os.Stat(dir)
if os.IsNotExist(err) {
continue
}
if err != nil {
return "", err
}
if !fi.IsDir() {
continue
}
return dir, nil
}
return path, os.ErrNotExist
}
func failInTests() {
if buildinfo.TestingLinked() {
panic("Unexpected non-hermetic use of host configuration during testing. (alternatively: the 'testing' package got accidentally linked in)")
}
}