Skip to content

Commit

Permalink
version command using ldflag
Browse files Browse the repository at this point in the history
  • Loading branch information
r33drichards committed Oct 29, 2021
1 parent 48d136b commit 01205a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ builds:
goarch:
- amd64
binary: brev
ldflags:
- -X github.com/brevdev/brev-cli/pkg/cmd/version.Version={{.Tag}}

brews:
- name: brev-cli
# GitHub/GitLab repository to push the formula to
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DEFAULT_GOAL := dev
VERSION := $(shell git tag | grep ^v | sort -V | tail -n 1)


.PHONY: dev
dev: ## dev build
Expand Down Expand Up @@ -66,7 +68,8 @@ build: install
.PHONY: fast-build
fast-build: ## go build -o brev
$(call print-target)
go build -o brev
go build -o brev -ldflags "-X github.com/brevdev/brev-cli/pkg/cmd/version.Version=${VERSION}"


.PHONY: release
release: ## goreleaser --rm-dist
Expand Down
18 changes: 5 additions & 13 deletions pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package version

import (
"fmt"
"runtime/debug"

"github.com/fatih/color"

"github.com/brevdev/brev-cli/pkg/config"
"github.com/brevdev/brev-cli/pkg/requests"
"github.com/brevdev/brev-cli/pkg/terminal"
)
Expand All @@ -15,6 +13,8 @@ const (
cliReleaseURL = "https://api.github.com/repos/brevdev/brev-cli/releases/latest"
)

var Version = ""

var green = color.New(color.FgGreen).SprintfFunc()

var upToDateString = `
Expand Down Expand Up @@ -45,30 +45,22 @@ type githubReleaseMetadata struct {
}

func BuildVersionString(t *terminal.Terminal) (string, error) {
info, ok := debug.ReadBuildInfo()
var currentVersion string
if ok {
currentVersion = info.Main.Version
} else {
currentVersion = config.GlobalConfig.GetVersion()
}

githubRelease, err := getLatestGithubReleaseMetadata()
if err != nil {
t.Errprint(err, "Failed to retrieve latest version")
return "", err
}

var versionString string
if githubRelease.TagName == currentVersion {
if githubRelease.TagName == Version {
versionString = fmt.Sprintf(
upToDateString,
currentVersion,
Version,
)
} else {
versionString = fmt.Sprintf(
outOfDateString,
currentVersion,
Version,
githubRelease.TagName,
githubRelease.Name,
githubRelease.Body,
Expand Down

0 comments on commit 01205a1

Please sign in to comment.