Skip to content

Commit

Permalink
update reshctl version to use output package
Browse files Browse the repository at this point in the history
  • Loading branch information
curusarn committed Jan 19, 2023
1 parent 5e0af13 commit baf7cde
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 32 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Context-based replacement/enhancement for zsh and bash shell history
<!-- Better zsh history -->
<!-- PWD Directory -->

**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`)

Expand Down Expand Up @@ -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.

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions cmd/collect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 5 additions & 13 deletions cmd/control/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"strconv"
Expand All @@ -22,27 +22,19 @@ var versionCmd = &cobra.Command{
commitEnv := getEnvVarWithDefault("__RESH_REVISION", "<unknown>")
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
}

Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/postcollect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/session-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
28 changes: 26 additions & 2 deletions internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -65,18 +73,34 @@ 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",
zap.String("installed", installedVer),
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))
}

0 comments on commit baf7cde

Please sign in to comment.