From d5114c584aaba00dc988ae160076e68f8c090851 Mon Sep 17 00:00:00 2001 From: Marius Kimmina <38843153+mariuskimmina@users.noreply.github.com> Date: Tue, 2 Jan 2024 07:28:57 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20status=20cmd=20behind=20pr?= =?UTF-8?q?oxy=20(#2874)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/cnquery/cmd/status.go | 19 +++++++++++-------- cnquery.go | 8 ++++---- cnquery_test.go | 4 +++- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/apps/cnquery/cmd/status.go b/apps/cnquery/cmd/status.go index 95c7c5a767..5fc66cc3a7 100644 --- a/apps/cnquery/cmd/status.go +++ b/apps/cnquery/cmd/status.go @@ -78,6 +78,13 @@ Status sends a ping to Mondoo Platform to verify the credentials. } s.Upstream = upstreamStatus + latestVersion, err := cnquery.GetLatestVersion(httpClient) + if err != nil { + return cli_errors.NewCommandError(errors.Wrap(err, "failed to get latest version"), 1) + } + + s.Client.LatestVersion = latestVersion + // check valid agent authentication plugins := []ranger.ClientPlugin{} @@ -139,6 +146,7 @@ type ClientStatus struct { ServiceAccount string `json:"service_account,omitempty"` ParentMrn string `json:"parentMrn,omitempty"` Version string `json:"version,omitempty"` + LatestVersion string `json:"latest_version,omitempty"` Build string `json:"build,omitempty"` Labels map[string]string `json:"labels,omitempty"` Platform *inventory.Platform `json:"platform,omitempty"` @@ -162,15 +170,10 @@ func (s Status) RenderCliStatus() { log.Info().Msg("Time:\t\t\t" + s.Client.Timestamp) log.Info().Msg("Version:\t\t" + cnquery.GetVersion() + " (API Version: " + cnquery.APIVersion() + ")") - latestVersion, err := cnquery.GetLatestVersion() - if err != nil { - log.Warn().Err(err).Msg("failed to get latest version") - } - - if latestVersion != "" { - log.Info().Msg("Latest Version:\t" + latestVersion) + if s.Client.LatestVersion != "" { + log.Info().Msg("Latest Version:\t" + s.Client.LatestVersion) - if cnquery.GetVersion() != latestVersion && cnquery.GetVersion() != "unstable" { + if cnquery.GetVersion() != s.Client.LatestVersion && cnquery.GetVersion() != "unstable" { log.Warn().Msg("A newer version is available") } } diff --git a/cnquery.go b/cnquery.go index 220e82176d..22b8f7f29d 100644 --- a/cnquery.go +++ b/cnquery.go @@ -55,8 +55,8 @@ type Release struct { var cnqueryLatestReleaseUrl = "https://releases.mondoo.com/cnquery/latest.json?ignoreCache=1" // GetLatestReleaseName fetches the name of the latest release from releases.mondoo.com -func GetLatestReleaseName(url string) (string, error) { - resp, err := http.Get(url) +func GetLatestReleaseName(releaseUrl string, client *http.Client) (string, error) { + resp, err := client.Get(releaseUrl) if err != nil { return "", fmt.Errorf("error fetching latest release: %v", err) } @@ -80,8 +80,8 @@ func GetLatestReleaseName(url string) (string, error) { } // GetLatestVersion returns the latest version available on releases.mondoo.com -func GetLatestVersion() (string, error) { - releaseName, err := GetLatestReleaseName(cnqueryLatestReleaseUrl) +func GetLatestVersion(client *http.Client) (string, error) { + releaseName, err := GetLatestReleaseName(cnqueryLatestReleaseUrl, client) if err != nil { return "", err } diff --git a/cnquery_test.go b/cnquery_test.go index 251d6b1f3e..e24c8d1dd1 100644 --- a/cnquery_test.go +++ b/cnquery_test.go @@ -4,13 +4,15 @@ package cnquery import ( + "net/http" "testing" "github.com/stretchr/testify/assert" ) func TestGetLatestVersion(t *testing.T) { - version, err := GetLatestVersion() + client := &http.Client{} + version, err := GetLatestVersion(client) assert.NoError(t, err) assert.NotNil(t, version)