From 168cea0690c3d15ae07d633fa9aa2d8e7c24ea4b Mon Sep 17 00:00:00 2001 From: "Alberto J. Gomez" Date: Mon, 4 Nov 2024 12:12:20 +0100 Subject: [PATCH 1/2] add version command --- main.go | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 258016c1..33babe08 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( "fmt" + "net/http" "os" "runtime/debug" + "strings" "time" "github.com/bruin-data/bruin/cmd" @@ -11,30 +13,36 @@ import ( "github.com/urfave/cli/v2" ) -var Version = "development" +var ( + version = "dev" + commit = "" +) func main() { isDebug := false color.NoColor = false cli.VersionPrinter = func(cCtx *cli.Context) { - var commit = func() string { - if info, ok := debug.ReadBuildInfo(); ok { - for _, setting := range info.Settings { - if setting.Key == "vcs.revision" { - return setting.Value + hash := commit + if hash == "" { + hash = func() string { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } } } - } - return "" - }() + return "" + }() + } - fmt.Printf("bruin CLI %s (%s)\n", cCtx.App.Version, commit) + fmt.Printf("bruin CLI %s (%s)\n", cCtx.App.Version, hash) } app := &cli.App{ Name: "bruin", - Version: Version, + Version: version, Usage: "The CLI used for managing Bruin-powered data pipelines", Compiled: time.Now(), Flags: []cli.Flag{ @@ -57,6 +65,20 @@ 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") + if err != nil { + fmt.Println("Failed to check the latest version: " + err.Error()) + return nil + } + + fmt.Println("Latest version: " + strings.TrimPrefix(res.Request.URL.String(), "https://github.com/bruin-data/bruin/releases/tag/")) + return nil + }, + }, }, } From 97f626cc3d213ef9a2af0a180dc6223fc8cffb71 Mon Sep 17 00:00:00 2001 From: "Alberto J. Gomez" Date: Mon, 4 Nov 2024 12:13:17 +0100 Subject: [PATCH 2/2] add version command --- main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 33babe08..ca1a06d4 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/bruin-data/bruin/cmd" "github.com/fatih/color" + "github.com/pkg/errors" "github.com/urfave/cli/v2" ) @@ -69,10 +70,10 @@ func main() { 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") + res, err := http.Get("https://github.com/bruin-data/bruin/releases/latest") //nolint + defer res.Body.Close() //nolint if err != nil { - fmt.Println("Failed to check the latest version: " + err.Error()) - return 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/"))