diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..11e6c78 --- /dev/null +++ b/build.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +VERSION="$(git describe --tags --dirty)" + +# Linker flags trick below from here: +# https://www.reddit.com/r/golang/comments/4cpi2y/question_where_to_keep_the_version_number_of_a_go/d1kbap7?utm_source=share&utm_medium=web2x + +# This line must be last in the script so that its return code +# propagates properly to its caller +go build -ldflags="-X main.versionString=$VERSION" diff --git a/moar.go b/moar.go index a99d132..6913e22 100644 --- a/moar.go +++ b/moar.go @@ -14,18 +14,20 @@ import ( "golang.org/x/crypto/ssh/terminal" ) +var versionString = "Should be set when building, please use build.sh to build" + func main() { // FIXME: On any panic or warnings, also print system info and how to report bugs - version := flag.Bool("version", false, "Prints the moar version number") + printVersion := flag.Bool("version", false, "Prints the moar version number") // FIXME: Support --help // FIXME: Support --no-highlight flag.Parse() - if *version { - fmt.Println("FIXME: Imagine a version string here") + if *printVersion { + fmt.Println(versionString) os.Exit(0) } diff --git a/moar.sh b/moar.sh index 47664f3..f1d414b 100755 --- a/moar.sh +++ b/moar.sh @@ -7,6 +7,6 @@ set -e -o pipefail rm -f moar -go build 1>&2 +./build.sh 1>&2 ./moar "$@" diff --git a/test.sh b/test.sh index 87ce4e3..ede9a0c 100755 --- a/test.sh +++ b/test.sh @@ -5,9 +5,14 @@ set -e -o pipefail # Unit tests first go test github.com/walles/moar/m -# Verify sending the output to a file -go build +# Ensure we can cross compile +GOOS=linux GOARCH=386 ./build.sh +GOOS=darwin GOARCH=amd64 ./build.sh +# Make sure we have a runnable binary for the current platform when done +./build.sh + +# Verify sending the output to a file RESULT="$(mktemp)" function cleanup { rm -rf "$RESULT" @@ -34,9 +39,5 @@ echo Test --version... ./moar --version > /dev/null # Should exit with code 0 diff -u <(./moar --version) <(git describe --tags --dirty) -# Ensure we can cross compile -GOOS=linux GOARCH=386 go build -GOOS=darwin GOARCH=amd64 go build - echo echo "All tests passed!"