Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 6aa23bf

Browse files
committed
Remove dependency on HOME env var
1 parent 0f0160a commit 6aa23bf

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

internal/cmd/configssh.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
5252
ctx, cancel := context.WithCancel(context.Background())
5353
defer cancel()
5454

55+
usr, err := user.Current()
56+
if err != nil {
57+
return xerrors.Errorf("get user home directory: %w", err)
58+
}
59+
60+
privateKeyFilepath := filepath.Join(usr.HomeDir, ".ssh", "coder_enterprise")
61+
5562
if strings.HasPrefix(*configpath, "~") {
56-
usr, err := user.Current()
57-
if err != nil {
58-
return xerrors.Errorf("get user home directory: %w", err)
59-
}
6063
*configpath = strings.Replace(*configpath, "~", usr.HomeDir, 1)
6164
}
6265

@@ -104,7 +107,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
104107
if len(envs) < 1 {
105108
return xerrors.New("no environments found")
106109
}
107-
newConfig, err := makeNewConfigs(user.Username, envs, startToken, startMessage, endToken)
110+
newConfig, err := makeNewConfigs(user.Username, envs, startToken, startMessage, endToken, privateKeyFilepath)
108111
if err != nil {
109112
return xerrors.Errorf("make new ssh configurations: %w", err)
110113
}
@@ -122,7 +125,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
122125
if err != nil {
123126
return xerrors.Errorf("write new configurations to ssh config file %q: %w", *configpath, err)
124127
}
125-
err = writeSSHKey(ctx, client)
128+
err = writeSSHKey(ctx, client, privateKeyFilepath)
126129
if err != nil {
127130
return xerrors.Errorf("fetch and write ssh key: %w", err)
128131
}
@@ -135,34 +138,30 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
135138
}
136139
}
137140

138-
var (
139-
privateKeyFilepath = filepath.Join(os.Getenv("HOME"), ".ssh", "coder_enterprise")
140-
)
141-
142-
func writeSSHKey(ctx context.Context, client *coder.Client) error {
141+
func writeSSHKey(ctx context.Context, client *coder.Client, privateKeyPath string) error {
143142
key, err := client.SSHKey(ctx)
144143
if err != nil {
145144
return err
146145
}
147-
return ioutil.WriteFile(privateKeyFilepath, []byte(key.PrivateKey), 0400)
146+
return ioutil.WriteFile(privateKeyPath, []byte(key.PrivateKey), 0400)
148147
}
149148

150-
func makeNewConfigs(userName string, envs []coder.Environment, startToken, startMsg, endToken string) (string, error) {
149+
func makeNewConfigs(userName string, envs []coder.Environment, startToken, startMsg, endToken, privateKeyFilepath string) (string, error) {
151150
hostname, err := configuredHostname()
152151
if err != nil {
153152
return "", nil
154153
}
155154

156155
newConfig := fmt.Sprintf("\n%s\n%s\n\n", startToken, startMsg)
157156
for _, env := range envs {
158-
newConfig += makeSSHConfig(hostname, userName, env.Name)
157+
newConfig += makeSSHConfig(hostname, userName, env.Name, privateKeyFilepath)
159158
}
160159
newConfig += fmt.Sprintf("\n%s\n", endToken)
161160

162161
return newConfig, nil
163162
}
164163

165-
func makeSSHConfig(host, userName, envName string) string {
164+
func makeSSHConfig(host, userName, envName, privateKeyFilepath string) string {
166165
return fmt.Sprintf(
167166
`Host coder.%s
168167
HostName %s

0 commit comments

Comments
 (0)