feat: -v --version version (#263)

Drew Smirnoff created

Change summary

.github/labeler-config.yml |  2 +-
Makefile                   | 12 +++++++++++-
main.go                    | 13 +++++++++++++
3 files changed, 25 insertions(+), 2 deletions(-)

Detailed changes

.github/labeler-config.yml 🔗

@@ -1,4 +1,4 @@
-"bug": '\b([Bb]ug(s)?|[Ee]rror(s)?|[Ff]ix(es)?|[Ii]ssue(s)?)\b'
+"bug": '\b([Bb]ug(s)?|[Ee]rror(s)?|[Ff]ix(es)?)\b'
 
 "enhancement": '\b([Ee]nhancement(s)?|[Ff]eature(s)?|[Ii]dea(s)?|[Ss]uggestion(s)?[Ff]eat(s)?)\b'
 

Makefile 🔗

@@ -1,4 +1,4 @@
-.PHONY: build test run clean lint fmt vet
+.PHONY: build test run clean lint fmt vet build-full
 
 BINARY_NAME=matcha
 BUILD_DIR=bin
@@ -11,6 +11,16 @@ generate_gif:
 build:
 	go build -o $(BUILD_DIR)/$(BINARY_NAME) .
 
+build-full:
+	@echo "Building with version information..."
+	@VERSION=$$(git describe --tags --abbrev=0 2>/dev/null || echo "dev"); \
+	COMMIT=$$(git rev-parse --short HEAD 2>/dev/null || echo "unknown"); \
+	DATE=$$(date +%Y-%m-%d); \
+	echo "Version: $$VERSION"; \
+	echo "Commit: $$COMMIT"; \
+	echo "Date: $$DATE"; \
+	go build -ldflags="-X 'main.version=$$VERSION' -X 'main.commit=$$COMMIT' -X 'main.date=$$DATE'" -o $(BUILD_DIR)/$(BINARY_NAME)-full .;
+
 run:
 	go run .
 

main.go 🔗

@@ -1952,6 +1952,19 @@ func filterUnique(existing, incoming []fetcher.Email) []fetcher.Email {
 }
 
 func main() {
+	// If invoked with version flag, print version and exit
+	if len(os.Args) > 1 && (os.Args[1] == "-v" || os.Args[1] == "--version" || os.Args[1] == "version") {
+		fmt.Printf("matcha version %s", version)
+		if commit != "" {
+			fmt.Printf(" (%s)", commit)
+		}
+		if date != "" {
+			fmt.Printf(" built on %s", date)
+		}
+		fmt.Println()
+		os.Exit(0)
+	}
+
 	// If invoked as CLI update command, run updater and exit.
 	if len(os.Args) > 1 && os.Args[1] == "update" {
 		if err := runUpdateCLI(); err != nil {