@@ -52,11 +52,14 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
52
52
ctx , cancel := context .WithCancel (context .Background ())
53
53
defer cancel ()
54
54
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
+
55
62
if strings .HasPrefix (* configpath , "~" ) {
56
- usr , err := user .Current ()
57
- if err != nil {
58
- return xerrors .Errorf ("get user home directory: %w" , err )
59
- }
60
63
* configpath = strings .Replace (* configpath , "~" , usr .HomeDir , 1 )
61
64
}
62
65
@@ -104,7 +107,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
104
107
if len (envs ) < 1 {
105
108
return xerrors .New ("no environments found" )
106
109
}
107
- newConfig , err := makeNewConfigs (user .Username , envs , startToken , startMessage , endToken )
110
+ newConfig , err := makeNewConfigs (user .Username , envs , startToken , startMessage , endToken , privateKeyFilepath )
108
111
if err != nil {
109
112
return xerrors .Errorf ("make new ssh configurations: %w" , err )
110
113
}
@@ -122,7 +125,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
122
125
if err != nil {
123
126
return xerrors .Errorf ("write new configurations to ssh config file %q: %w" , * configpath , err )
124
127
}
125
- err = writeSSHKey (ctx , client )
128
+ err = writeSSHKey (ctx , client , privateKeyFilepath )
126
129
if err != nil {
127
130
return xerrors .Errorf ("fetch and write ssh key: %w" , err )
128
131
}
@@ -135,34 +138,30 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
135
138
}
136
139
}
137
140
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 {
143
142
key , err := client .SSHKey (ctx )
144
143
if err != nil {
145
144
return err
146
145
}
147
- return ioutil .WriteFile (privateKeyFilepath , []byte (key .PrivateKey ), 0400 )
146
+ return ioutil .WriteFile (privateKeyPath , []byte (key .PrivateKey ), 0400 )
148
147
}
149
148
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 ) {
151
150
hostname , err := configuredHostname ()
152
151
if err != nil {
153
152
return "" , nil
154
153
}
155
154
156
155
newConfig := fmt .Sprintf ("\n %s\n %s\n \n " , startToken , startMsg )
157
156
for _ , env := range envs {
158
- newConfig += makeSSHConfig (hostname , userName , env .Name )
157
+ newConfig += makeSSHConfig (hostname , userName , env .Name , privateKeyFilepath )
159
158
}
160
159
newConfig += fmt .Sprintf ("\n %s\n " , endToken )
161
160
162
161
return newConfig , nil
163
162
}
164
163
165
- func makeSSHConfig (host , userName , envName string ) string {
164
+ func makeSSHConfig (host , userName , envName , privateKeyFilepath string ) string {
166
165
return fmt .Sprintf (
167
166
`Host coder.%s
168
167
HostName %s
0 commit comments