From 4ef9d682f994841386a804f35e859ed19f5d2b02 Mon Sep 17 00:00:00 2001 From: Satyam Singh Date: Mon, 21 Aug 2023 13:33:59 +0530 Subject: [PATCH 1/2] Version command shows server version as well --- cmd/about.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/pre.go | 4 ++++ cmd/version.go | 21 ++++++++++++++--- main.go | 8 +++++-- 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 cmd/about.go diff --git a/cmd/about.go b/cmd/about.go new file mode 100644 index 0000000..60b428b --- /dev/null +++ b/cmd/about.go @@ -0,0 +1,62 @@ +// Copyright (c) 2023 Cloudnatively Services Pvt Ltd +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "encoding/json" + "errors" + "fmt" + "io" +) + +type About struct { + Commit string `json:"commit"` + DeploymentID string `json:"deploymentId"` + LatestVersion any `json:"latestVersion"` + License string `json:"license"` + Mode string `json:"mode"` + Staging string `json:"staging"` + Store string `json:"store"` + UpdateAvailable bool `json:"updateAvailable"` + Version string `json:"version"` +} + +func FetchAbout(client *HTTPClient) (about About, err error) { + req, err := client.NewRequest("GET", "about", nil) + if err != nil { + return + } + + resp, err := client.client.Do(req) + if err != nil { + return + } + + bytes, err := io.ReadAll(resp.Body) + if err != nil { + return + } + defer resp.Body.Close() + + if resp.StatusCode == 200 { + err = json.Unmarshal(bytes, &about) + } else { + body := string(bytes) + body = fmt.Sprintf("Request Failed\nStatus Code: %s\nResponse: %s\n", resp.Status, body) + err = errors.New(body) + } + return +} diff --git a/cmd/pre.go b/cmd/pre.go index 6b969b0..713d86c 100644 --- a/cmd/pre.go +++ b/cmd/pre.go @@ -29,6 +29,10 @@ var DefaultProfile config.Profile // PreRunDefaultProfile if a profile exists. // This is required by mostly all commands except profile func PreRunDefaultProfile(_ *cobra.Command, _ []string) error { + return PreRun() +} + +func PreRun() error { conf, err := config.ReadConfigFromFile() if os.IsNotExist(err) { return errors.New("no config found to run this command. add a profile using pb profile command") diff --git a/cmd/version.go b/cmd/version.go index 550507e..e040ee4 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -31,7 +31,22 @@ var VersionCmd = &cobra.Command{ // PrintVersion prints version information func PrintVersion(version, commit string) { - fmt.Printf("\n%s \n\n", standardStyleAlt.Render("pb version")) - fmt.Printf(" %s %s\n", standardStyleBold.Render("version: "), version) - fmt.Printf(" %s %s\n\n", standardStyleBold.Render("commit: "), commit) + client := DefaultClient() + about, err := FetchAbout(&client) + + fmt.Printf("\n%s \n", standardStyleAlt.Render("pb version")) + fmt.Printf("- %s %s\n", standardStyleBold.Render("version: "), version) + fmt.Printf("- %s %s\n\n", standardStyleBold.Render("commit: "), commit) + + if err != nil { + return + } + + if err := PreRun(); err != nil { + return + } + + fmt.Printf("%s %s \n", standardStyleAlt.Render("Connected to"), standardStyleBold.Render(DefaultProfile.URL)) + fmt.Printf("- %s %s\n", standardStyleBold.Render("version: "), about.Version) + fmt.Printf("- %s %s\n\n", standardStyleBold.Render("commit: "), about.Commit) } diff --git a/main.go b/main.go index 32953c3..61f29a1 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ package main import ( + "errors" "fmt" "os" "pb/cmd" @@ -28,8 +29,8 @@ import ( "github.com/spf13/cobra" ) +// populated at build time var ( - // populated at build time Version string Commit string ) @@ -55,10 +56,13 @@ var cli = &cobra.Command{ Use: "pb", Short: "\nParseable command line tool", Long: "\npb is a command line tool for Parseable", - Run: func(command *cobra.Command, args []string) { + RunE: func(command *cobra.Command, args []string) error { if p, _ := command.Flags().GetBool(versionFlag); p { cmd.PrintVersion(Version, Commit) + return nil } + + return errors.New("No command or flag supplied\n") }, } From a233154ad92d4b6ce5c0a658f5c31fb8befa1cc9 Mon Sep 17 00:00:00 2001 From: Satyam Singh Date: Mon, 21 Aug 2023 14:14:47 +0530 Subject: [PATCH 2/2] Fix --- cmd/version.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/version.go b/cmd/version.go index e040ee4..65ea42f 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -32,17 +32,16 @@ var VersionCmd = &cobra.Command{ // PrintVersion prints version information func PrintVersion(version, commit string) { client := DefaultClient() - about, err := FetchAbout(&client) fmt.Printf("\n%s \n", standardStyleAlt.Render("pb version")) fmt.Printf("- %s %s\n", standardStyleBold.Render("version: "), version) fmt.Printf("- %s %s\n\n", standardStyleBold.Render("commit: "), commit) - if err != nil { + if err := PreRun(); err != nil { return } - - if err := PreRun(); err != nil { + about, err := FetchAbout(&client) + if err != nil { return }