From 2c63c8cfba5671765318a869be9e9a0b86138400 Mon Sep 17 00:00:00 2001 From: Ramil Mirhasanov Date: Sun, 20 Nov 2022 14:28:21 +0300 Subject: [PATCH] added gofail --version Printing: * GitSHA * Version * Go Version * Go OS/Arch Signed-off-by: Ramil Mirhasanov --- Makefile | 5 +++-- build.sh | 7 +++++++ go.sum | 0 gofail.go | 28 ++++++++++++++++++++++++++-- 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100755 build.sh create mode 100644 go.sum diff --git a/Makefile b/Makefile index 2c74c86..a4da662 100644 --- a/Makefile +++ b/Makefile @@ -15,5 +15,6 @@ test: fmt: gofmt -w -l -s $(SRCS) -gofail: $(SRCS) - go build -v +gofail: + GO_BUILD_FLAGS="-v" ./build.sh + ./gofail --version diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..8d4f252 --- /dev/null +++ b/build.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +GIT_SHA=$(git rev-parse --short HEAD || echo "GitNotFound") + +# Set GO_LDFLAGS="-s" for building without symbols for debugging. +GO_LDFLAGS=("-X 'main.GitSHA=${GIT_SHA}'") +go build $GO_BUILD_FLAGS -ldflags="$GO_LDFLAGS" -o gofail diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/gofail.go b/gofail.go index be7dafa..46cf588 100644 --- a/gofail.go +++ b/gofail.go @@ -22,11 +22,29 @@ import ( "os" "path" "path/filepath" + "runtime" "strings" "go.etcd.io/gofail/code" ) +var ( + // Git SHA Value will be set during build + GitSHA = "Not provided (use ./build.sh instead of go build)" + + Version = "0.1.0" +) + +var usageLine = `Usage: +gofail enable [list of files or directories] + Enable the failpoints + +gofail disable [list of files or directories] + Disable the checkpoints + +gofail --version + Show the version of gofail` + type xfrmFunc func(io.Writer, io.Reader) ([]*code.Failpoint, error) func xfrmFile(xfrm xfrmFunc, path string) ([]*code.Failpoint, error) { @@ -152,7 +170,7 @@ func writeBinding(file string, fps []*code.Failpoint) { func main() { if len(os.Args) < 2 { - fmt.Println("not enough arguments") + fmt.Println(usageLine) os.Exit(1) } @@ -164,8 +182,14 @@ func main() { enable = true case "disable": xfrm = code.ToComments + case "--version": + fmt.Println("Git SHA: ", GitSHA) + fmt.Println("Go Version: ", runtime.Version()) + fmt.Println("gofail Version: ", Version) + fmt.Printf("Go OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH) + os.Exit(1) default: - fmt.Println("expected enable or disable") + fmt.Println(usageLine) os.Exit(1) }