Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit6aa23bf

Browse files
committed
Remove dependency on HOME env var
1 parent0f0160a commit6aa23bf

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
defercancel()
5454

55+
usr,err:=user.Current()
56+
iferr!=nil {
57+
returnxerrors.Errorf("get user home directory: %w",err)
58+
}
59+
60+
privateKeyFilepath:=filepath.Join(usr.HomeDir,".ssh","coder_enterprise")
61+
5562
ifstrings.HasPrefix(*configpath,"~") {
56-
usr,err:=user.Current()
57-
iferr!=nil {
58-
returnxerrors.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
iflen(envs)<1 {
105108
returnxerrors.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
iferr!=nil {
109112
returnxerrors.Errorf("make new ssh configurations: %w",err)
110113
}
@@ -122,7 +125,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
122125
iferr!=nil {
123126
returnxerrors.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
iferr!=nil {
127130
returnxerrors.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-
funcwriteSSHKey(ctx context.Context,client*coder.Client)error {
141+
funcwriteSSHKey(ctx context.Context,client*coder.Client,privateKeyPathstring)error {
143142
key,err:=client.SSHKey(ctx)
144143
iferr!=nil {
145144
returnerr
146145
}
147-
returnioutil.WriteFile(privateKeyFilepath, []byte(key.PrivateKey),0400)
146+
returnioutil.WriteFile(privateKeyPath, []byte(key.PrivateKey),0400)
148147
}
149148

150-
funcmakeNewConfigs(userNamestring,envs []coder.Environment,startToken,startMsg,endTokenstring) (string,error) {
149+
funcmakeNewConfigs(userNamestring,envs []coder.Environment,startToken,startMsg,endToken,privateKeyFilepathstring) (string,error) {
151150
hostname,err:=configuredHostname()
152151
iferr!=nil {
153152
return"",nil
154153
}
155154

156155
newConfig:=fmt.Sprintf("\n%s\n%s\n\n",startToken,startMsg)
157156
for_,env:=rangeenvs {
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
returnnewConfig,nil
163162
}
164163

165-
funcmakeSSHConfig(host,userName,envNamestring)string {
164+
funcmakeSSHConfig(host,userName,envName,privateKeyFilepathstring)string {
166165
returnfmt.Sprintf(
167166
`Host coder.%s
168167
HostName %s

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp