From 77d94c9e12348efb0139ec56cc233717713589ac Mon Sep 17 00:00:00 2001 From: Simon Let Date: Fri, 4 Oct 2019 14:31:19 +0200 Subject: [PATCH] Save std{out,err} of last run, add reshctl debug reshctl debug reads all the last_run files --- Makefile | 2 +- cmd/control/cmd/debug.go | 44 ++++++++++++++++++++++++++++++++++++++++ cmd/control/cmd/root.go | 2 ++ scripts/shellrc.sh | 16 +++++++-------- 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 cmd/control/cmd/debug.go diff --git a/Makefile b/Makefile index b11529a..277de70 100644 --- a/Makefile +++ b/Makefile @@ -90,7 +90,7 @@ install: build submodules/bash-preexec/bash-preexec.sh scripts/shellrc.sh conf/c grep '[ -f ~/.resh/shellrc ] && source ~/.resh/shellrc' ~/.zshrc ||\ echo '[ -f ~/.resh/shellrc ] && source ~/.resh/shellrc' >> ~/.zshrc # Restarting resh daemon ... - [ ! -f ~/.resh/resh.pid ] || kill -SIGTERM $$(cat ~/.resh/resh.pid) + -[ ! -f ~/.resh/resh.pid ] || kill -SIGTERM $$(cat ~/.resh/resh.pid) nohup resh-daemon &>/dev/null & disown # Final touch touch ~/.resh_history.json diff --git a/cmd/control/cmd/debug.go b/cmd/control/cmd/debug.go new file mode 100644 index 0000000..c4dd299 --- /dev/null +++ b/cmd/control/cmd/debug.go @@ -0,0 +1,44 @@ +package cmd + +import ( + "fmt" + "io/ioutil" + "os/user" + "path/filepath" + + "github.com/curusarn/resh/cmd/control/status" + "github.com/spf13/cobra" +) + +// completionCmd represents the completion command +var debugCmd = &cobra.Command{ + Use: "debug", + Short: "Shows logs and output from last runs of resh", + Long: "Shows logs and output from last runs of resh", + Run: func(cmd *cobra.Command, args []string) { + files := []string{ + "daemon_last_run_out.txt", + "collect_last_run_out.txt", + "postcollect_last_run_out.txt", + } + usr, _ := user.Current() + dir := usr.HomeDir + reshdir := filepath.Join(dir, ".resh") + for _, fpath := range files { + fpath := filepath.Join(reshdir, fpath) + debugReadFile(fpath) + } + exitCode = status.Success + }, +} + +func debugReadFile(path string) { + fmt.Println("============================================================") + fmt.Println(" filepath:", path) + fmt.Println("============================================================") + dat, err := ioutil.ReadFile(path) + if err != nil { + fmt.Println("ERROR while reading file:", err) + } + fmt.Println(string(dat)) +} diff --git a/cmd/control/cmd/root.go b/cmd/control/cmd/root.go index b83c6c9..b2f57a0 100644 --- a/cmd/control/cmd/root.go +++ b/cmd/control/cmd/root.go @@ -26,6 +26,8 @@ func Execute() status.Code { rootCmd.AddCommand(completionCmd) completionCmd.AddCommand(completionBashCmd) completionCmd.AddCommand(completionZshCmd) + + rootCmd.AddCommand(debugCmd) if err := rootCmd.Execute(); err != nil { fmt.Println(err) return status.Fail diff --git a/scripts/shellrc.sh b/scripts/shellrc.sh index 83db854..739c299 100644 --- a/scripts/shellrc.sh +++ b/scripts/shellrc.sh @@ -40,7 +40,7 @@ __resh_run_daemon() { if [ -n "$ZSH_VERSION" ]; then setopt LOCAL_OPTIONS NO_NOTIFY NO_MONITOR fi - nohup resh-daemon &>/dev/null & disown + nohup resh-daemon &>~/.resh/daemon_last_run_out.txt & disown } # __resh_session_init() { @@ -219,7 +219,7 @@ __resh_preexec() { -osReleaseIdLike "$__RESH_OS_RELEASE_ID_LIKE" \ -osReleaseName "$__RESH_OS_RELEASE_NAME" \ -osReleasePrettyName "$__RESH_OS_RELEASE_PRETTY_NAME" \ - &>~/.resh/client_last_run_out.txt || echo "resh-collect ERROR: $(head -n 1 ~/.resh/client_last_run_out.txt)" + &>~/.resh/collect_last_run_out.txt || echo "resh-collect ERROR: $(head -n 1 ~/.resh/collect_last_run_out.txt)" fi } @@ -233,22 +233,22 @@ __resh_precmd() { __RESH_GIT_REMOTE_AFTER="$(git remote get-url origin 2>/dev/null)" __RESH_GIT_REMOTE_EXIT_CODE_AFTER=$? if [ -n "${__RESH_COLLECT}" ]; then - if [ "$__RESH_VERSION" != "$(resh-collect -version)" ]; then + if [ "$__RESH_VERSION" != "$(resh-postcollect -version)" ]; then # shellcheck source=shellrc.sh source ~/.resh/shellrc - if [ "$__RESH_VERSION" != "$(resh-collect -version)" ]; then + if [ "$__RESH_VERSION" != "$(resh-postcollect -version)" ]; then echo "RESH WARNING: You probably just updated RESH - PLEASE RESTART OR RELOAD THIS TERMINAL SESSION (resh version: $(resh-collect -version); resh version of this terminal session: ${__RESH_VERSION})" else echo "RESH INFO: New RESH shellrc script was loaded - if you encounter any issues please restart this terminal session." fi - elif [ "$__RESH_REVISION" != "$(resh-collect -revision)" ]; then + elif [ "$__RESH_REVISION" != "$(resh-postcollect -revision)" ]; then # shellcheck source=shellrc.sh source ~/.resh/shellrc - if [ "$__RESH_REVISION" != "$(resh-collect -revision)" ]; then + if [ "$__RESH_REVISION" != "$(resh-postcollect -revision)" ]; then echo "RESH WARNING: You probably just updated RESH - PLEASE RESTART OR RELOAD THIS TERMINAL SESSION (resh revision: $(resh-collect -revision); resh revision of this terminal session: ${__RESH_REVISION})" fi fi - if [ "$__RESH_VERSION" = "$(resh-collect -version)" ] && [ "$__RESH_REVISION" = "$(resh-collect -revision)" ]; then + if [ "$__RESH_VERSION" = "$(resh-postcollect -version)" ] && [ "$__RESH_REVISION" = "$(resh-postcollect -revision)" ]; then resh-postcollect -requireVersion "$__RESH_VERSION" \ -requireRevision "$__RESH_REVISION" \ -cmdLine "$__RESH_CMDLINE" \ @@ -262,7 +262,7 @@ __resh_precmd() { -gitRemoteExitCodeAfter "$__RESH_GIT_REMOTE_EXIT_CODE_AFTER" \ -realtimeAfter "$__RESH_RT_AFTER" \ -timezoneAfter "$__RESH_TZ_AFTER" \ - &>~/.resh/client_last_run_out.txt || echo "resh-postcollect ERROR: $(head -n 1 ~/.resh/client_last_run_out.txt)" + &>~/.resh/postcollect_last_run_out.txt || echo "resh-postcollect ERROR: $(head -n 1 ~/.resh/postcollect_last_run_out.txt)" fi fi unset __RESH_COLLECT