Skip to content

Commit a66aaaf

Browse files
authored
Merge pull request cli#40 from github/version-flag
Add `--version` flag support
2 parents f0ca9aa + 2939924 commit a66aaaf

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
BUILD_FILES = $(shell go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}}\
22
{{end}}' ./...)
33

4-
# export GOFLAGS := -mod=vendor $GOFLAGS
4+
GH_VERSION = $(shell go describe --tags 2>/dev/null || git rev-parse --short HEAD)
5+
LDFLAGS := -X github.com/github/gh-cli/command.Version=$(GH_VERSION) $(LDFLAGS)
6+
LDFLAGS := -X github.com/github/gh-cli/command.BuildDate=$(shell date +%Y-%m-%d) $(LDFLAGS)
57

68
bin/gh: $(BUILD_FILES)
7-
go build -o "$@"
9+
@go build -ldflags "$(LDFLAGS)" -o "$@"
810

911
test:
1012
go test ./...

command/root.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ import (
66

77
"github.com/github/gh-cli/api"
88
"github.com/github/gh-cli/context"
9-
"github.com/github/gh-cli/version"
109

1110
"github.com/spf13/cobra"
1211
)
1312

13+
// Version is dynamically set at build time in the Makefile
14+
var Version = "DEV"
15+
16+
// BuildDate is dynamically set at build time in the Makefile
17+
var BuildDate = "YYYY-MM-DD"
18+
1419
func init() {
20+
RootCmd.Version = fmt.Sprintf("%s (%s)", Version, BuildDate)
1521
RootCmd.PersistentFlags().StringP("repo", "R", "", "current GitHub repository")
1622
RootCmd.PersistentFlags().StringP("current-branch", "B", "", "current git branch")
1723
// TODO:
@@ -65,7 +71,7 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
6571
}
6672
opts := []api.ClientOption{
6773
api.AddHeader("Authorization", fmt.Sprintf("token %s", token)),
68-
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", version.Version)),
74+
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)),
6975
}
7076
if verbose := os.Getenv("DEBUG"); verbose != "" {
7177
opts = append(opts, api.VerboseLog(os.Stderr))

version/version.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)