reshctl removal

pull/184/head
Simon Let 4 years ago committed by Simon Let
parent d036f86c7d
commit 617a375236
  1. 80
      cmd/control/cmd/enable.go
  2. 10
      cmd/control/cmd/root.go
  3. 54
      cmd/control/cmd/sanitize.go
  4. 72
      cmd/control/cmd/status.go
  5. 86
      cmd/control/cmd/version.go
  6. 7
      pkg/records/records.go
  7. 4
      scripts/hooks.sh
  8. 4
      scripts/install.sh
  9. 77
      scripts/reshctl.sh
  10. 5
      scripts/widgets.sh

@ -1,80 +0,0 @@
package cmd
import (
"fmt"
"os"
"os/user"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/curusarn/resh/cmd/control/status"
"github.com/curusarn/resh/pkg/cfg"
"github.com/spf13/cobra"
)
// Enable commands
var enableCmd = &cobra.Command{
Use: "enable",
Short: "enable RESH features (bindings)",
}
var enableControlRBindingCmd = &cobra.Command{
Use: "ctrl_r_binding",
Short: "enable RESH-CLI binding for Ctrl+R",
Run: func(cmd *cobra.Command, args []string) {
exitCode = enableDisableControlRBindingGlobally(true)
if exitCode == status.Success {
exitCode = status.EnableControlRBinding
}
},
}
// Disable commands
var disableCmd = &cobra.Command{
Use: "disable",
Short: "disable RESH features (bindings)",
}
var disableControlRBindingCmd = &cobra.Command{
Use: "ctrl_r_binding",
Short: "disable RESH-CLI binding for Ctrl+R",
Run: func(cmd *cobra.Command, args []string) {
exitCode = enableDisableControlRBindingGlobally(false)
if exitCode == status.Success {
exitCode = status.DisableControlRBinding
}
},
}
func enableDisableControlRBindingGlobally(value bool) status.Code {
usr, _ := user.Current()
dir := usr.HomeDir
configPath := filepath.Join(dir, ".config/resh.toml")
var config cfg.Config
if _, err := toml.DecodeFile(configPath, &config); err != nil {
fmt.Println("Error reading config", err)
return status.Fail
}
if config.BindControlR != value {
config.BindControlR = value
f, err := os.Create(configPath)
if err != nil {
fmt.Println("Error: Failed to create/open file:", configPath, "; error:", err)
return status.Fail
}
defer f.Close()
if err := toml.NewEncoder(f).Encode(config); err != nil {
fmt.Println("Error: Failed to encode and write the config values to hdd. error:", err)
return status.Fail
}
}
if value {
fmt.Println("RESH SEARCH app Ctrl+R binding: ENABLED")
} else {
fmt.Println("RESH SEARCH app Ctrl+R binding: DISABLED")
}
return status.Success
}

