Rich Enhanced Shell History - Contextual shell history for zsh and bash
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
resh/collect/resh-collect.go

52 lines
1.2 KiB

package main
import (
"os"
"os/exec"
"log"
"strconv"
"strings"
common "github.com/curusarn/resh/common"
)
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 := common.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")
}