Skip to content
This repository has been archived by the owner on Jan 17, 2021. It is now read-only.

Commit

Permalink
Fix bug where an empty bind address errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sreya committed May 8, 2019
1 parent dbc0ff5 commit ffe788b
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions sshcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ func sshCode(host, dir string, o options) error {
o.sshFlags = strings.Join([]string{extraSSHFlags, o.sshFlags}, " ")
}

o.bindAddr, err = parseBindAddr(o.bindAddr)
if err != nil {
return xerrors.Errorf("failed to parse bind address: %w", err)
}

if o.remotePort == "" {
o.remotePort, err = randomPort()
}
if err != nil {
return xerrors.Errorf("failed to find available remote port: %w", err)
}

dlScript := downloadScript(codeServerPath)

// Downloads the latest code-server and allows it to be executed.
Expand Down Expand Up @@ -79,18 +91,6 @@ func sshCode(host, dir string, o options) error {

flog.Info("starting code-server...")

o.bindAddr, err = parseBindAddr(o.bindAddr)
if err != nil {
return xerrors.Errorf("failed to parse bind address: %w", err)
}

if o.remotePort == "" {
o.remotePort, err = randomPort()
}
if err != nil {
return xerrors.Errorf("failed to find available remote port: %w", err)
}

flog.Info("Tunneling remote port %v to %v", o.remotePort, o.bindAddr)

sshCmdStr =
Expand Down Expand Up @@ -168,19 +168,26 @@ func sshCode(host, dir string, o options) error {
}

func parseBindAddr(bindAddr string) (string, error) {
if bindAddr == "" {
bindAddr = ":"
}

host, port, err := net.SplitHostPort(bindAddr)
if err != nil {
return "", err
}

if host == "" {
host = "127.0.0.1"
}

if port == "" {
port, err = randomPort()
}
if err != nil {
return "", err
}

return net.JoinHostPort(host, port), nil
}

Expand Down

0 comments on commit ffe788b

Please sign in to comment.