Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ func init() {

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

if err := fang.Execute(context.Background(), rootCmd); err != nil {
if err := fang.Execute(context.Background(), rootCmd,
fang.WithVersion(versionStr),
); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand Down
64 changes: 59 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
package main

import "github.com/duneanalytics/cli/cli"
import (
"runtime/debug"
"strings"

// Set by GoReleaser via ldflags.
"github.com/duneanalytics/cli/cli"
)

// Set by GoReleaser or Makefile via ldflags.
var (
version = "dev"
commit = "none"
date = "unknown"
version = ""
commit = ""
date = ""
)

func main() {
resolveVersion()
cli.Execute(version, commit, date)
}

// resolveVersion fills in version/commit/date from Go build info
// when they haven't been set via ldflags (i.e. plain go build / go install).
func resolveVersion() {
info, ok := debug.ReadBuildInfo()
if !ok {
setDefaults()
return
}

// For go install ...@vX.Y.Z, the module version is clean (e.g. "v0.0.2").
// For local builds it is "(devel)" — not useful.
if version == "" {
if v := info.Main.Version; v != "" && v != "(devel)" && !strings.Contains(v, "-") {
version = strings.TrimPrefix(v, "v")
}
}

for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
if commit == "" && s.Value != "" {
commit = s.Value
if len(commit) > 12 {
commit = commit[:12]
}
}
case "vcs.time":
if date == "" && s.Value != "" {
date = s.Value
}
}
}

setDefaults()
}

func setDefaults() {
if version == "" {
version = "dev"
}
if commit == "" {
commit = "unknown"
}
if date == "" {
date = "unknown"
}
}
4 changes: 2 additions & 2 deletions cmd/query/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func newArchiveCmd() *cobra.Command {
Long: "Mark a Dune query as archived. Archived queries are hidden from the library\n" +
"but can still be retrieved by ID. You must own the query or have edit access\n" +
"via team membership.",
Args: cobra.ExactArgs(1),
RunE: runArchive,
Args: cobra.ExactArgs(1),
RunE: runArchive,
}

output.AddFormatFlag(cmd, "text")
Expand Down
4 changes: 2 additions & 2 deletions cmd/query/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func newGetCmd() *cobra.Command {
Short: "Fetch a saved Dune query by ID, including SQL and metadata",
Long: "Retrieve SQL, metadata, owner, privacy flags, tags, and execution state for\n" +
"an existing Dune query. Only queries visible to the authenticated user are returned.",
Args: cobra.ExactArgs(1),
RunE: runGet,
Args: cobra.ExactArgs(1),
RunE: runGet,
}

output.AddFormatFlag(cmd, "text")
Expand Down
15 changes: 13 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
.PHONY: all setup build lint test
.PHONY: all setup build lint test install

VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null | sed 's/^v//')
COMMIT ?= $(shell git rev-parse --short=12 HEAD 2>/dev/null || echo "unknown")
DATE ?= $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
LDFLAGS = -s -w \
-X main.version=$(VERSION) \
-X main.commit=$(COMMIT) \
-X main.date=$(DATE)

all: lint test build

setup: bin/golangci-lint
go mod download

dune-cli: lint
go build -o dune-cli cmd/main.go
go build -ldflags '$(LDFLAGS)' -o dune-cli ./cmd

build: dune-cli

install:
go build -ldflags '$(LDFLAGS)' -o $(shell go env GOPATH)/bin/dune ./cmd

bin:
mkdir -p bin

Expand Down
1 change: 0 additions & 1 deletion output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,3 @@ func FormatBytes(b int64) string {
return fmt.Sprintf("%d B", b)
}
}