mirror of https://github.com/curusarn/resh
parent
d036f86c7d
commit
617a375236
@ -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 |
|
||||||
} |
|
||||||
@ -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 |
||||||
|
} |
||||||
Loading…
Reference in new issue