Skip to content

Commit 2aa5214

Browse files
authored
Versioning improvements (#17)
* Versioning improvements * fix install script
1 parent 60112ac commit 2aa5214

6 files changed

Lines changed: 80 additions & 14 deletions

File tree

cli/root.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ func init() {
8484

8585
// Execute runs the root command via Fang.
8686
func Execute(version, commit, date string) {
87-
rootCmd.Version = fmt.Sprintf("%s (commit: %s, built: %s)", version, commit, date)
87+
versionStr := fmt.Sprintf("%s (commit: %s, built: %s)", version, commit, date)
8888

89-
if err := fang.Execute(context.Background(), rootCmd); err != nil {
89+
if err := fang.Execute(context.Background(), rootCmd,
90+
fang.WithVersion(versionStr),
91+
); err != nil {
9092
fmt.Fprintln(os.Stderr, err)
9193
os.Exit(1)
9294
}

cmd/main.go

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,68 @@
11
package main
22

3-
import "github.com/duneanalytics/cli/cli"
3+
import (
4+
"runtime/debug"
5+
"strings"
46

5-
// Set by GoReleaser via ldflags.
7+
"github.com/duneanalytics/cli/cli"
8+
)
9+
10+
// Set by GoReleaser or Makefile via ldflags.
611
var (
7-
version = "dev"
8-
commit = "none"
9-
date = "unknown"
12+
version = ""
13+
commit = ""
14+
date = ""
1015
)
1116

1217
func main() {
18+
resolveVersion()
1319
cli.Execute(version, commit, date)
1420
}
21+
22+
// resolveVersion fills in version/commit/date from Go build info
23+
// when they haven't been set via ldflags (i.e. plain go build / go install).
24+
func resolveVersion() {
25+
info, ok := debug.ReadBuildInfo()
26+
if !ok {
27+
setDefaults()
28+
return
29+
}
30+
31+
// For go install ...@vX.Y.Z, the module version is clean (e.g. "v0.0.2").
32+
// For local builds it is "(devel)" — not useful.
33+
if version == "" {
34+
if v := info.Main.Version; v != "" && v != "(devel)" && !strings.Contains(v, "-") {
35+
version = strings.TrimPrefix(v, "v")
36+
}
37+
}
38+
39+
for _, s := range info.Settings {
40+
switch s.Key {
41+
case "vcs.revision":
42+
if commit == "" && s.Value != "" {
43+
commit = s.Value
44+
if len(commit) > 12 {
45+
commit = commit[:12]
46+
}
47+
}
48+
case "vcs.time":
49+
if date == "" && s.Value != "" {
50+
date = s.Value
51+
}
52+
}
53+
}
54+
55+
setDefaults()
56+
}
57+
58+
func setDefaults() {
59+
if version == "" {
60+
version = "dev"
61+
}
62+
if commit == "" {
63+
commit = "unknown"
64+
}
65+
if date == "" {
66+
date = "unknown"
67+
}
68+
}

cmd/query/archive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func newArchiveCmd() *cobra.Command {
1515
Long: "Mark a Dune query as archived. Archived queries are hidden from the library\n" +
1616
"but can still be retrieved by ID. You must own the query or have edit access\n" +
1717
"via team membership.",
18-
Args: cobra.ExactArgs(1),
19-
RunE: runArchive,
18+
Args: cobra.ExactArgs(1),
19+
RunE: runArchive,
2020
}
2121

2222
output.AddFormatFlag(cmd, "text")

cmd/query/get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func newGetCmd() *cobra.Command {
1515
Short: "Fetch a saved Dune query by ID, including SQL and metadata",
1616
Long: "Retrieve SQL, metadata, owner, privacy flags, tags, and execution state for\n" +
1717
"an existing Dune query. Only queries visible to the authenticated user are returned.",
18-
Args: cobra.ExactArgs(1),
19-
RunE: runGet,
18+
Args: cobra.ExactArgs(1),
19+
RunE: runGet,
2020
}
2121

2222
output.AddFormatFlag(cmd, "text")

makefile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
.PHONY: all setup build lint test
1+
.PHONY: all setup build lint test install
2+
3+
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null | sed 's/^v//')
4+
COMMIT ?= $(shell git rev-parse --short=12 HEAD 2>/dev/null || echo "unknown")
5+
DATE ?= $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
6+
LDFLAGS = -s -w \
7+
-X main.version=$(VERSION) \
8+
-X main.commit=$(COMMIT) \
9+
-X main.date=$(DATE)
210

311
all: lint test build
412

513
setup: bin/golangci-lint
614
go mod download
715

816
dune-cli: lint
9-
go build -o dune-cli cmd/main.go
17+
go build -ldflags '$(LDFLAGS)' -o dune-cli ./cmd
1018

1119
build: dune-cli
1220

21+
install:
22+
go build -ldflags '$(LDFLAGS)' -o $(shell go env GOPATH)/bin/dune ./cmd
23+
1324
bin:
1425
mkdir -p bin
1526

output/output.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,3 @@ func FormatBytes(b int64) string {
7676
return fmt.Sprintf("%d B", b)
7777
}
7878
}
79-

0 commit comments

Comments
 (0)