From 94835f416a867473c4bd1d836156de6f6879a4ed Mon Sep 17 00:00:00 2001 From: asdf Date: Sun, 22 Sep 2019 14:01:16 +1000 Subject: [PATCH 1/2] Added -v flag and makefile for application version --- README.md | 4 ++-- main.go | 12 ++++++++++++ makefile | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 makefile diff --git a/README.md b/README.md index cc05a91..681f83f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Assuming you have `go` installed, run the following: ``` git clone https://tildegit.org/sloum/gfu.git cd gfu -go install +make install ``` Assuming `go install` is set up to install to a place on your path, you should be able to execute `gfu` from the terminal: ``` @@ -29,7 +29,7 @@ gfu ``` #### Troubleshooting -If you run `gfu` and get `gfu: command not found`, try running `go build` from within the cloned repo. Then try: `./gfu`. If that works it means that Go does not install to your path. `go build` added an executable file to the repo directory. Move that file to somewhere on your path. I suggest `/usr/local/bin` on most systems, but that may be a matter of personal preference. +If you run `gfu` and get `gfu: command not found`, try running `make` from within the cloned repo. Then try: `./gfu`. If that works it means that Go does not install to your path. `make` added an executable file to the repo directory. Move that file to somewhere on your path. I suggest `/usr/local/bin` on most systems, but that may be a matter of personal preference. ### Downloading If you would prefer to download a binary for your system, rather than build from source, please visit the [gfu downloads](https://rawtext.club/~sloum/gfu.html#downloads) page. diff --git a/main.go b/main.go index 995a609..d98a499 100644 --- a/main.go +++ b/main.go @@ -42,12 +42,19 @@ import ( "strings" ) +//version information, set by ldflags with some default fallbacks +var ( + version string = "not set" + build string = "not set" +) + //command line flag global variables var ( deconstructInfoTextLines bool header string footer string stdout bool + printVersion bool ) //regex global variable that identifies the format of gopher menu lines @@ -142,6 +149,7 @@ func init() { flag.StringVar(&header, "head", "", "Path to a file containing header content") flag.StringVar(&footer, "foot", "", "Path to a file containing footer content") flag.BoolVar(&stdout, "stdout", false, "Instead of writing changes to a file, return them to stdout") + flag.BoolVar(&printVersion, "v", false, "Output version information") } //PrintHelp produces a nice display message when the --help flag is used @@ -165,6 +173,10 @@ func main() { flag.Parse() args := flag.Args() + if printVersion { + fmt.Printf("gfu - gophermap format utility - version %s build %s\n", version, build) + os.Exit(0) + } if l := len(args); l != 1 { fmt.Fprintf(os.Stderr, "Incorrect number of arguments. Expected 1, got %d\n", l) os.Exit(1) diff --git a/makefile b/makefile new file mode 100644 index 0000000..628f638 --- /dev/null +++ b/makefile @@ -0,0 +1,25 @@ +BINARY := gfu +PKGS := $(shell go list ./... |grep -v /vendor) +VERSION := $(shell git describe --tags) +BUILD := $(shell date -Iminutes) + +LDFLAGS := -ldflags "-s -X main.version=${VERSION} -X main.build=${BUILD}" +#ldflags for versioning info: https://stackoverflow.com/questions/11354518/application-auto-build-versioning +#-s flag info: https://blog.spiralscout.com/using-w-and-s-flags-in-golang-97ae59b50e26 + +build: + go build ${LDFLAGS} -o ${BINARY} + +.PHONY: install +install: + go install ${LDFLAGS} + +.PHONY: clean +clean: + go clean + +.PHONY: test +test: + go test $(PKGS) + + From bd518fdeb2973279ece9a12d0c15a0ce1620c0c9 Mon Sep 17 00:00:00 2001 From: asdf Date: Tue, 24 Sep 2019 20:52:38 +1000 Subject: [PATCH 2/2] Added fallback for git version failure, make uninstall --- VERSION | 1 + makefile | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 VERSION diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..49d5957 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1 diff --git a/makefile b/makefile index 628f638..ab16706 100644 --- a/makefile +++ b/makefile @@ -1,11 +1,17 @@ BINARY := gfu PKGS := $(shell go list ./... |grep -v /vendor) -VERSION := $(shell git describe --tags) -BUILD := $(shell date -Iminutes) -LDFLAGS := -ldflags "-s -X main.version=${VERSION} -X main.build=${BUILD}" +# version checks git tags. if any errors, they are discarded, and the version is +# read from the VERSION file +VERSION := $(shell git describe --tags 2> /dev/null) +ifeq ($(VERSION),) + VERSION := $(shell cat VERSION) +endif +BUILD := $(shell date -Iminutes) + #ldflags for versioning info: https://stackoverflow.com/questions/11354518/application-auto-build-versioning #-s flag info: https://blog.spiralscout.com/using-w-and-s-flags-in-golang-97ae59b50e26 +LDFLAGS := -ldflags "-s -X main.version=${VERSION} -X main.build=${BUILD}" build: go build ${LDFLAGS} -o ${BINARY} @@ -14,12 +20,10 @@ build: install: go install ${LDFLAGS} +.PHONY: uninstall +uninstall: + go clean -i + .PHONY: clean clean: go clean - -.PHONY: test -test: - go test $(PKGS) - -