Skip to content

Commit 97df426

Browse files
chore(autocomplete): use os.UserHomeDir (#844)
1 parent 908df83 commit 97df426

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

internal/core/context.go

+10
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,15 @@ func ExtractEnv(ctx context.Context, envKey string) string {
8181
if value, exist := meta.OverrideEnv[envKey]; exist {
8282
return value
8383
}
84+
85+
if envKey == "HOME" {
86+
homeDir, _ := os.UserHomeDir()
87+
return homeDir
88+
}
89+
8490
return os.Getenv(envKey)
8591
}
92+
93+
func ExtractUserHomeDir(ctx context.Context) string {
94+
return ExtractEnv(ctx, "HOME")
95+
}

internal/namespaces/account/custom.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func initCommand() *core.Command {
4949
func InitRun(ctx context.Context, argsI interface{}) (i interface{}, e error) {
5050
// Get default local SSH key
5151
relativePath := path.Join(".ssh", "id_rsa.pub")
52-
filename := path.Join(core.ExtractEnv(ctx, "HOME"), relativePath)
52+
filename := path.Join(core.ExtractUserHomeDir(ctx), relativePath)
5353
shortenedFilename := "~/" + relativePath
5454
localSSHKeyContent, err := ioutil.ReadFile(filename)
5555

internal/namespaces/account/custom_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
)
1111

1212
func Test_initCommand(t *testing.T) {
13+
tmpDir := os.TempDir()
1314
t.Run("simple", core.Test(&core.TestConfig{
1415
Commands: GetCommands(),
1516
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
16-
tmpDir := os.TempDir()
1717
pathToPublicKey := path.Join(tmpDir, ".ssh", "id_rsa.pub")
1818
_, err := os.Stat(pathToPublicKey)
1919
if err != nil {
@@ -33,7 +33,7 @@ func Test_initCommand(t *testing.T) {
3333
core.TestCheckExitCode(0),
3434
),
3535
OverrideEnv: map[string]string{
36-
"HOME": os.TempDir(),
36+
"HOME": tmpDir,
3737
},
3838
}))
3939
}

internal/namespaces/autocomplete/autocomplete.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type autocompleteScript struct {
3434
ShellConfigurationFile map[string]string
3535
}
3636

37+
var homePath, _ = os.UserHomeDir()
38+
3739
// autocompleteScripts regroups the autocomplete scripts for the different shells
3840
// The key is the path of the shell.
3941
var autocompleteScripts = map[string]autocompleteScript{
@@ -63,8 +65,8 @@ var autocompleteScripts = map[string]autocompleteScript{
6365
`,
6466
CompleteScript: `eval "$(scw autocomplete script shell=bash)"`,
6567
ShellConfigurationFile: map[string]string{
66-
"darwin": path.Join(os.Getenv("HOME"), ".bash_profile"),
67-
"linux": path.Join(os.Getenv("HOME"), ".bashrc"),
68+
"darwin": path.Join(homePath, ".bash_profile"),
69+
"linux": path.Join(homePath, ".bashrc"),
6870
},
6971
},
7072
"fish": {
@@ -87,8 +89,8 @@ var autocompleteScripts = map[string]autocompleteScript{
8789
`,
8890
CompleteScript: `eval (scw autocomplete script shell=fish)`,
8991
ShellConfigurationFile: map[string]string{
90-
"darwin": path.Join(os.Getenv("HOME"), ".config/fish/config.fish"),
91-
"linux": path.Join(os.Getenv("HOME"), ".config/fish/config.fish"),
92+
"darwin": path.Join(homePath, ".config/fish/config.fish"),
93+
"linux": path.Join(homePath, ".config/fish/config.fish"),
9294
},
9395
},
9496
"zsh": {
@@ -109,8 +111,8 @@ var autocompleteScripts = map[string]autocompleteScript{
109111
`,
110112
CompleteScript: `eval "$(scw autocomplete script shell=zsh)"`,
111113
ShellConfigurationFile: map[string]string{
112-
"darwin": path.Join(os.Getenv("HOME"), ".zshrc"),
113-
"linux": path.Join(os.Getenv("HOME"), ".zshrc"),
114+
"darwin": path.Join(homePath, ".zshrc"),
115+
"linux": path.Join(homePath, ".zshrc"),
114116
},
115117
},
116118
}

0 commit comments

Comments
 (0)