Skip to content

Commit

Permalink
fix(client): prefer CLI arguments to config, following OpenSSH behavi…
Browse files Browse the repository at this point in the history
…our (#45)

The client prefered using values from the config file. The expected
behaviour is to prefer vales from the CLI and fill the missing ones with
defaults in the configuration files.
This commit fixes 35 by prefering CLI over config.

fix: 35
  • Loading branch information
TheoTechnicguy authored Dec 18, 2023
1 parent 6127e06 commit d868c6c
Showing 1 changed file with 17 additions and 8 deletions.
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
// 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 {
// 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
}

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

0 comments on commit d868c6c

Please sign in to comment.