Skip to content

Commit

Permalink
feat: add version command
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst committed Aug 18, 2024
1 parent eb57506 commit faf35a6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions cmd/ayo/config/config.go
Original file line number Diff line number Diff line change
@@ -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.
)
3 changes: 2 additions & 1 deletion cmd/ayo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
34 changes: 34 additions & 0 deletions cmd/ayo/version.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit faf35a6

Please sign in to comment.