From d07ff3c5ed16aa49d16f472aa15c2609c6bef6b7 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Wed, 29 May 2019 23:25:22 +0200 Subject: [PATCH] add version and revision checking --- Makefile | 10 ++++++---- collect/resh-collect.go | 37 +++++++++++++++++++++++++++++++++++-- common/resh-common.go | 5 ++++- daemon/resh-daemon.go | 9 ++++++++- shellrc.sh | 7 ++++++- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 9ca0d17..ab9e247 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ SHELL=/bin/bash - +VERSION=$(shell cat version) +REVISION=$(shell [ -n "$(git status --untracked-files=no --porcelain)" ] && git rev-parse --short=12 HEAD || echo "no_revision") +GOFLAGS=-ldflags "-X main.Version=${VERSION} -X main.Revision=${REVISION}" autoinstall: ./install_helper.sh @@ -39,7 +41,7 @@ install: build | $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config $(HOME)/.resh/r ########################################################## # # WHAT'S NEXT - # Please close all open terminal windows (or reload your rc files) + # Please RESTART ALL OPEN TERMINAL WINDOWS (or reload your rc files) # Your resh history is located in `~/.resh_history.json` # You can look at it using e.g. `tail -f ~/.resh_history.json | jq` # @@ -54,10 +56,10 @@ uninstall: -rm -rf ~/.resh/ resh-daemon: daemon/resh-daemon.go common/resh-common.go - go build -o $@ $< + go build ${GOFLAGS} -o $@ $< resh-collect: collect/resh-collect.go common/resh-common.go - go build -o $@ $< + go build ${GOFLAGS} -o $@ $< $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config: diff --git a/collect/resh-collect.go b/collect/resh-collect.go index 07cd248..e2a6c30 100644 --- a/collect/resh-collect.go +++ b/collect/resh-collect.go @@ -4,12 +4,13 @@ import ( "bytes" "encoding/json" "flag" + "fmt" "github.com/BurntSushi/toml" common "github.com/curusarn/resh/common" "io/ioutil" "log" "net/http" - // "os" + "os" // "os/exec" "os/user" "path/filepath" @@ -17,6 +18,9 @@ import ( "strings" ) +var Version string +var Revision string + func main() { usr, _ := user.Current() dir := usr.HomeDir @@ -29,6 +33,11 @@ func main() { if _, err := toml.DecodeFile(configPath, &config); err != nil { log.Fatal("Error reading config:", err) } + showVersion := flag.Bool("version", false, "Show version and exit") + showRevision := flag.Bool("revision", false, "Show git revision and exit") + + requireVersion := flag.String("requireVersion", "", "abort if version doesn't match") + requireRevision := flag.String("requireRevision", "", "abort if revision doesn't match") cmdLine := flag.String("cmdLine", "", "command line") exitCode := flag.Int("exitCode", -1, "exit code") @@ -84,6 +93,26 @@ func main() { "on session start $EPOCHREALTIME") flag.Parse() + if *showVersion == true { + fmt.Println(Version) + os.Exit(0) + } + if *showRevision == true { + fmt.Println(Revision) + os.Exit(0) + } + if *requireVersion != "" && *requireVersion != Version { + log.Fatal("Please restart/reload this terminal session " + + "(resh version: " + Version + + " resh version of this terminal session: " + *requireVersion + + ")") + } + if *requireRevision != "" && *requireRevision != Revision { + log.Fatal("Please restart/reload this terminal session " + + "(resh revision: " + Revision + + " resh revision of this terminal session: " + *requireRevision + + ")") + } realtimeAfter, err := strconv.ParseFloat(*rta, 64) if err != nil { log.Fatal("Flag Parsing error (rta):", err) @@ -185,13 +214,16 @@ func main() { GitRealDir: gitRealDir, GitOriginRemote: *gitRemote, MachineId: readFileContent(machineIdPath), - ReshUuid: readFileContent(reshUuidPath), OsReleaseId: *osReleaseId, OsReleaseVersionId: *osReleaseVersionId, OsReleaseIdLike: *osReleaseIdLike, OsReleaseName: *osReleaseName, OsReleasePrettyName: *osReleasePrettyName, + + ReshUuid: readFileContent(reshUuidPath), + ReshVersion: Version, + ReshRevision: Revision, } sendRecord(rec, strconv.Itoa(config.Port)) } @@ -293,3 +325,4 @@ func getTimezoneOffsetInSeconds(zone string) float64 { // } // return strings.TrimSuffix(string(out), "\n") // } +// } diff --git a/common/resh-common.go b/common/resh-common.go index 70727fa..b456d2b 100644 --- a/common/resh-common.go +++ b/common/resh-common.go @@ -50,13 +50,16 @@ type Record struct { GitRealDir string `json:"gitRealDir"` GitOriginRemote string `json:"gitOriginRemote"` MachineId string `json:"machineId"` - ReshUuid string `json:"reshUuid"` OsReleaseId string `json:"osReleaseId"` OsReleaseVersionId string `json:"osReleaseVersionId"` OsReleaseIdLike string `json:"osReleaseIdLike"` OsReleaseName string `json:"osReleaseName"` OsReleasePrettyName string `json:"osReleasePrettyName"` + + ReshUuid string `json:"reshUuid"` + ReshVersion string `json:"reshVersion"` + ReshRevision string `json:"reshRevision"` } type Config struct { diff --git a/daemon/resh-daemon.go b/daemon/resh-daemon.go index 78eb310..fb1b31a 100644 --- a/daemon/resh-daemon.go +++ b/daemon/resh-daemon.go @@ -16,7 +16,13 @@ import ( "strings" ) +var Version string +var Revision string + func main() { + log.Println("Daemon starting... \n" + + "version: " + Version + + " revision: " + Revision) usr, _ := user.Current() dir := usr.HomeDir pidfilePath := filepath.Join(dir, ".resh/resh.pid") @@ -67,7 +73,8 @@ func main() { } func statusHandler(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("OK\n")) + w.Write([]byte("OK; version: " + Version + + "; revision: " + Revision + "\n")) log.Println("Status OK") } diff --git a/shellrc.sh b/shellrc.sh index b838816..556367d 100644 --- a/shellrc.sh +++ b/shellrc.sh @@ -96,6 +96,9 @@ elif [ $__RESH_MACOS -eq 1 ]; then __RESH_RT_SESS_SINCE_BOOT=$(sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//g') fi +__RESH_VERSION=$(resh-collect -version) +__RESH_REVISION=$(resh-collect -revision) + __resh_run_daemon __resh_preexec() { @@ -149,7 +152,9 @@ __resh_precmd() { __RESH_TZ_AFTER=$(date +%z) __RESH_PWD_AFTER="$PWD" if [ -n "${__RESH_COLLECT}" ]; then - resh-collect -cmdLine "$__RESH_CMDLINE" \ + resh-collect -requireVersion "$__RESH_VERSION" \ + -requireRevision "$__RESH_REVISION" \ + -cmdLine "$__RESH_CMDLINE" \ -exitCode "$__RESH_EXIT_CODE" \ -shell "$__RESH_SHELL" \ -uname "$__RESH_UNAME" \