@ -41,12 +41,6 @@ func Execute(ver, com string) status.Code {
// log.SetFlags(log.LstdFlags | log.Lmicroseconds)
}
rootCmd.AddCommand(enableCmd)
enableCmd.AddCommand(enableControlRBindingCmd)
rootCmd.AddCommand(disableCmd)
disableCmd.AddCommand(disableControlRBindingCmd)
rootCmd.AddCommand(completionCmd)
completionCmd.AddCommand(completionBashCmd)
completionCmd.AddCommand(completionZshCmd)
@ -56,13 +50,11 @@ func Execute(ver, com string) status.Code {
debugCmd.AddCommand(debugInspectCmd)
debugCmd.AddCommand(debugOutputCmd)
rootCmd.AddCommand(statusCmd)
rootCmd.AddCommand(versionCmd)
updateCmd.Flags().BoolVar(&betaFlag, "beta", false, "Update to latest version even if it's beta.")
rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(sanitizeCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
return status.Fail

@ -1,54 +0,0 @@
package cmd
import (
"fmt"
"os"
"os/exec"
"os/user"
"github.com/curusarn/resh/cmd/control/status"
"github.com/spf13/cobra"
)
var sanitizeCmd = &cobra.Command{
Use: "sanitize",
Short: "produce a sanitized version of your RESH history",
Run: func(cmd *cobra.Command, args []string) {
exitCode = status.Success
usr, _ := user.Current()
dir := usr.HomeDir
fmt.Println()
fmt.Println(" HOW IT WORKS")
fmt.Println(" In sanitized history, all sensitive information is replaced with its SHA256 hashes.")
fmt.Println()
fmt.Println("Creating sanitized history files ...")
fmt.Println(" * ~/resh_history_sanitized.json (full lengh hashes)")
execCmd := exec.Command("resh-sanitize", "-trim-hashes", "0", "--output", dir+"/resh_history_sanitized.json")
execCmd.Stdout = os.Stdout
execCmd.Stderr = os.Stderr
err := execCmd.Run()
if err != nil {
exitCode = status.Fail
}
fmt.Println(" * ~/resh_history_sanitized_trim12.json (12 char hashes)")
execCmd = exec.Command("resh-sanitize", "-trim-hashes", "12", "--output", dir+"/resh_history_sanitized_trim12.json")
execCmd.Stdout = os.Stdout
execCmd.Stderr = os.Stderr
err = execCmd.Run()
if err != nil {
exitCode = status.Fail
}
fmt.Println()
fmt.Println("Please direct all questions and/or issues to: https://github.com/curusarn/resh/issues")
fmt.Println()
fmt.Println("Please look at the resulting sanitized history using commands below.")
fmt.Println(" * Pretty print JSON")
fmt.Println(" cat ~/resh_history_sanitized_trim12.json | jq")
fmt.Println()
fmt.Println(" * Only show commands, don't show metadata")
fmt.Println(" cat ~/resh_history_sanitized_trim12.json | jq '.[\"cmdLine\"]'")
fmt.Println()
},
}

@ -1,72 +0,0 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/curusarn/resh/cmd/control/status"
"github.com/curusarn/resh/pkg/msg"
"github.com/spf13/cobra"
)
var statusCmd = &cobra.Command{
Use: "status",
Short: "show RESH status",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("resh " + version)
fmt.Println()
fmt.Println("Resh versions ...")
fmt.Println(" * installed: " + version + " (" + commit + ")")
versionEnv, found := os.LookupEnv("__RESH_VERSION")
if found == false {
versionEnv = "UNKNOWN!"
}
commitEnv, found := os.LookupEnv("__RESH_REVISION")
if found == false {
commitEnv = "unknown"
}
fmt.Println(" * this shell session: " + versionEnv + " (" + commitEnv + ")")
resp, err := getDaemonStatus(config.Port)
if err != nil {
fmt.Println(" * RESH-DAEMON IS NOT RUNNING")
fmt.Println(" * Please REPORT this here: https://github.com/curusarn/resh/issues")
fmt.Println(" * Please RESTART this terminal window")
exitCode = status.Fail
return
}
fmt.Println(" * daemon: " + resp.Version + " (" + resp.Commit + ")")
if version != resp.Version || version != versionEnv {
fmt.Println(" * THERE IS A MISMATCH BETWEEN VERSIONS!")
fmt.Println(" * Please REPORT this here: https://github.com/curusarn/resh/issues")
fmt.Println(" * Please RESTART this terminal window")
}
exitCode = status.ReshStatus
},
}
func getDaemonStatus(port int) (msg.StatusResponse, error) {
mess := msg.StatusResponse{}
url := "http://localhost:" + strconv.Itoa(port) + "/status"
resp, err := http.Get(url)
if err != nil {
return mess, err
}
defer resp.Body.Close()
jsn, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error while reading 'daemon /status' response:", err)
}
err = json.Unmarshal(jsn, &mess)
if err != nil {
log.Fatal("Error while decoding 'daemon /status' response:", err)
}
return mess, nil
}

@ -0,0 +1,86 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/curusarn/resh/cmd/control/status"
"github.com/curusarn/resh/pkg/msg"
"github.com/spf13/cobra"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "show RESH version",
Run: func(cmd *cobra.Command, args []string) {
printVersion("Installed", version, commit)
versionEnv := getEnvVarWithDefault("__RESH_VERSION", "<unknown>")
commitEnv := getEnvVarWithDefault("__RESH_REVISION", "<unknown>")
printVersion("This terminal session", versionEnv, commitEnv)
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")
exitCode = status.Fail
return
}
printVersion("Daemon", resp.Version, resp.Commit)
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")
return
}
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")
return
}
exitCode = status.ReshStatus
},
}
func printErrExtras() {
}
func printVersion(title, version, commit string) {
fmt.Printf("%s: %s (commit: %s)\n", title, version, commit)
}
func getEnvVarWithDefault(varName, defaultValue string) string {
val, found := os.LookupEnv(varName)
if !found {
return defaultValue
}
return val
}
func getDaemonStatus(port int) (msg.StatusResponse, error) {
mess := msg.StatusResponse{}
url := "http://localhost:" + strconv.Itoa(port) + "/status"
resp, err := http.Get(url)
if err != nil {
return mess, err
}
defer resp.Body.Close()
jsn, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error while reading 'daemon /status' response:", err)
}
err = json.Unmarshal(jsn, &mess)
if err != nil {
log.Fatal("Error while decoding 'daemon /status' response:", err)
}
return mess, nil
}

