forked from juju/juju
/
home.go
86 lines (75 loc) · 2.38 KB
/
home.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package osenv
import (
"os"
"path/filepath"
"runtime"
"sync"
"github.com/juju/utils"
)
// jujuXDGDataHome stores the path to the juju configuration
// folder, which is only meaningful when running the juju
// CLI tool, and is typically defined by $JUJU_DATA or
// $XDG_DATA_HOME/juju or ~/.local/share/juju as default if none
// of the aforementioned variables are defined.
var (
jujuXDGDataHomeMu sync.Mutex
jujuXDGDataHome string
)
// SetJujuXDGDataHome sets the value of juju home and
// returns the current one.
func SetJujuXDGDataHome(newJujuXDGDataHomeHome string) string {
jujuXDGDataHomeMu.Lock()
defer jujuXDGDataHomeMu.Unlock()
oldJujuXDGDataHomeHome := jujuXDGDataHome
jujuXDGDataHome = newJujuXDGDataHomeHome
return oldJujuXDGDataHomeHome
}
// JujuXDGDataHome returns the current juju home.
func JujuXDGDataHome() string {
jujuXDGDataHomeMu.Lock()
defer jujuXDGDataHomeMu.Unlock()
return jujuXDGDataHome
}
// JujuXDGDataHomePath returns the path to a file in the
// current juju home.
func JujuXDGDataHomePath(names ...string) string {
all := append([]string{JujuXDGDataHomeDir()}, names...)
return filepath.Join(all...)
}
// JujuXDGDataHomeDir returns the directory where juju should store application-specific files
func JujuXDGDataHomeDir() string {
JujuXDGDataHomeDir := JujuXDGDataHome()
if JujuXDGDataHomeDir != "" {
return JujuXDGDataHomeDir
}
JujuXDGDataHomeDir = os.Getenv(JujuXDGDataHomeEnvKey)
if JujuXDGDataHomeDir == "" {
if runtime.GOOS == "windows" {
JujuXDGDataHomeDir = jujuXDGDataHomeWin()
} else {
JujuXDGDataHomeDir = jujuXDGDataHomeLinux()
}
}
return JujuXDGDataHomeDir
}
// jujuXDGDataHomeLinux returns the directory where juju should store application-specific files on Linux.
func jujuXDGDataHomeLinux() string {
xdgConfig := os.Getenv(XDGDataHome)
if xdgConfig != "" {
return filepath.Join(xdgConfig, "juju")
}
// If xdg config home is not defined, the standard indicates that its default value
// is $HOME/.local/share
home := utils.Home()
return filepath.Join(home, ".local", "share", "juju")
}
// jujuXDGDataHomeWin returns the directory where juju should store application-specific files on Windows.
func jujuXDGDataHomeWin() string {
appdata := os.Getenv("APPDATA")
if appdata == "" {
return ""
}
return filepath.Join(appdata, "Juju")
}