Skip to content

Commit

Permalink
add json output
Browse files Browse the repository at this point in the history
  • Loading branch information
albertobruin committed Nov 6, 2024
1 parent e2f940d commit 8f853d4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ./
Expand All @@ -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: ./
Expand All @@ -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
Expand Down
59 changes: 59 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}
18 changes: 1 addition & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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),
},
}

Expand Down

0 comments on commit 8f853d4

Please sign in to comment.