Skip to content

Commit cd34d23

Browse files
authored
Version command shows server version (#13)
1 parent 1e889c4 commit cd34d23

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

cmd/about.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2023 Cloudnatively Services Pvt Ltd
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
package cmd
17+
18+
import (
19+
"encoding/json"
20+
"errors"
21+
"fmt"
22+
"io"
23+
)
24+
25+
type About struct {
26+
Commit string `json:"commit"`
27+
DeploymentID string `json:"deploymentId"`
28+
LatestVersion any `json:"latestVersion"`
29+
License string `json:"license"`
30+
Mode string `json:"mode"`
31+
Staging string `json:"staging"`
32+
Store string `json:"store"`
33+
UpdateAvailable bool `json:"updateAvailable"`
34+
Version string `json:"version"`
35+
}
36+
37+
func FetchAbout(client *HTTPClient) (about About, err error) {
38+
req, err := client.NewRequest("GET", "about", nil)
39+
if err != nil {
40+
return
41+
}
42+
43+
resp, err := client.client.Do(req)
44+
if err != nil {
45+
return
46+
}
47+
48+
bytes, err := io.ReadAll(resp.Body)
49+
if err != nil {
50+
return
51+
}
52+
defer resp.Body.Close()
53+
54+
if resp.StatusCode == 200 {
55+
err = json.Unmarshal(bytes, &about)
56+
} else {
57+
body := string(bytes)
58+
body = fmt.Sprintf("Request Failed\nStatus Code: %s\nResponse: %s\n", resp.Status, body)
59+
err = errors.New(body)
60+
}
61+
return
62+
}

cmd/pre.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ var DefaultProfile config.Profile
2929
// PreRunDefaultProfile if a profile exists.
3030
// This is required by mostly all commands except profile
3131
func PreRunDefaultProfile(_ *cobra.Command, _ []string) error {
32+
return PreRun()
33+
}
34+
35+
func PreRun() error {
3236
conf, err := config.ReadConfigFromFile()
3337
if os.IsNotExist(err) {
3438
return errors.New("no config found to run this command. add a profile using pb profile command")

cmd/version.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ var VersionCmd = &cobra.Command{
3131

3232
// PrintVersion prints version information
3333
func PrintVersion(version, commit string) {
34-
fmt.Printf("\n%s \n\n", standardStyleAlt.Render("pb version"))
35-
fmt.Printf(" %s %s\n", standardStyleBold.Render("version: "), version)
36-
fmt.Printf(" %s %s\n\n", standardStyleBold.Render("commit: "), commit)
34+
client := DefaultClient()
35+
36+
fmt.Printf("\n%s \n", standardStyleAlt.Render("pb version"))
37+
fmt.Printf("- %s %s\n", standardStyleBold.Render("version: "), version)
38+
fmt.Printf("- %s %s\n\n", standardStyleBold.Render("commit: "), commit)
39+
40+
if err := PreRun(); err != nil {
41+
return
42+
}
43+
about, err := FetchAbout(&client)
44+
if err != nil {
45+
return
46+
}
47+
48+
fmt.Printf("%s %s \n", standardStyleAlt.Render("Connected to"), standardStyleBold.Render(DefaultProfile.URL))
49+
fmt.Printf("- %s %s\n", standardStyleBold.Render("version: "), about.Version)
50+
fmt.Printf("- %s %s\n\n", standardStyleBold.Render("commit: "), about.Commit)
3751
}

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
2223
"pb/cmd"
@@ -28,8 +29,8 @@ import (
2829
"github.com/spf13/cobra"
2930
)
3031

32+
// populated at build time
3133
var (
32-
// populated at build time
3334
Version string
3435
Commit string
3536
)
@@ -55,10 +56,13 @@ var cli = &cobra.Command{
5556
Use: "pb",
5657
Short: "\nParseable command line tool",
5758
Long: "\npb is a command line tool for Parseable",
58-
Run: func(command *cobra.Command, args []string) {
59+
RunE: func(command *cobra.Command, args []string) error {
5960
if p, _ := command.Flags().GetBool(versionFlag); p {
6061
cmd.PrintVersion(Version, Commit)
62+
return nil
6163
}
64+
65+
return errors.New("No command or flag supplied\n")
6266
},
6367
}
6468

0 commit comments

Comments
 (0)