From e06b0bff303f402e468e98d3ace863e6d2bd174e Mon Sep 17 00:00:00 2001 From: Alessio Date: Tue, 4 Jan 2022 12:57:43 -0500 Subject: [PATCH] Add --version flag to the executable --- .build.yml | 6 +++++- cmd/compile.sh | 6 +++++- cmd/tests.sh | 5 ++++- cmd/twitter/main.go | 18 ++++++++++++++---- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/.build.yml b/.build.yml index 34ef734..7f1ce63 100644 --- a/.build.yml +++ b/.build.yml @@ -122,8 +122,12 @@ tasks: twitter fetch_user michaelmalice twitter get_user_tweets michaelmalice + # Compile the binary with the version number + cd .. && ./compile.sh $version + cd outdated_profile + # Now test the upgrade using the newer version - which ../tw # Should be here from previous step + which ../tw ../tw fetch_user michaelmalice # Any operation other than `create_profile` should trigger the upgrade duration=$SECONDS diff --git a/cmd/compile.sh b/cmd/compile.sh index ff16197..94bd136 100755 --- a/cmd/compile.sh +++ b/cmd/compile.sh @@ -3,5 +3,9 @@ set -x set -e -go build -ldflags="-s -w" -o tw ./twitter +if [[ -n "$1" ]]; then + go build -ldflags="-s -w -X main.version_string=$1" -o tw ./twitter +else + go build -ldflags="-s -w" -o tw ./twitter +fi chmod +x tw diff --git a/cmd/tests.sh b/cmd/tests.sh index fa48a17..1b54ab9 100755 --- a/cmd/tests.sh +++ b/cmd/tests.sh @@ -5,12 +5,15 @@ set -x PS4='+(${BASH_SOURCE}:${LINENO}): ' -./compile.sh +FAKE_VERSION="1.100.3489" +./compile.sh $FAKE_VERSION test -e data && rm -r data PATH=`pwd`:$PATH +test "$(tw --version)" = "v$FAKE_VERSION" + tw create_profile data cd data diff --git a/cmd/twitter/main.go b/cmd/twitter/main.go index 9ae38c7..cf612fe 100644 --- a/cmd/twitter/main.go +++ b/cmd/twitter/main.go @@ -13,19 +13,29 @@ import ( */ var profile persistence.Profile +var version_string string /** * Main method */ func main() { - if len(os.Args) < 3 { - die("", true, 0) - } - profile_dir := flag.String("profile", ".", "TODO USAGE") + flag.StringVar(profile_dir, "p", ".", "TODO USAGE") + + show_version_flag := flag.Bool("version", false, "???") + flag.BoolVar(show_version_flag, "v", false, "???") flag.Parse() args := flag.Args() + if *show_version_flag { + if version_string == "" { + fmt.Println("Development version") + } else { + fmt.Println("v" + version_string) + } + os.Exit(0) + } + if len(args) < 2 { die("", true, 1) }