From 8f853d4deec9718efa3e536830cee6e81991100f Mon Sep 17 00:00:00 2001 From: "Alberto J. Gomez" Date: Wed, 6 Nov 2024 11:29:52 +0100 Subject: [PATCH] add json output --- .goreleaser.yaml | 6 ++--- cmd/version.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 18 +-------------- 3 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 cmd/version.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 0a3d8b0d..3dae7a19 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -34,7 +34,7 @@ builds: flags: - -trimpath ldflags: - - -s -w -X main.version={{ .Env.VERSION }} + - -s -w -X main.version={{ .Env.VERSION }} -X main.commit={{ .Env.COMMIT_SHA }} - id: bruin-linux-arm64 binary: bruin main: ./ @@ -49,7 +49,7 @@ builds: flags: - -trimpath ldflags: - - -s -w -X main.version={{ .Env.VERSION }}" + - -s -w -X main.version={{ .Env.VERSION }} -X main.commit={{ .Env.COMMIT_SHA }} - id: bruin-windows-amd64 binary: bruin main: ./ @@ -62,7 +62,7 @@ builds: - CXX=x86_64-w64-mingw32-g++ - CGO_ENABLED=1 ldflags: - - -s -w -X main.version={{ .Env.VERSION }}" + - -s -w -X main.version={{ .Env.VERSION }} -X main.commit={{ .Env.COMMIT_SHA }} flags: - -trimpath - -buildmode=exe diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 00000000..9bf9baee --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,59 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/pkg/errors" + "github.com/urfave/cli/v2" +) + +func VersionCmd(commit string) *cli.Command { + return &cli.Command{ + Name: "version", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "output", + Aliases: []string{"o"}, + Usage: "the output type, possible values are: plain, json", + }, + }, + Action: func(c *cli.Context) error { + outputFormat := c.String("output") + version := c.App.Version + + res, err := http.Get("https://github.com/bruin-data/bruin/releases/latest") //nolint + defer res.Body.Close() //nolint + if err != nil { + return errors.Wrap(err, "failed to check the latest version") + } + latest := strings.TrimPrefix(res.Request.URL.String(), "https://github.com/bruin-data/bruin/releases/tag/") + + if outputFormat == "json" { + output := struct { + Version string `json:"version"` + Commit string `json:"commit"` + Latest string `json:"latest"` + }{ + Version: version, + Commit: commit, + Latest: latest, + } + + outputString, err := json.Marshal(output) + if err != nil { + return errors.Wrap(err, "failed to marshal the output") + } + fmt.Println(string(outputString)) + + return nil + } + + fmt.Printf("Current version: %s (%s)\n", c.App.Version, commit) + fmt.Println("Latest version: " + latest) + return nil + }, + } +} diff --git a/main.go b/main.go index ca1a06d4..7abf54ed 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,12 @@ package main import ( "fmt" - "net/http" "os" "runtime/debug" - "strings" "time" "github.com/bruin-data/bruin/cmd" "github.com/fatih/color" - "github.com/pkg/errors" "github.com/urfave/cli/v2" ) @@ -66,20 +63,7 @@ func main() { cmd.Environments(&isDebug), cmd.Connections(), cmd.Fetch(), - &cli.Command{ - Name: "version", - Action: func(c *cli.Context) error { - fmt.Printf("Current version: %s (%s)\n", c.App.Version, commit) - res, err := http.Get("https://github.com/bruin-data/bruin/releases/latest") //nolint - defer res.Body.Close() //nolint - if err != nil { - return errors.Wrap(err, "failed to check the latest version") - } - - fmt.Println("Latest version: " + strings.TrimPrefix(res.Request.URL.String(), "https://github.com/bruin-data/bruin/releases/tag/")) - return nil - }, - }, + cmd.VersionCmd(commit), }, }