diff --git a/Makefile b/Makefile index d687a2a..d73dabd 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,30 @@ +SHELL=/bin/bash -build: submodules/bash-preexec/bash-preexec.sh - @echo "build" +build: submodules resh-collect -install: build - @echo "install" +install: build $(HOME)/.resh $(HOME)/.resh/bin + cp submodules/bash-preexec/bash-preexec.sh ~/.bash-preexec.sh + cp src/bashrc.sh ~/.resh/bashrc + cp resh-collect ~/.resh/bin/ + grep '[[ -f ~/.resh/bashrc ]] && source ~/.resh/bashrc' ~/.bashrc ||\ + echo '[[ -f ~/.resh/bashrc ]] && source ~/.resh/bashrc' >> ~/.bashrc + grep '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' ~/.bashrc ||\ + echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc + +resh-collect: src/resh-collect.go + go build -o resh-collect src/resh-collect.go + +$(HOME)/.resh: + mkdir $(HOME)/.resh + +$(HOME)/.resh/bin: + mkdir $(HOME)/.resh/bin + +.PHONY: submodules build install + +submodules: submodules/bash-preexec/bash-preexec.sh + # this is always run and updates submodules + git submodule update --recursive submodules/%: - -git submodule init - git submodule update + git submodule init diff --git a/src/bashrc.sh b/src/bashrc.sh new file mode 100644 index 0000000..5f64f20 --- /dev/null +++ b/src/bashrc.sh @@ -0,0 +1,13 @@ + +PATH=$PATH:~/.resh/bin + +preexec() { + __RESH_PWD="$PWD" + __RESH_CMDLINE="$1" +} + +precmd() { + __RESH_EXIT_CODE=$? + resh-collect $__RESH_EXIT_CODE "$PWD" "$__RESH_CMDLINE" +} + diff --git a/src/resh-collect.go b/src/resh-collect.go new file mode 100644 index 0000000..5b902e8 --- /dev/null +++ b/src/resh-collect.go @@ -0,0 +1,67 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "log" + "strconv" + "strings" +) + +type record struct { + CmdLine string + Pwd string + GitWorkTree string + Shell string + ExitCode int + //Logs string[] +} + +func main() { + exitCode, err := strconv.Atoi(os.Args[1]) + if err != nil { + // log this + log.Fatal("First arg is not a number! (expecting $?)", err) + } + pwd := os.Args[2] + cmdLine := os.Args[3] + rec := record{ + CmdLine: cmdLine, + Pwd: pwd, + GitWorkTree: getGitDir(), + Shell: os.Getenv("SHELL"), + ExitCode: exitCode, + } + rec.send() +} + +func getGitDir() string { + // assume we are in pwd + gitWorkTree := os.Getenv("GIT_WORK_TREE") + + if gitWorkTree != "" { + return gitWorkTree + } + // we should look up the git directory ourselves + // OR leave it to resh daemon to not slow down user + out, err := exec.Command("git", "rev-parse", "--show-toplevel").Output() + if err != nil { + if exitError, ok := err.(*exec.ExitError); ok { + if exitError.ExitCode() == 128 { + return "" + } + log.Fatal("git cmd failed") + } else { + log.Fatal("git cmd failed w/o exit code") + } + } + return strings.TrimSuffix(string(out), "\n") +} + +func (r record) send() { + fmt.Println("cmd:", r.CmdLine) + fmt.Println("pwd:", r.Pwd) + fmt.Println("git:", r.GitWorkTree) + fmt.Println("exit_code:", r.ExitCode) +}