Skip to content

Commit ca11011

Browse files
authored
Merge pull request bruin-data#144 from bruin-data/patch/add-version
Patch/add version
2 parents 7bf08c6 + 97f626c commit ca11011

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

main.go

+34-11
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,48 @@ package main
22

33
import (
44
"fmt"
5+
"net/http"
56
"os"
67
"runtime/debug"
8+
"strings"
79
"time"
810

911
"github.com/bruin-data/bruin/cmd"
1012
"github.com/fatih/color"
13+
"github.com/pkg/errors"
1114
"github.com/urfave/cli/v2"
1215
)
1316

14-
var Version = "development"
17+
var (
18+
version = "dev"
19+
commit = ""
20+
)
1521

1622
func main() {
1723
isDebug := false
1824
color.NoColor = false
1925

2026
cli.VersionPrinter = func(cCtx *cli.Context) {
21-
var commit = func() string {
22-
if info, ok := debug.ReadBuildInfo(); ok {
23-
for _, setting := range info.Settings {
24-
if setting.Key == "vcs.revision" {
25-
return setting.Value
27+
hash := commit
28+
if hash == "" {
29+
hash = func() string {
30+
if info, ok := debug.ReadBuildInfo(); ok {
31+
for _, setting := range info.Settings {
32+
if setting.Key == "vcs.revision" {
33+
return setting.Value
34+
}
2635
}
2736
}
28-
}
29-
return ""
30-
}()
37+
return ""
38+
}()
39+
}
3140

32-
fmt.Printf("bruin CLI %s (%s)\n", cCtx.App.Version, commit)
41+
fmt.Printf("bruin CLI %s (%s)\n", cCtx.App.Version, hash)
3342
}
3443

3544
app := &cli.App{
3645
Name: "bruin",
37-
Version: Version,
46+
Version: version,
3847
Usage: "The CLI used for managing Bruin-powered data pipelines",
3948
Compiled: time.Now(),
4049
Flags: []cli.Flag{
@@ -57,6 +66,20 @@ func main() {
5766
cmd.Environments(&isDebug),
5867
cmd.Connections(),
5968
cmd.Fetch(),
69+
&cli.Command{
70+
Name: "version",
71+
Action: func(c *cli.Context) error {
72+
fmt.Printf("Current version: %s (%s)\n", c.App.Version, commit)
73+
res, err := http.Get("https://github.com/bruin-data/bruin/releases/latest") //nolint
74+
defer res.Body.Close() //nolint
75+
if err != nil {
76+
return errors.Wrap(err, "failed to check the latest version")
77+
}
78+
79+
fmt.Println("Latest version: " + strings.TrimPrefix(res.Request.URL.String(), "https://github.com/bruin-data/bruin/releases/tag/"))
80+
return nil
81+
},
82+
},
6083
},
6184
}
6285

0 commit comments

Comments
 (0)