1
1
mirror of https://github.com/walles/moar.git synced 2024-08-16 23:40:35 +03:00

Write version number into executable

This commit is contained in:
Johan Walles 2019-07-07 18:34:05 +02:00
parent 84ae2cb21c
commit 0fbb2ce8d3
4 changed files with 23 additions and 10 deletions

10
build.sh Executable file
View File

@ -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"

View File

@ -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)
}

View File

@ -7,6 +7,6 @@ set -e -o pipefail
rm -f moar
go build 1>&2
./build.sh 1>&2
./moar "$@"

13
test.sh
View File

@ -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!"