From baf7cdedc59f849d815a14b29267c3a76c411412 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Sat, 17 Dec 2022 16:24:22 +0100 Subject: [PATCH] update reshctl version to use output package --- README.md | 24 +++++++++++++----------- cmd/collect/main.go | 4 ++-- cmd/control/cmd/version.go | 18 +++++------------- cmd/postcollect/main.go | 4 ++-- cmd/session-init/main.go | 4 ++-- internal/output/output.go | 28 ++++++++++++++++++++++++++-- 6 files changed, 50 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index b2e12a7..f345f97 100644 --- a/README.md +++ b/README.md @@ -17,15 +17,15 @@ Context-based replacement/enhancement for zsh and bash shell history -**Search your history by commands and get relevant results based on current directory, git repo, exit status, and host.** +**Search your history by commands or arguments and get relevant results based on current directory, git repo, exit status, and device.** ## Installation ### Prerequisites -Standard stuff: `bash(4.3+)`, `curl`, `tar`, ... +Nohup -Bash completions will only work if you have `bash-completion` installed +Standard stuff: `bash(4.3+)`, `curl`, `tar`, ... MacOS: `coreutils` (`brew install coreutils`) @@ -62,7 +62,7 @@ reshctl update This is the most important part of this project. -RESH SEARCH app searches your history by commands. It uses host, directories, git remote, and exit status to show you relevant results first. +RESH SEARCH app searches your history by commands. It uses device, directories, git remote, and exit status to show you relevant results first. All this context is not in the regular shell history. RESH records shell history with context to use it when searching. @@ -74,7 +74,7 @@ Eventually most of your history will have context and RESH SEARCH app will get m ![resh search app](img/screen-resh-cli-v2-7.png) -Without a query, RESH SEARCH app shows you the latest history based on the current context (host, directory, git). +Without a query, RESH SEARCH app shows you the latest history based on the current context (device, directory, git). ![resh search app](img/screen-resh-cli-v2-7-no-query.png) @@ -99,13 +99,15 @@ reshctl disable ctrl_r_binding ### View the recorded history -Resh history is saved to `~/.resh_history.json` +FIXME: redo/update this section + +Resh history is saved to: `~/.resh_history.json` -Each line is a JSON that represents one executed command line. +Each line is a versioned JSON that represents one executed command line. This is how I view it `tail -f ~/.resh_history.json | jq` or `jq < ~/.resh_history.json`. -You can install `jq` using your favourite package manager or you can use other JSON parser to view the history. +You can install `jq` using your favorite package manager or you can use other JSON parser to view the history. ![screenshot](img/screen.png) @@ -115,13 +117,13 @@ You can install `jq` using your favourite package manager or you can use other J ### Q: I use bash on macOS and resh doesn't work -**A:** You have to add `[ -f ~/.bashrc ] && . ~/.bashrc` to your `~/.bash_profile`. +**A:** Add line `[ -f ~/.bashrc ] && . ~/.bashrc` to your `~/.bash_profile`. -**Long Answer:** Under macOS bash shell only loads `~/.bash_profile` because every shell runs as login shell. I will definitely work around this in the future but since this doesn't affect many people I decided to not solve this issue at the moment. +**Long Answer:** Under macOS bash shell only loads `~/.bash_profile` because every shell runs as login shell. ## Issues and ideas -Please do create issues if you encounter any problems or if you have a suggestions: https://github.com/curusarn/resh/issues +Please do create issues if you encounter any problems or if you have suggestions: https://github.com/curusarn/resh/issues ## Uninstallation diff --git a/cmd/collect/main.go b/cmd/collect/main.go index 0bfbed2..6c4aae1 100644 --- a/cmd/collect/main.go +++ b/cmd/collect/main.go @@ -69,11 +69,11 @@ func main() { os.Exit(0) } if *requireVersion != "" && *requireVersion != version { - out.FatalVersionMismatch(version, *requireVersion) + out.FatalTerminalVersionMismatch(version, *requireVersion) } if *requireRevision != "" && *requireRevision != commit { // this is only relevant for dev versions so we can reuse FatalVersionMismatch() - out.FatalVersionMismatch("revision "+commit, "revision "+*requireVersion) + out.FatalTerminalVersionMismatch("revision "+commit, "revision "+*requireVersion) } time, err := strconv.ParseFloat(*time_, 64) diff --git a/cmd/control/cmd/version.go b/cmd/control/cmd/version.go index 9d86f20..bf6fafd 100644 --- a/cmd/control/cmd/version.go +++ b/cmd/control/cmd/version.go @@ -3,7 +3,7 @@ package cmd import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "strconv" @@ -22,27 +22,19 @@ var versionCmd = &cobra.Command{ commitEnv := getEnvVarWithDefault("__RESH_REVISION", "") printVersion("This terminal session", versionEnv, commitEnv) - // TODO: use output.Output.Error... for these resp, err := getDaemonStatus(config.Port) if err != nil { - fmt.Fprintf(os.Stderr, "\nERROR: Resh-daemon didn't respond - it's probably not running.\n\n") - fmt.Fprintf(os.Stderr, "-> Try restarting this terminal window to bring resh-daemon back up.\n") - fmt.Fprintf(os.Stderr, "-> If the problem persists you can check resh-daemon logs: ~/.resh/daemon.log\n") - fmt.Fprintf(os.Stderr, "-> You can file an issue at: https://github.com/curusarn/resh/issues\n") + out.ErrorDaemonNotRunning(err) return } printVersion("Currently running daemon", resp.Version, resp.Commit) if version != resp.Version { - fmt.Fprintf(os.Stderr, "\nWARN: Resh-daemon is running in different version than is installed now - it looks like something went wrong during resh update.\n\n") - fmt.Fprintf(os.Stderr, "-> Kill resh-daemon and then launch a new terminal window to fix that.\n") - fmt.Fprintf(os.Stderr, " $ pkill resh-daemon\n") - fmt.Fprintf(os.Stderr, "-> You can file an issue at: https://github.com/curusarn/resh/issues\n") + out.ErrorDaemonVersionMismatch(version, resp.Version) return } if version != versionEnv { - fmt.Fprintf(os.Stderr, "\nWARN: This terminal session was started with different resh version than is installed now - it looks like you updated resh and didn't restart this terminal.\n\n") - fmt.Fprintf(os.Stderr, "-> Restart this terminal window to fix that.\n") + out.ErrorTerminalVersionMismatch(version, versionEnv) return } @@ -69,7 +61,7 @@ func getDaemonStatus(port int) (msg.StatusResponse, error) { return mess, err } defer resp.Body.Close() - jsn, err := ioutil.ReadAll(resp.Body) + jsn, err := io.ReadAll(resp.Body) if err != nil { out.Fatal("Error while reading 'daemon /status' response", err) } diff --git a/cmd/postcollect/main.go b/cmd/postcollect/main.go index b1d9f6f..a87f4af 100644 --- a/cmd/postcollect/main.go +++ b/cmd/postcollect/main.go @@ -57,11 +57,11 @@ func main() { os.Exit(0) } if *requireVersion != "" && *requireVersion != version { - out.FatalVersionMismatch(version, *requireVersion) + out.FatalTerminalVersionMismatch(version, *requireVersion) } if *requireRevision != "" && *requireRevision != commit { // this is only relevant for dev versions so we can reuse FatalVersionMismatch() - out.FatalVersionMismatch("revision "+commit, "revision "+*requireVersion) + out.FatalTerminalVersionMismatch("revision "+commit, "revision "+*requireVersion) } timeAfter, err := strconv.ParseFloat(*rta, 64) diff --git a/cmd/session-init/main.go b/cmd/session-init/main.go index f7c5293..6580e4a 100644 --- a/cmd/session-init/main.go +++ b/cmd/session-init/main.go @@ -49,11 +49,11 @@ func main() { os.Exit(0) } if *requireVersion != "" && *requireVersion != version { - out.FatalVersionMismatch(version, *requireVersion) + out.FatalTerminalVersionMismatch(version, *requireVersion) } if *requireRevision != "" && *requireRevision != commit { // this is only relevant for dev versions so we can reuse FatalVersionMismatch() - out.FatalVersionMismatch("revision "+commit, "revision "+*requireVersion) + out.FatalTerminalVersionMismatch("revision "+commit, "revision "+*requireVersion) } rec := recordint.SessionInit{ diff --git a/internal/output/output.go b/internal/output/output.go index 817494a..610fdfc 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -55,6 +55,14 @@ It looks like you updated resh and didn't restart this terminal. ` +var msgDaemonVersionMismatch = `Resh-daemon is running in different version than is installed now. +It looks like something went wrong during resh update. + + -> Kill resh-daemon and then launch a new terminal window to fix that: pkill resh-daemon + -> You can create an issue at: https://github.com/curusarn/resh/issues + +` + func (f *Output) ErrorDaemonNotRunning(err error) { fmt.Fprintf(os.Stderr, "%s: %s", f.ErrPrefix, msgDaemonNotRunning) f.Logger.Error("Daemon is not running", zap.Error(err)) @@ -65,7 +73,7 @@ func (f *Output) FatalDaemonNotRunning(err error) { f.Logger.Fatal("Daemon is not running", zap.Error(err)) } -func (f *Output) ErrorVersionMismatch(installedVer, terminalVer string) { +func (f *Output) ErrorTerminalVersionMismatch(installedVer, terminalVer string) { fmt.Fprintf(os.Stderr, "%s: %s\n\n(installed version: %s, this terminal version: %s)", f.ErrPrefix, msgVersionMismatch, installedVer, terminalVer) f.Logger.Fatal("Version mismatch", @@ -73,10 +81,26 @@ func (f *Output) ErrorVersionMismatch(installedVer, terminalVer string) { zap.String("terminal", terminalVer)) } -func (f *Output) FatalVersionMismatch(installedVer, terminalVer string) { +func (f *Output) FatalTerminalVersionMismatch(installedVer, terminalVer string) { fmt.Fprintf(os.Stderr, "%s: %s\n(installed version: %s, this terminal version: %s)\n", f.ErrPrefix, msgVersionMismatch, installedVer, terminalVer) f.Logger.Fatal("Version mismatch", zap.String("installed", installedVer), zap.String("terminal", terminalVer)) } + +func (f *Output) ErrorDaemonVersionMismatch(installedVer, daemonVer string) { + fmt.Fprintf(os.Stderr, "%s: %s\n(installed version: %s, running daemon version: %s)\n", + f.ErrPrefix, msgDaemonVersionMismatch, installedVer, daemonVer) + f.Logger.Error("Version mismatch", + zap.String("installed", installedVer), + zap.String("daemon", daemonVer)) +} + +func (f *Output) FatalDaemonVersionMismatch(installedVer, daemonVer string) { + fmt.Fprintf(os.Stderr, "%s: %s\n(installed version: %s, running daemon version: %s)\n", + f.ErrPrefix, msgDaemonVersionMismatch, installedVer, daemonVer) + f.Logger.Fatal("Version mismatch", + zap.String("installed", installedVer), + zap.String("daemon", daemonVer)) +}