From faf35a647cc45e87b69ff5ff656ab842da2df8cb Mon Sep 17 00:00:00 2001 From: banst Date: Sun, 18 Aug 2024 19:21:58 +0200 Subject: [PATCH] feat: add version command --- .goreleaser.yaml | 6 ++++++ Makefile | 13 ++++++++++++- cmd/ayo/config/config.go | 8 ++++++++ cmd/ayo/main.go | 3 ++- cmd/ayo/version.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 cmd/ayo/config/config.go create mode 100644 cmd/ayo/version.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index f2f08a3..3e03b15 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -19,6 +19,12 @@ builds: - env: - CGO_ENABLED=0 main: ./cmd/ayo/ + ldflags: + - -s -w + - -X 'github.com/banst/ayo/cmd/ayo/config.Version={{.Version}}' + - -X 'github.com/banst/ayo/cmd/ayo/config.Commit={{.Commit}}' + - -X 'github.com/banst/ayo/cmd/ayo/config.Date={{.Date}}' + - -X 'github.com/banst/ayo/cmd/ayo/config.BuiltBy=goreleaser' goos: - linux - windows diff --git a/Makefile b/Makefile index a14ab80..36d6918 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,17 @@ BUILD_DIR=dist # Coverage file name COVERAGE_FILE=$(BUILD_DIR)/coverage.out +project_version := $(shell git describe --tags --always) +git_commit := $(shell git rev-parse --verify HEAD) +date := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ') +author := $(USER) + +LDFLAGS := -s -w +LDFLAGS += -X 'github.com/banst/ayo/cmd/ayo/config.Version=$(project_version)' +LDFLAGS += -X 'github.com/banst/ayo/cmd/ayo/config.Commit=$(git_commit)' +LDFLAGS += -X 'github.com/banst/ayo/cmd/ayo/config.Date=$(date)' +LDFLAGS += -X 'github.com/banst/ayo/cmd/ayo/config.BuiltBy=$(author)' + # Default target .PHONY: all all: build @@ -15,7 +26,7 @@ all: build .PHONY: build build-go: ## Build the Go project @mkdir -p $(BUILD_DIR) - go build -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/ayo + go build -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/ayo # Build the Documentation .PHONY: build-docs diff --git a/cmd/ayo/config/config.go b/cmd/ayo/config/config.go new file mode 100644 index 0000000..48224c6 --- /dev/null +++ b/cmd/ayo/config/config.go @@ -0,0 +1,8 @@ +package config + +var ( + Version = "devel" //nolint: gochecknoglobals // Build-time variable. + Commit = "none" //nolint: gochecknoglobals // Build-time variable. + Date = "unknown" //nolint: gochecknoglobals // Build-time variable. + BuiltBy = "unknown" //nolint: gochecknoglobals // Build-time variable. +) diff --git a/cmd/ayo/main.go b/cmd/ayo/main.go index f5d9e24..1b402d6 100644 --- a/cmd/ayo/main.go +++ b/cmd/ayo/main.go @@ -15,7 +15,8 @@ type CLI struct { LogFormat string `help:"Log format to use." enum:"json,pretty,logfmt" default:"pretty"` Toolbox string `help:"The directory to load tools from" default:"~/.config/ayo/toolbox" type:"existingdir" env:"AYO_TOOLBOX"` //nolint: lll - Chat Chat `cmd:"" help:"Send a message to the chatbot" default:"withargs"` + Chat Chat `cmd:"" help:"Send a message to the chatbot" default:"withargs"` + Version Version `cmd:"" help:"Show the version information"` } func main() { diff --git a/cmd/ayo/version.go b/cmd/ayo/version.go new file mode 100644 index 0000000..c33d7b1 --- /dev/null +++ b/cmd/ayo/version.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "text/tabwriter" + + "github.com/alecthomas/kong" + + "github.com/banst/ayo/cmd/ayo/config" +) + +// Version represents the version command. +type Version struct { + Short bool `help:"Print just the semver version." short:"s"` +} + +// Run runs the version command. +func (v *Version) Run(ctx *kong.Context) error { + if v.Short { + _, err := fmt.Fprintln(ctx.Stdout, config.Version) + if err != nil { + return err + } + return nil + } + + w := tabwriter.NewWriter(ctx.Stdout, 0, 0, 3, ' ', 0) + format := "Version:\t%s\nCommit:\t%s\nDate:\t%s\nBuilt by:\t%s\n" + _, err := fmt.Fprintf(w, format, config.Version, config.Commit, config.Date, config.BuiltBy) + if err != nil { + return err + } + return w.Flush() +}