diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 3a460c32..7a020576 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -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 @@ -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 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), }, }