Skip to content

Commit 5125340

Browse files
authored
store things following XDG standards (#102)
* add config dir function * use xdg default paths * store logs in XDG state dir
1 parent 9123118 commit 5125340

File tree

8 files changed

+58
-17
lines changed

8 files changed

+58
-17
lines changed

e2e/scripts/addons-only.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@ main() {
106106
}
107107

108108
export HELMVM_METRICS_BASEURL="https://staging.replicated.app"
109-
export KUBECONFIG=/root/.helmvm/etc/kubeconfig
110-
export PATH="$PATH:/root/.helmvm/bin"
109+
export KUBECONFIG=/root/.config/.helmvm/etc/kubeconfig
110+
export PATH=$PATH:/root/.config/.helmvm/bin
111111
main

e2e/scripts/embed-and-install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,6 @@ main() {
139139
}
140140

141141
export HELMVM_METRICS_BASEURL="https://staging.replicated.app"
142-
export KUBECONFIG=/root/.helmvm/etc/kubeconfig
143-
export PATH=$PATH:/root/.helmvm/bin
142+
export KUBECONFIG=/root/.config/.helmvm/etc/kubeconfig
143+
export PATH=$PATH:/root/.config/.helmvm/bin
144144
main

e2e/scripts/embedded-preflight.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ main() {
185185
}
186186

187187
export HELMVM_METRICS_BASEURL="https://staging.replicated.app"
188-
export KUBECONFIG=/root/.helmvm/etc/kubeconfig
189-
export PATH=$PATH:/root/.helmvm/bin
188+
export KUBECONFIG=/root/.config/.helmvm/etc/kubeconfig
189+
export PATH=$PATH:/root/.config/.helmvm/bin
190190
main

e2e/scripts/install-with-disabled-addons.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ main() {
9494
}
9595

9696
export HELMVM_METRICS_BASEURL="https://staging.replicated.app"
97-
export KUBECONFIG=/root/.helmvm/etc/kubeconfig
98-
export PATH=$PATH:/root/.helmvm/bin
97+
export KUBECONFIG=/root/.config/.helmvm/etc/kubeconfig
98+
export PATH=$PATH:/root/.config/.helmvm/bin
9999
main

e2e/scripts/multi-node-install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ main() {
8686
}
8787

8888
export HELMVM_METRICS_BASEURL="https://staging.replicated.app"
89-
export KUBECONFIG=/root/.helmvm/etc/kubeconfig
90-
export PATH=$PATH:/root/.helmvm/bin
89+
export KUBECONFIG=/root/.config/.helmvm/etc/kubeconfig
90+
export PATH=$PATH:/root/.config/.helmvm/bin
9191
main

e2e/scripts/single-node-install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ main() {
6666
}
6767

6868
export HELMVM_METRICS_BASEURL="https://staging.replicated.app"
69-
export KUBECONFIG=/root/.helmvm/etc/kubeconfig
70-
export PATH=$PATH:/root/.helmvm/bin
69+
export KUBECONFIG=/root/.config/.helmvm/etc/kubeconfig
70+
export PATH=$PATH:/root/.config/.helmvm/bin
7171
main

e2e/scripts/wait-for-ready-nodes.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ main() {
2222
}
2323

2424
export HELMVM_METRICS_BASEURL="https://staging.replicated.app"
25-
export KUBECONFIG=/root/.helmvm/etc/kubeconfig
26-
export PATH=$PATH:/root/.helmvm/bin
25+
export KUBECONFIG=/root/.config/.helmvm/etc/kubeconfig
26+
export PATH=$PATH:/root/.config/.helmvm/bin
2727
main "$@"

pkg/defaults/provider.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,46 @@ func (d *DefaultsProvider) home() string {
6060
return home
6161
}
6262

63+
// config returns the user's config dir.
64+
func (d *DefaultsProvider) config() string {
65+
home, err := os.UserHomeDir()
66+
if err != nil {
67+
logrus.Fatalf("unable to get user home dir: %s", err)
68+
}
69+
70+
// use the XDG_CONFIG_HOME environment variable if set
71+
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
72+
return xdgConfigHome
73+
}
74+
75+
// otherwise, default to $HOME/.config on linux
76+
if runtime.GOOS == "linux" {
77+
return filepath.Join(home, ".config")
78+
}
79+
80+
return home
81+
}
82+
83+
// state returns the user's state dir.
84+
func (d *DefaultsProvider) state() string {
85+
home, err := os.UserHomeDir()
86+
if err != nil {
87+
logrus.Fatalf("unable to get user home dir: %s", err)
88+
}
89+
90+
// use the XDG_STATE_HOME environment variable if set
91+
if xdgStateHome := os.Getenv("XDG_STATE_HOME"); xdgStateHome != "" {
92+
return xdgStateHome
93+
}
94+
95+
// otherwise, default to $HOME/.local/state on linux
96+
if runtime.GOOS == "linux" {
97+
return filepath.Join(home, ".local", "state")
98+
}
99+
100+
return home
101+
}
102+
63103
// BinaryName returns the binary name, this is useful for places where we
64104
// need to present the name of the binary to the user (the name may vary if
65105
// the binary is renamed). We make sure the name does not contain invalid
@@ -77,7 +117,7 @@ func (d *DefaultsProvider) BinaryName() string {
77117
// stored. This is a subdirectory of the user's home directory.
78118
func (d *DefaultsProvider) HelmVMLogsSubDir() string {
79119
hidden := fmt.Sprintf(".%s", d.BinaryName())
80-
return filepath.Join(d.Base, d.home(), hidden, "logs")
120+
return filepath.Join(d.Base, d.state(), hidden, "logs")
81121
}
82122

83123
// PathToLog returns the full path to a log file. This function does not check
@@ -100,7 +140,7 @@ func (d *DefaultsProvider) K0sctlBinsSubDir() string {
100140
// are stored. This is a subdirectory of the user's home directory.
101141
func (d *DefaultsProvider) HelmVMBinsSubDir() string {
102142
hidden := fmt.Sprintf(".%s", d.BinaryName())
103-
return filepath.Join(d.Base, d.home(), hidden, "bin")
143+
return filepath.Join(d.Base, d.config(), hidden, "bin")
104144
}
105145

106146
// K0sctlApplyLogPath returns the path to the k0sctl apply log file.
@@ -126,9 +166,10 @@ func (d *DefaultsProvider) SSHConfigSubDir() string {
126166

127167
// ConfigSubDir returns the path to the directory where k0sctl configuration
128168
// files are stored. This is a subdirectory of the user's home directory.
169+
// TODO update
129170
func (d *DefaultsProvider) ConfigSubDir() string {
130171
hidden := fmt.Sprintf(".%s", d.BinaryName())
131-
return filepath.Join(d.Base, d.home(), hidden, "etc")
172+
return filepath.Join(d.Base, d.config(), hidden, "etc")
132173
}
133174

134175
// K0sBinaryPath returns the path to the k0s binary.

0 commit comments

Comments
 (0)