Skip to content

Commit

Permalink
Merge pull request bruin-data#159 from bruin-data/patch/version-fixes
Browse files Browse the repository at this point in the history
add json output
  • Loading branch information
albertobruin authored Nov 6, 2024
2 parents e2f940d + 0a106f2 commit d1cf92a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ github.ref_name }}
COMMIT_SHA: ${{ github.sha }}
- name: Use install script
shell: bash
run: curl -LsSf https://raw.githubusercontent.com/bruin-data/bruin/${{ github.sha }}/install.sh | sh -s -- -d
Expand All @@ -107,7 +108,7 @@ jobs:

- name: Run GoReleaser
run: |
docker run -e VERSION=0.0.0 -v $(pwd):/src -w /src goreleaser/goreleaser-cross:v1.22 build --snapshot --clean --id bruin-darwin --id bruin-linux-amd64 --id bruin-linux-arm64 --single-target
docker run -e VERSION=0.0.0 -e COMMIT_SHA=${{ github.sha }} -v $(pwd):/src -w /src goreleaser/goreleaser-cross:v1.22 build --snapshot --clean --id bruin-darwin --id bruin-linux-amd64 --id bruin-linux-arm64 --single-target
# - name: Use install script
# run: curl -LsSf https://raw.githubusercontent.com/bruin-data/bruin/${{ github.sha }}/install.sh | sh -s -- -d
# - name: Test Pipeline
Expand Down
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 d1cf92a

Please sign in to comment.