From ba2e9d80fd96d2874cca7beb26b43bb24e9a8395 Mon Sep 17 00:00:00 2001 From: W-Mark Kubacki Date: Fri, 18 Mar 2016 23:10:24 +0100 Subject: [PATCH] Use best practices in build.bash Format of main.buildDate has been locale-dependent, and is now ISO-8601 compliant. Caddy displayed with ```-version``` something like (mind the datetime format): Caddy 0.8.2 (+591b209 Fri Mar 18 21:22:55 UTC 2016) 2 files changed, 9 insertions(+), 4 deletions(-) build.bash main.go which is now: Caddy 0.8.2 (+591b209 2016-03-18 21:22:55Z) 2 files changed, 9 insertions(+), 4 deletions(-) build.bash,main.go See also: * http://wiki.bash-hackers.org/scripting/obsolete * https://google.github.io/styleguide/shell.xml * https://xkcd.com/1179/ --- .gitattributes | 2 ++ build.bash | 54 +++++++++++++++++++++++++------------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/.gitattributes b/.gitattributes index b3384bdc..a850e37c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,5 @@ +*.bash text eol=lf whitespace=blank-at-eol,space-before-tab,tab-in-indent,trailing-space,tabwidth=2 + # files for systemd *.path text eol=lf whitespace=blank-at-eol,space-before-tab,tab-in-indent,trailing-space,tabwidth=2 *.service text eol=lf whitespace=blank-at-eol,space-before-tab,tab-in-indent,trailing-space,tabwidth=2 diff --git a/build.bash b/build.bash index b7c97d1e..6f5bf06f 100755 --- a/build.bash +++ b/build.bash @@ -8,48 +8,48 @@ # # Outputs compiled program in current directory. # Default file name is 'ecaddy'. -# + set -e -output="$1" -if [ -z "$output" ]; then - output="ecaddy" -fi +: ${output_filename:="$1"} +: ${output_filename:="ecaddy"} pkg=main +ldflags=() # Timestamp of build -builddate_id=$pkg.buildDate -builddate=`date -u` +name="${pkg}.buildDate" +value=$(date --utc +"%F %H:%M:%SZ") +ldflags+=("-X" "\"${name}=${value}\"") # Current tag, if HEAD is on a tag -tag_id=$pkg.gitTag +name="${pkg}.gitTag" set +e -tag=`git describe --exact-match HEAD 2> /dev/null` +value="$(git describe --exact-match HEAD 2>/dev/null)" set -e +ldflags+=("-X" "\"${name}=${value}\"") # Nearest tag on branch -lasttag_id=$pkg.gitNearestTag -lasttag=`git describe --abbrev=0 --tags HEAD` +name="${pkg}.gitNearestTag" +value="$(git describe --abbrev=0 --tags HEAD)" +ldflags+=("-X" "\"${name}=${value}\"") # Commit SHA -commit_id=$pkg.gitCommit -commit=`git rev-parse --short HEAD` +name="${pkg}.gitCommit" +value="$(git rev-parse --short HEAD)" +ldflags+=("-X" "\"${name}=${value}\"") -# Summary of uncommited changes -shortstat_id=$pkg.gitShortStat -shortstat=`git diff-index --shortstat HEAD` +# Summary of uncommitted changes +name="${pkg}.gitShortStat" +value="$(git diff-index --shortstat HEAD)" +ldflags+=("-X" "\"${name}=${value}\"") # List of modified files -files_id=$pkg.gitFilesModified -files=`git diff-index --name-only HEAD` +name="${pkg}.gitFilesModified" +value="$(git diff-index --name-only HEAD | tr "\n" "," | sed -e 's:,$::')" +ldflags+=("-X" "\"${name}=${value}\"") - -go build -ldflags " - -X \"$builddate_id=$builddate\" - -X \"$tag_id=$tag\" - -X \"$lasttag_id=$lasttag\" - -X \"$commit_id=$commit\" - -X \"$shortstat_id=$shortstat\" - -X \"$files_id=$files\" -" -o "$output" +set -x +go build \ + -ldflags "${ldflags[*]}" \ + -o "${output_filename}"