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 version string "vnot connected" #460

Merged
merged 3 commits into from
Oct 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions internal/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type VersionOptions struct {
}

func version(options VersionOptions) error {
clientSemVer := fmt.Sprintf("v%s", internal.Version)
serverVersion := "not connected"
clientVersion := internal.Version
serverVersion := "disconnected"

// Note that we use the client to get this version, but it is in fact the server version
client, err := apiClientFromConfig()
Expand All @@ -31,9 +31,7 @@ func version(options VersionOptions) error {
}
}

serverSemVer := fmt.Sprintf("v%s", serverVersion)

err = checkUpdate(clientSemVer, serverSemVer)
err = checkUpdate(clientVersion, serverVersion)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed checking for updates:", err.Error())
}
Expand All @@ -44,20 +42,22 @@ func version(options VersionOptions) error {
fmt.Fprintln(w)

if !options.Registry {
fmt.Fprintln(w, "Client:\t", clientSemVer)
fmt.Fprintln(w, "Client:\t", clientVersion)
}

if !options.Client {
fmt.Fprintln(w, "Registry:\t", serverSemVer)
fmt.Fprintln(w, "Registry:\t", serverVersion)
}

fmt.Fprintln(w)

return nil
}

func checkUpdate(clientSemVer, serverSemVer string) error {
func checkUpdate(clientVersion, serverVersion string) error {
latestSemVer := "nonexistent"
clientSemVer := fmt.Sprintf("v%s", clientVersion)
serverSemVer := fmt.Sprintf("v%s", serverVersion)

req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://releases.infrahq.com/infra/latest", nil)
if err != nil {
Expand All @@ -80,12 +80,19 @@ func checkUpdate(clientSemVer, serverSemVer string) error {
latestSemVer = strings.TrimSpace(string(body))
}

var latestVersion string

_, err = fmt.Sscanf(latestSemVer, "v%s", &latestVersion)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

latestVersion will only be printed if it's a valid semver which needs to start with v so shouldn't need to check if Sscanf actually scanned anything

if err != nil {
return err
}

if clientSemVer != "v0.0.0-development" && semver.Compare(latestSemVer, clientSemVer) > 0 {
fmt.Fprintf(os.Stderr, "Your client (%s) is out of date. Please update to %s.\n", clientSemVer, latestSemVer)
fmt.Fprintf(os.Stderr, "Your client (%s) is out of date. Please update to %s.\n", clientVersion, latestVersion)
}

if serverSemVer != "v0.0.0-development" && semver.IsValid(serverSemVer) && semver.Compare(latestSemVer, serverSemVer) > 0 {
fmt.Fprintf(os.Stderr, "Your registry (%s) is out of date. Please update to %s.\n", serverSemVer, latestSemVer)
fmt.Fprintf(os.Stderr, "Your registry (%s) is out of date. Please update to %s.\n", serverVersion, latestVersion)
}

return nil
Expand Down