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
25 changes: 17 additions & 8 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 @@ -417,13 +414,25 @@ 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)
var port int
if urlPort != "" {
if parsedPort, err := strconv.Atoi(urlPort); err == nil && parsedPort < 0xffff {
// There is a port in the CLI and the port is valid. Use the CLI port.
port = parsedPort
} else {
// There is a port in the CLI but it is not valid.
// 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 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 ?

// There is no port in the CLI, but one in a config file. Use the config port.
port = configPort
} else {
// There is no port specified, neither in the CLI, nor in the configuration.
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