Skip to content
Merged
Show file tree
Hide file tree
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
62 changes: 62 additions & 0 deletions cmd/about.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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
}
4 changes: 4 additions & 0 deletions cmd/pre.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 17 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ 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()

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 := PreRun(); err != nil {
return
}
about, err := FetchAbout(&client)
if 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)
}
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"errors"
"fmt"
"os"
"pb/cmd"
Expand All @@ -28,8 +29,8 @@ import (
"github.com/spf13/cobra"
)

// populated at build time
var (
// populated at build time
Version string
Commit string
)
Expand All @@ -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")
},
}

Expand Down