From 4dfe16321b7ef7e32f412e2968d4cfc1f8c3e553 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Mon, 16 Dec 2019 16:03:06 +0100 Subject: [PATCH] use exit code to signify recall errors instead of just assuming empty response means error --- Makefile | 2 +- cmd/collect/main.go | 6 +++++- cmd/daemon/recall.go | 8 +++++--- pkg/collect/collect.go | 5 +++-- scripts/hooks.sh | 2 ++ scripts/widgets.sh | 6 ++++-- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 54608f5..f48198c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SHELL=/bin/bash LATEST_TAG=$(shell git describe --tags) REVISION=$(shell [ -z "$(git status --untracked-files=no --porcelain)" ] && git rev-parse --short=12 HEAD || echo "no_revision") -VERSION=${LATEST_TAG}-dev-${REVISION} +VERSION="${LATEST_TAG}-DEV" GOFLAGS=-ldflags "-X main.version=${VERSION} -X main.commit=${REVISION}" sanitize: diff --git a/cmd/collect/main.go b/cmd/collect/main.go index f6972b9..eec8d79 100644 --- a/cmd/collect/main.go +++ b/cmd/collect/main.go @@ -175,7 +175,11 @@ func main() { RecallHistno: *recallHistno, RecallPrefix: *recallPrefix, } - fmt.Print(collect.SendRecallRequest(rec, strconv.Itoa(config.Port))) + str, found := collect.SendRecallRequest(rec, strconv.Itoa(config.Port)) + if found == false { + os.Exit(1) + } + fmt.Println(str) } else { rec := records.Record{ // posix diff --git a/cmd/daemon/recall.go b/cmd/daemon/recall.go index 43e4e6b..32e63aa 100644 --- a/cmd/daemon/recall.go +++ b/cmd/daemon/recall.go @@ -34,13 +34,15 @@ func (h *recallHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } log.Println("/recall recalling ...") + found := true cmd, err := h.sesshistDispatch.Recall(rec.SessionID, rec.RecallHistno, rec.RecallPrefix) if err != nil { log.Println("/recall - sess id:", rec.SessionID, " - histno:", rec.RecallHistno, " -> ERROR") log.Println("Recall error:", err) - return + found = false + cmd = "" } - resp := collect.SingleResponse{CmdLine: cmd} + resp := collect.SingleResponse{CmdLine: cmd, Found: found} log.Println("/recall marshaling response ...") jsn, err = json.Marshal(&resp) if err != nil { @@ -51,7 +53,7 @@ func (h *recallHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Println(string(jsn)) log.Println("/recall writing response ...") w.Write(jsn) - log.Println("/recall END - sess id:", rec.SessionID, " - histno:", rec.RecallHistno, " -> ", cmd) + log.Println("/recall END - sess id:", rec.SessionID, " - histno:", rec.RecallHistno, " -> ", cmd, " (found:", found, ")") } type inspectHandler struct { diff --git a/pkg/collect/collect.go b/pkg/collect/collect.go index 56ee374..fd7d5a7 100644 --- a/pkg/collect/collect.go +++ b/pkg/collect/collect.go @@ -15,11 +15,12 @@ import ( // SingleResponse json struct type SingleResponse struct { + Found bool `json:"found"` CmdLine string `json:"cmdline"` } // SendRecallRequest to daemon -func SendRecallRequest(r records.SlimRecord, port string) string { +func SendRecallRequest(r records.SlimRecord, port string) (string, bool) { recJSON, err := json.Marshal(r) if err != nil { log.Fatal("send err 1", err) @@ -50,7 +51,7 @@ func SendRecallRequest(r records.SlimRecord, port string) string { log.Fatal("unmarshal resp error: ", err) } log.Println(response) - return response.CmdLine + return response.CmdLine, response.Found } // SendRecord to daemon diff --git a/scripts/hooks.sh b/scripts/hooks.sh index 8da69c3..7798699 100644 --- a/scripts/hooks.sh +++ b/scripts/hooks.sh @@ -103,7 +103,9 @@ __resh_collect() { -osReleasePrettyName "$__RESH_OS_RELEASE_PRETTY_NAME" \ -histno "$__RESH_HISTNO" \ "$@" + return $? fi + return 1 } __resh_precmd() { diff --git a/scripts/widgets.sh b/scripts/widgets.sh index a29a8f4..a931f58 100644 --- a/scripts/widgets.sh +++ b/scripts/widgets.sh @@ -51,10 +51,12 @@ __resh_widget_arrow_up() { else # run recall local NEW_BUFFER + local status_code NEW_BUFFER="$(__resh_collect --recall --prefix-search "$__RESH_PREFIX" 2> ~/.resh/arrow_up_last_run_out.txt)" - # IF new buffer in non-empty THEN use the new buffer ELSE revert histno change + status_code=$? + # revert histno change on error # shellcheck disable=SC2015 - if [ "${#NEW_BUFFER}" -gt 0 ]; then + if [ "${status_code}" -eq 0 ]; then BUFFER=$NEW_BUFFER else __RESH_HISTNO=$((__RESH_HISTNO-1))