print version (#13)

Reviewed-on: #13
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-10-27 23:55:39 +00:00
committed by phoenix
parent 7e3fff50c1
commit 154c9f493e
4 changed files with 47 additions and 10 deletions
+21
View File
@@ -0,0 +1,21 @@
VERSION ?= $(shell git describe --tags 2>/dev/null || echo "dev")
COMMIT ?= $(shell git rev-parse --short HEAD)
BUILD_TIME ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
GO_VERSION ?= $(shell go version | awk '{print $$3}')
.PHONY: build
build:
go build -ldflags="\
-X 'git.kundeng.us/phoenix/clean_file/version.Version=$(VERSION)' \
-X 'git.kundeng.us/phoenix/clean_file/version.BuildTime=$(BUILD_TIME)' \
-X 'git.kundeng.us/phoenix/clean_file/version.Commit=$(COMMIT)' \
-X 'git.kundeng.us/phoenix/clean_file/version.GoVersion=$(GO_VERSION)'" \
-o clean_file main.go
.PHONY: install
install:
go install -ldflags="\
-X 'git.kundeng.us/phoenix/clean_file/version.Version=$(VERSION)' \
-X 'git.kundeng.us/phoenix/clean_file/version.BuildTime=$(BUILD_TIME)' \
-X 'git.kundeng.us/phoenix/clean_file/version.Commit=$(COMMIT)' \
-X 'git.kundeng.us/phoenix/clean_file/version.GoVersion=$(GO_VERSION)'"
+1 -6
View File
@@ -2,12 +2,7 @@ CLI software that processes a text file containing US phone numbers
and sanitizes the numbers. The result is contained in a json file. and sanitizes the numbers. The result is contained in a json file.
## Building ## Building
```
go build
```
To include version information
``` ```
go build -ldflags "-X main.version=3.0.101 -X main.commit=$(git rev-parse HEAD) -X main.date=$(date +%Y-%m-%dT%H:%M:%S%z)" -o clean_file make build
``` ```
+12 -1
View File
@@ -1,11 +1,13 @@
package main package main
import "flag"
import "fmt" import "fmt"
import "os" import "os"
import "git.kundeng.us/phoenix/clean_file/parser" import "git.kundeng.us/phoenix/clean_file/parser"
import "git.kundeng.us/phoenix/clean_file/version"
func main() { func main() {
fmt.Println("clean_file")
args := os.Args args := os.Args
if len(args) < 2 { if len(args) < 2 {
@@ -13,6 +15,15 @@ func main() {
os.Exit(-1) os.Exit(-1)
} }
versionFlag := flag.Bool("version", false, "Print version information")
flag.Parse()
if *versionFlag {
fmt.Println(version.String())
return
}
fmt.Println("clean_file")
filepath := args[1] filepath := args[1]
fmt.Println("File path:", filepath) fmt.Println("File path:", filepath)
+13 -3
View File
@@ -1,7 +1,17 @@
package version package version
import "fmt"
var ( var (
Version = "dev" Version = "dev"
Commit = "none" BuildTime = "unknown"
Date = "unknown" Commit = "unknown"
GoVersion = "unknown"
) )
func String() string {
return fmt.Sprintf(
"Version: %s\nBuild Date: %s\nCommit: %s\nGo Version: %s",
Version, BuildTime, Commit, GoVersion,
)
}