@ -560,9 +560,10 @@ func LoadFromFile(fname string) []Record {
// log.Println("records: Loaded lines - count:", i)
if encounteredErrors > 0 {
// fix errors in the history file
log.Printf("There were %d decoding errors, the first error happend on line %d/%d", encounteredErrors, firstErrLine, i)
log.Println("Backing up current history file ...")
err := copyFile(fname, fname+".bak")
log.Printf("There were %d decoding errors, the first error happend on line %d/%d\n", encounteredErrors, firstErrLine, i)
fnameBak := fname + ".bak"
log.Printf("Backing up current history file to %s\n", fnameBak)
err := copyFile(fname, fnameBak)
if err != nil {
log.Fatalln("Failed to backup history file with decode errors")
}

@ -18,9 +18,7 @@ __resh_collect() {
local __RESH_COLS="$COLUMNS"
local __RESH_LANG="$LANG"
local __RESH_LC_ALL="$LC_ALL"
# other LC ?
local __RESH_LINES="$LINES"
# __RESH_PATH="$PATH"
local __RESH_PWD="$PWD"
# non-posix
@ -29,8 +27,6 @@ __resh_collect() {
local __RESH_GIT_CDUP_EXIT_CODE=$?
local __RESH_GIT_REMOTE; __RESH_GIT_REMOTE="$(git remote get-url origin 2>/dev/null)"
local __RESH_GIT_REMOTE_EXIT_CODE=$?
#__RESH_GIT_TOPLEVEL="$(git rev-parse --show-toplevel)"
#__RESH_GIT_TOPLEVEL_EXIT_CODE=$?
if [ -n "${ZSH_VERSION-}" ]; then
# assume Zsh

@ -218,10 +218,6 @@ RESH SEARCH APPLICATION = Redesigned reverse search that actually works
At first, the search application will use the standard shell history without context.
All history recorded from now on will have context which will by the RESH SEARCH app.
Enable/disable Ctrl+R binding using reshctl command:
$ reshctl enable ctrl_r_binding
$ reshctl disable ctrl_r_binding
CHECK FOR UPDATES
To check for (and install) updates use reshctl command:
$ reshctl update

@ -77,79 +77,4 @@ resh() {
echo "$buffer" >| "$fpath_last_run"
echo "resh-cli failed - check '$fpath_last_run' and '~/.resh/cli.log'"
fi
}
reshctl() {
# export current shell because resh-control needs to know
export __RESH_ctl_shell=$__RESH_SHELL
# run resh-control aka the real reshctl
resh-control "$@"
# modify current shell session based on exit status
local _status=$?
# echo $_status
# unexport current shell
unset __RESH_ctl_shell
case "$_status" in
0|1)
# success | fail
return "$_status"
;;
# enable
# 30)
# # enable all
# __resh_bind_all
# return 0
# ;;
32)
# enable control R
__resh_bind_control_R
return 0
;;
# disable
# 40)
# # disable all
# __resh_unbind_all
# return 0
# ;;
42)
# disable control R
__resh_unbind_control_R
return 0
;;
50)
# reload rc files
. ~/.resh/shellrc
return 0
;;
51)
# inspect session history
# reshctl debug inspect N
resh-inspect --sessionID "$__RESH_SESSION_ID" --count "${3-10}"
return 0
;;
52)
# show status
echo
echo 'Control R binding ...'
if [ "$(resh-config --key BindControlR)" = true ]; then
echo ' * future sessions: ENABLED'
else
echo ' * future sessions: DISABLED'
fi
if [ "${__RESH_control_R_bind_enabled-0}" != 0 ]; then
echo ' * this session: ENABLED'
else
echo ' * this session: DISABLED'
fi
return 0
;;
*)
echo "reshctl() FATAL ERROR: unknown status ($_status)" >&2
echo "Possibly caused by version mismatch between installed resh and resh in this session." >&2
echo "Please REPORT this issue here: https://github.com/curusarn/resh/issues" >&2
echo "Please RESTART your terminal window." >&2
return "$_status"
;;
esac
}
}

@ -9,10 +9,7 @@ __resh_widget_control_R() {
# shellcheck disable=2034
__bp_preexec_interactive_mode="on"
# local __RESH_PREFIX=${BUFFER:0:CURSOR}
# __RESH_HIST_RECALL_ACTIONS="$__RESH_HIST_RECALL_ACTIONS;control_R:$__RESH_PREFIX"
local PREVBUFFER=$BUFFER
__RESH_HIST_RECALL_ACTIONS="$__RESH_HIST_RECALL_ACTIONS|||control_R:$BUFFER"
local status_code
local git_remote; git_remote="$(git remote get-url origin 2>/dev/null)"
@ -42,8 +39,6 @@ __resh_widget_control_R() {
BUFFER="$PREVBUFFER"
fi
CURSOR=${#BUFFER}
# recorded to history
__RESH_HIST_PREV_LINE=${BUFFER}
}
__resh_widget_control_R_compat() {

Loading…
Cancel
Save