add version and revision checking

pull/8/head
Simon Let 7 years ago
parent 78a8730fae
commit d07ff3c5ed
  1. 10
      Makefile
  2. 37
      collect/resh-collect.go
  3. 5
      common/resh-common.go
  4. 9
      daemon/resh-daemon.go
  5. 7
      shellrc.sh

@ -1,5 +1,7 @@
SHELL=/bin/bash 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: autoinstall:
./install_helper.sh ./install_helper.sh
@ -39,7 +41,7 @@ install: build | $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config $(HOME)/.resh/r
########################################################## ##########################################################
# #
# WHAT'S NEXT # 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` # Your resh history is located in `~/.resh_history.json`
# You can look at it using e.g. `tail -f ~/.resh_history.json | jq` # You can look at it using e.g. `tail -f ~/.resh_history.json | jq`
# #
@ -54,10 +56,10 @@ uninstall:
-rm -rf ~/.resh/ -rm -rf ~/.resh/
resh-daemon: daemon/resh-daemon.go common/resh-common.go 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 resh-collect: collect/resh-collect.go common/resh-common.go
go build -o $@ $< go build ${GOFLAGS} -o $@ $<
$(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config: $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config:

@ -4,12 +4,13 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
common "github.com/curusarn/resh/common" common "github.com/curusarn/resh/common"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
// "os" "os"
// "os/exec" // "os/exec"
"os/user" "os/user"
"path/filepath" "path/filepath"
@ -17,6 +18,9 @@ import (
"strings" "strings"
) )
var Version string
var Revision string
func main() { func main() {
usr, _ := user.Current() usr, _ := user.Current()
dir := usr.HomeDir dir := usr.HomeDir
@ -29,6 +33,11 @@ func main() {
if _, err := toml.DecodeFile(configPath, &config); err != nil { if _, err := toml.DecodeFile(configPath, &config); err != nil {
log.Fatal("Error reading config:", err) 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") cmdLine := flag.String("cmdLine", "", "command line")
exitCode := flag.Int("exitCode", -1, "exit code") exitCode := flag.Int("exitCode", -1, "exit code")
@ -84,6 +93,26 @@ func main() {
"on session start $EPOCHREALTIME") "on session start $EPOCHREALTIME")
flag.Parse() 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) realtimeAfter, err := strconv.ParseFloat(*rta, 64)
if err != nil { if err != nil {
log.Fatal("Flag Parsing error (rta):", err) log.Fatal("Flag Parsing error (rta):", err)
@ -185,13 +214,16 @@ func main() {
GitRealDir: gitRealDir, GitRealDir: gitRealDir,
GitOriginRemote: *gitRemote, GitOriginRemote: *gitRemote,
MachineId: readFileContent(machineIdPath), MachineId: readFileContent(machineIdPath),
ReshUuid: readFileContent(reshUuidPath),
OsReleaseId: *osReleaseId, OsReleaseId: *osReleaseId,
OsReleaseVersionId: *osReleaseVersionId, OsReleaseVersionId: *osReleaseVersionId,
OsReleaseIdLike: *osReleaseIdLike, OsReleaseIdLike: *osReleaseIdLike,
OsReleaseName: *osReleaseName, OsReleaseName: *osReleaseName,
OsReleasePrettyName: *osReleasePrettyName, OsReleasePrettyName: *osReleasePrettyName,
ReshUuid: readFileContent(reshUuidPath),
ReshVersion: Version,
ReshRevision: Revision,
} }
sendRecord(rec, strconv.Itoa(config.Port)) sendRecord(rec, strconv.Itoa(config.Port))
} }
@ -293,3 +325,4 @@ func getTimezoneOffsetInSeconds(zone string) float64 {
// } // }
// return strings.TrimSuffix(string(out), "\n") // return strings.TrimSuffix(string(out), "\n")
// } // }
// }

@ -50,13 +50,16 @@ type Record struct {
GitRealDir string `json:"gitRealDir"` GitRealDir string `json:"gitRealDir"`
GitOriginRemote string `json:"gitOriginRemote"` GitOriginRemote string `json:"gitOriginRemote"`
MachineId string `json:"machineId"` MachineId string `json:"machineId"`
ReshUuid string `json:"reshUuid"`
OsReleaseId string `json:"osReleaseId"` OsReleaseId string `json:"osReleaseId"`
OsReleaseVersionId string `json:"osReleaseVersionId"` OsReleaseVersionId string `json:"osReleaseVersionId"`
OsReleaseIdLike string `json:"osReleaseIdLike"` OsReleaseIdLike string `json:"osReleaseIdLike"`
OsReleaseName string `json:"osReleaseName"` OsReleaseName string `json:"osReleaseName"`
OsReleasePrettyName string `json:"osReleasePrettyName"` OsReleasePrettyName string `json:"osReleasePrettyName"`
ReshUuid string `json:"reshUuid"`
ReshVersion string `json:"reshVersion"`
ReshRevision string `json:"reshRevision"`
} }
type Config struct { type Config struct {

@ -16,7 +16,13 @@ import (
"strings" "strings"
) )
var Version string
var Revision string
func main() { func main() {
log.Println("Daemon starting... \n" +
"version: " + Version +
" revision: " + Revision)
usr, _ := user.Current() usr, _ := user.Current()
dir := usr.HomeDir dir := usr.HomeDir
pidfilePath := filepath.Join(dir, ".resh/resh.pid") pidfilePath := filepath.Join(dir, ".resh/resh.pid")
@ -67,7 +73,8 @@ func main() {
} }
func statusHandler(w http.ResponseWriter, r *http.Request) { 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") log.Println("Status OK")
} }

@ -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') __RESH_RT_SESS_SINCE_BOOT=$(sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//g')
fi fi
__RESH_VERSION=$(resh-collect -version)
__RESH_REVISION=$(resh-collect -revision)
__resh_run_daemon __resh_run_daemon
__resh_preexec() { __resh_preexec() {
@ -149,7 +152,9 @@ __resh_precmd() {
__RESH_TZ_AFTER=$(date +%z) __RESH_TZ_AFTER=$(date +%z)
__RESH_PWD_AFTER="$PWD" __RESH_PWD_AFTER="$PWD"
if [ -n "${__RESH_COLLECT}" ]; then 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" \ -exitCode "$__RESH_EXIT_CODE" \
-shell "$__RESH_SHELL" \ -shell "$__RESH_SHELL" \
-uname "$__RESH_UNAME" \ -uname "$__RESH_UNAME" \

Loading…
Cancel
Save