@@ -101,9 +101,9 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
101
101
if err != nil {
102
102
return nil , err
103
103
}
104
- // y is loaded with FillDefault() already, so no need to care about nil pointers.
104
+ // instConf is loaded with FillDefault() already, so no need to care about nil pointers.
105
105
106
- sshLocalPort , err := determineSSHLocalPort (instConfig , instName )
106
+ sshLocalPort , err := determineSSHLocalPort (* instConfig . SSH . LocalPort , instName )
107
107
if err != nil {
108
108
return nil , err
109
109
}
@@ -146,7 +146,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
146
146
if err != nil {
147
147
return nil , err
148
148
}
149
- if err = writeSSHConfigFile (inst , inst .SSHAddress , sshLocalPort , sshOpts ); err != nil {
149
+ if err = writeSSHConfigFile (inst . Name , inst . Dir , inst .SSHAddress , sshLocalPort , sshOpts ); err != nil {
150
150
return nil , err
151
151
}
152
152
sshConfig := & ssh.SSHConfig {
@@ -220,9 +220,9 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
220
220
return a , nil
221
221
}
222
222
223
- func writeSSHConfigFile (inst * store. Instance , instSSHAddress string , sshLocalPort int , sshOpts []string ) error {
224
- if inst . Dir == "" {
225
- return fmt .Errorf ("directory is unknown for the instance %q" , inst . Name )
223
+ func writeSSHConfigFile (instName , instDir , instSSHAddress string , sshLocalPort int , sshOpts []string ) error {
224
+ if instDir == "" {
225
+ return fmt .Errorf ("directory is unknown for the instance %q" , instName )
226
226
}
227
227
var b bytes.Buffer
228
228
if _ , err := fmt .Fprintf (& b , `# This SSH config file can be passed to 'ssh -F'.
@@ -231,35 +231,33 @@ func writeSSHConfigFile(inst *store.Instance, instSSHAddress string, sshLocalPor
231
231
` ); err != nil {
232
232
return err
233
233
}
234
- if err := sshutil .Format (& b , inst . Name , sshutil .FormatConfig ,
234
+ if err := sshutil .Format (& b , instName , sshutil .FormatConfig ,
235
235
append (sshOpts ,
236
236
fmt .Sprintf ("Hostname=%s" , instSSHAddress ),
237
237
fmt .Sprintf ("Port=%d" , sshLocalPort ),
238
238
)); err != nil {
239
239
return err
240
240
}
241
- fileName := filepath .Join (inst . Dir , filenames .SSHConfig )
241
+ fileName := filepath .Join (instDir , filenames .SSHConfig )
242
242
return os .WriteFile (fileName , b .Bytes (), 0o600 )
243
243
}
244
244
245
- func determineSSHLocalPort (y * limayaml. LimaYAML , instName string ) (int , error ) {
246
- if * y . SSH . LocalPort > 0 {
247
- return * y . SSH . LocalPort , nil
245
+ func determineSSHLocalPort (confLocalPort int , instName string ) (int , error ) {
246
+ if confLocalPort > 0 {
247
+ return confLocalPort , nil
248
248
}
249
- if * y . SSH . LocalPort < 0 {
250
- return 0 , fmt .Errorf ("invalid ssh local port %d" , y . SSH . LocalPort )
249
+ if confLocalPort < 0 {
250
+ return 0 , fmt .Errorf ("invalid ssh local port %d" , confLocalPort )
251
251
}
252
- switch instName {
253
- case "default" :
252
+ if instName == "default" {
254
253
// use hard-coded value for "default" instance, for backward compatibility
255
254
return 60022 , nil
256
- default :
257
- sshLocalPort , err := findFreeTCPLocalPort ()
258
- if err != nil {
259
- return 0 , fmt .Errorf ("failed to find a free port, try setting `ssh.localPort` manually: %w" , err )
260
- }
261
- return sshLocalPort , nil
262
255
}
256
+ sshLocalPort , err := findFreeTCPLocalPort ()
257
+ if err != nil {
258
+ return 0 , fmt .Errorf ("failed to find a free port, try setting `ssh.localPort` manually: %w" , err )
259
+ }
260
+ return sshLocalPort , nil
263
261
}
264
262
265
263
func findFreeTCPLocalPort () (int , error ) {
0 commit comments