Skip to content

Commit cd12dbe

Browse files
authored
Merge pull request #2657 from alexandear/refactor/pkg-hostagent-priv-funcs
Refactor unexported functions in the pkg/hostagent
2 parents d3def6c + fca0873 commit cd12dbe

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

pkg/hostagent/hostagent.go

+19-21
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
101101
if err != nil {
102102
return nil, err
103103
}
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.
105105

106-
sshLocalPort, err := determineSSHLocalPort(instConfig, instName)
106+
sshLocalPort, err := determineSSHLocalPort(*instConfig.SSH.LocalPort, instName)
107107
if err != nil {
108108
return nil, err
109109
}
@@ -146,7 +146,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
146146
if err != nil {
147147
return nil, err
148148
}
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 {
150150
return nil, err
151151
}
152152
sshConfig := &ssh.SSHConfig{
@@ -220,9 +220,9 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
220220
return a, nil
221221
}
222222

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)
226226
}
227227
var b bytes.Buffer
228228
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
231231
`); err != nil {
232232
return err
233233
}
234-
if err := sshutil.Format(&b, inst.Name, sshutil.FormatConfig,
234+
if err := sshutil.Format(&b, instName, sshutil.FormatConfig,
235235
append(sshOpts,
236236
fmt.Sprintf("Hostname=%s", instSSHAddress),
237237
fmt.Sprintf("Port=%d", sshLocalPort),
238238
)); err != nil {
239239
return err
240240
}
241-
fileName := filepath.Join(inst.Dir, filenames.SSHConfig)
241+
fileName := filepath.Join(instDir, filenames.SSHConfig)
242242
return os.WriteFile(fileName, b.Bytes(), 0o600)
243243
}
244244

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
248248
}
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)
251251
}
252-
switch instName {
253-
case "default":
252+
if instName == "default" {
254253
// use hard-coded value for "default" instance, for backward compatibility
255254
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
262255
}
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
263261
}
264262

265263
func findFreeTCPLocalPort() (int, error) {

0 commit comments

Comments
 (0)