diff --git a/Makefile b/Makefile index 1d0cdda..8214b4e 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S') GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown") DEB_PACKAGE_NAME := $(BINARY_NAME)_$(VERSION)_amd64.deb +PKG_PATH=git.runcible.io/${REPO_OWNER}/${REPO_NAME}/cmd # Default target .PHONY: all @@ -16,7 +17,7 @@ all: build build: @echo "Building $(BINARY_NAME) version $(VERSION)..." @mkdir -p $(BUILD_DIR) - @go build -ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT)" \ + @go build -ldflags "-X $(PKG_PATH).Version=$(VERSION) -X $(PKG_PATH).BuildTime=$(BUILD_TIME) -X $(PKG_PATH).GitCommit=$(GIT_COMMIT)" \ -o $(BUILD_DIR)/$(BINARY_NAME) . # Clean build artifacts diff --git a/cmd/root.go b/cmd/root.go index c7faf99..cedf8b0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -13,7 +13,7 @@ var rootCmd = &cobra.Command{ Long: `A longer description that spans multiple lines and likely contains examples and usage of using your application.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Hello from your new Go CLI!") + fmt.Println("Hello from your new Go CLI tool ${REPO_NAME}!") }, } diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..77de2a4 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,38 @@ +package cmd + +import ( + "fmt" + "runtime/debug" + + "github.com/spf13/cobra" +) + +// These variables are set at build time using ldflags +var ( + Version = "dev" // Represents the version of the application + BuildTime = "unknown" // The time the binary was built + GitCommit = "unknown" // The git commit hash +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number, build time, and git commit", + Long: `Displays the application's version, the time it was built, the git commit it was built from, and the Go version used.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("Version: %s\n", Version) + fmt.Printf("Build Time: %s\n", BuildTime) + fmt.Printf("Git Commit: %s\n", GitCommit) + + // Print Go version and other build info if available + if info, ok := debug.ReadBuildInfo(); ok { + fmt.Printf("Go Version: %s\n", info.GoVersion) + } + }, +} + +// init function is called by Go when the package is initialized. +// We use it to add the 'version' command to our root command. +func init() { + rootCmd.AddCommand(versionCmd) +}