-
Notifications
You must be signed in to change notification settings - Fork 507
/
app.go
97 lines (91 loc) · 2.97 KB
/
app.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
package filelocation
import (
"context"
"path/filepath"
)
const appName = "telepresence"
// AppUserLogDir returns the directory to use for application-specific
// user-specific log files.
//
// - On Darwin, it returns "$HOME/Library/Logs/telepresnece". Specified by:
// https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
//
// - On everything else, it returns "{{AppUserCacheDir}}/logs" (using the
// appropriate path separator, if not "/").
//
// If the location cannot be determined (for example, $HOME is not defined),
// then it will return an error.
func AppUserLogDir(ctx context.Context) (string, error) {
if untyped := ctx.Value(logCtxKey{}); untyped != nil {
return untyped.(string), nil
}
switch goos(ctx) {
case "darwin":
home, err := UserHomeDir(ctx)
if err != nil {
return "", err
}
return filepath.Join(home, "Library", "Logs", appName), nil
default: // Unix
appCacheDir, err := AppUserCacheDir(ctx)
if err != nil {
return "", err
}
return filepath.Join(appCacheDir, "logs"), nil
}
}
// AppUserCacheDir returns the directory to use for application-specific
// user-specific cache data.
//
// On all platforms, this returns "{{UserCacheDir}}/telepresence" (using the
// appropriate path separator, if not "/").
//
// If the location cannot be determined (for example, $HOME is not defined),
// then it will return an error.
func AppUserCacheDir(ctx context.Context) (string, error) {
userDir, err := userCacheDir(ctx)
if err != nil {
return "", err
}
return filepath.Join(userDir, appName), nil
}
// AppUserConfigDir returns the directory to use for application-specific
// user-specific configuration data.
//
// On all platforms, this returns "{{UserConfigDir}}/telepresence" (using the
// appropriate path separator, if not "/").
//
// If the location cannot be determined (for example, $HOME is not defined),
// then it will return an error.
func AppUserConfigDir(ctx context.Context) (string, error) {
if untyped := ctx.Value(configCtxKey{}); untyped != nil {
return untyped.(string), nil
}
userDir, err := UserConfigDir(ctx)
if err != nil {
return "", err
}
return filepath.Join(userDir, appName), nil
}
// AppSystemConfigDirs returns a list of directories to search for
// application-specific (but not user-specific) configuration data.
//
// On all platforms, this returns the list from SystemConfigDirs, with
// "/telepresence" appended to each directory (using the appropriate path
// separator, if not "/").
//
// If the location cannot be determined, then it will return an error.
func AppSystemConfigDirs(ctx context.Context) ([]string, error) {
if untyped := ctx.Value(sysConfigsCtxKey{}); untyped != nil {
return untyped.([]string), nil
}
dirs, err := systemConfigDirs(ctx)
if err != nil {
return nil, err
}
ret := make([]string, 0, len(dirs))
for _, dir := range dirs {
ret = append(ret, filepath.Join(dir, appName))
}
return ret, nil
}