Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): prefer CLI arguments, following OpenSSH behaviour #45

Merged
merged 8 commits into from
Dec 18, 2023
34 changes: 25 additions & 9 deletions cli/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,6 @@ func mainWithStatusCode() int {
}

urlHostname, urlPort := parsedUrl.Hostname(), parsedUrl.Port()
if urlPort == "" {
urlPort = "443"
}

configHostname, configPort, configUser, configAuthMethods, err := ssh3.GetConfigForHost(urlHostname, sshConfig)
if err != nil {
Expand All @@ -416,14 +413,33 @@ func mainWithStatusCode() int {
}

hostnameIsAnIP := net.ParseIP(hostname) != nil

port := configPort
if port == -1 && urlPort != "" {
port, err = strconv.Atoi(urlPort)
if err != nil {
log.Error().Msgf("invalid port number: %s: %s", urlPort, err)
// *** Port selection ***
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I am not a huge fan of the table. I think we can make it more concise. The error case when the port cannot be parsed seems obvious.
I think it is fine if we just say that the CLI port takes precedence over the config port when specified.

// There are six scenarios:
//
// | CLI port | parse | config port | program behaviour |
// | -------- | ----- | ----------- | -------------------- |
// | yes | nok | yes | throw error and exit |
// | yes | nok | no | throw error and exit |
// | yes | ok | yes | use CLI port |
// | yes | ok | no | use CLI port |
// | no | --- | yes | use config port |
// | no | --- | no | use default port |

var port int
if urlPort != "" {
if parsedPort, err := strconv.Atoi(urlPort); err != nil || parsedPort > 0xffff {
// use WithLevel(zerolog.FatalLevel) to log a fatal level, but let us handle
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind inverting the two parts if this branch ? Putting the non-error case in the if part and the error case in the else part ? I think it makes it more readable because the default flow goes towards what we want to achieve. But maybe it is just me ? If you have good reasons to let it like that, let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do. I think this is more a go habit, as most of my if statements are used to check for the absence of errors 😄

// program termination. log.Fatal() exits with os.Exit(1).
log.WithLevel(zerolog.FatalLevel).Str("Port", urlPort).Err(err).Msg("cli contains an invalid port")
fmt.Fprintf(os.Stderr, "Bad port '%s'\n", urlPort)
return -1
} else {
port = parsedPort
}
} else if configPort != -1 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the comment // no port specified in the CLI args ?

port = configPort
} else {
port = 443
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment, like // no explicit port from the CLI and no port specified in config ?

}

username := parsedUrl.User.Username()
Expand Down