diff --git a/Makefile b/Makefile index 69750be..658df2b 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ SHELL=/bin/bash build: submodules resh-collect resh-daemon -install: build | $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config/resh +install: build | $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config $(HOME)/.resh/resh-uuid cp submodules/bash-preexec/bash-preexec.sh ~/.bash-preexec.sh -f cp config.toml ~/.config/resh.toml -f cp bashrc.sh ~/.resh/bashrc -f @@ -23,9 +23,12 @@ resh-collect: collect/resh-collect.go common/resh-common.go go build -o $@ $< -$(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config/resh: +$(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config: mkdir -p $@ +$(HOME)/.resh/resh-uuid: + cat /proc/sys/kernel/random/uuid > $@ + .PHONY: submodules build install diff --git a/bashrc.sh b/bashrc.sh index e1d0854..54b77d7 100644 --- a/bashrc.sh +++ b/bashrc.sh @@ -2,7 +2,7 @@ PATH=$PATH:~/.resh/bin export __RESH_RT_SESSION=$EPOCHREALTIME export __RESH_RT_SESS_SINCE_BOOT=$(cat /proc/uptime | cut -d' ' -f1) -export __RESH_SESSION_ID=$RANDOM +export __RESH_SESSION_ID=$(cat /proc/sys/kernel/random/uuid) nohup resh-daemon &>/dev/null & disown preexec() { @@ -32,6 +32,12 @@ preexec() { __RESH_OSTYPE="$OSTYPE" __RESH_MACHTYPE="$MACHTYPE" __RESH_SHLVL="$SHLVL" + __RESH_GIT_CDUP="$(git rev-parse --show-cdup 2>/dev/null)" + __RESH_GIT_CDUP_EXIT_CODE=$? + __RESH_GIT_REMOTE="$(git remote get-url origin 2>/dev/null)" + __RESH_GIT_REMOTE_EXIT_CODE=$? + #__RESH_GIT_TOPLEVEL="$(git rev-parse --show-toplevel)" + #__RESH_GIT_TOPLEVEL_EXIT_CODE=$? # time __RESH_TZ_BEFORE=$(date +%:z) @@ -53,6 +59,7 @@ precmd() { -login "$__RESH_LOGIN" \ -path "$__RESH_PATH" \ -pwd "$__RESH_PWD" \ + -realPwd "$(realpath $__RESH_PWD)" \ -shell "$__RESH_SHELL" \ -term "$__RESH_TERM" \ -pid "$__RESH_PID" -shellPid "$__RESH_SHELL_PID" \ @@ -62,6 +69,10 @@ precmd() { -ostype "$__RESH_OSTYPE" \ -machtype "$__RESH_MACHTYPE" \ -shlvl "$__RESH_SHLVL" \ + -gitCdup "$__RESH_GIT_CDUP" \ + -gitCdupExitCode "$__RESH_GIT_CDUP_EXIT_CODE" \ + -gitRemote "$__RESH_GIT_REMOTE" \ + -gitRemoteExitCode "$__RESH_GIT_REMOTE_EXIT_CODE" \ -realtimeBefore "$__RESH_RT_BEFORE" \ -realtimeAfter "$__RESH_RT_AFTER" \ -realtimeSession "$__RESH_RT_SESSION" \ diff --git a/collect/resh-collect.go b/collect/resh-collect.go index edbfb10..ef66457 100644 --- a/collect/resh-collect.go +++ b/collect/resh-collect.go @@ -9,8 +9,8 @@ import ( "io/ioutil" "log" "net/http" - "os" - "os/exec" + // "os" + // "os/exec" "os/user" "path/filepath" "strconv" @@ -21,6 +21,9 @@ func main() { usr, _ := user.Current() dir := usr.HomeDir configPath := filepath.Join(dir, "/.config/resh.toml") + reshUuidPath := filepath.Join(dir, "/.resh/resh-uuid") + + machineIdPath := "/etc/machine-id" var config common.Config if _, err := toml.DecodeFile(configPath, &config); err != nil { @@ -50,10 +53,16 @@ func main() { windowId := flag.Int("windowId", -1, "$WINDOWID - session id") shlvl := flag.Int("shlvl", -1, "$SHLVL") + realPwd := flag.String("realPwd", "", "realpath $PWD") host := flag.String("host", "", "$HOSTNAME") hosttype := flag.String("hosttype", "", "$HOSTTYPE") ostype := flag.String("ostype", "", "$OSTYPE") machtype := flag.String("machtype", "", "$MACHTYPE") + gitCdup := flag.String("gitCdup", "", "git rev-parse --show-cdup") + gitRemote := flag.String("gitRemote", "", "git remote get-url origin") + + gitCdupExitCode := flag.Int("gitCdupExitCode", -1, "... $?") + gitRemoteExitCode := flag.Int("gitRemoteExitCode", -1, "... $?") // before after timezoneBefore := flag.String("timezoneBefore", "", "") @@ -86,6 +95,11 @@ func main() { realtimeBeforeLocal := realtimeBefore + timezoneBeforeOffset realtimeAfterLocal := realtimeAfter + timezoneAfterOffset + gitDir, gitRealDir := getGitDirs(*gitCdup, *gitCdupExitCode, *pwd) + if *gitRemoteExitCode != 0 { + *gitRemote = "" + } + rec := common.Record{ // core CmdLine: *cmdLine, @@ -105,14 +119,15 @@ func main() { Term: *term, // non-posix - Pid: *pid, - ShellPid: *shellPid, - WindowId: *windowId, - Host: *host, - Hosttype: *hosttype, - Ostype: *ostype, - Machtype: *machtype, - Shlvl: *shlvl, + RealpathPwd: *realPwd, + Pid: *pid, + ShellPid: *shellPid, + WindowId: *windowId, + Host: *host, + Hosttype: *hosttype, + Ostype: *ostype, + Machtype: *machtype, + Shlvl: *shlvl, // before after TimezoneBefore: *timezoneBefore, @@ -127,8 +142,11 @@ func main() { RealtimeSinceSessionStart: realtimeSinceSessionStart, RealtimeSinceBoot: realtimeSinceBoot, - GitWorkTree: getGitDir(), - MachineId: getMachineId(), + GitDir: gitDir, + GitRealDir: gitRealDir, + GitOriginRemote: *gitRemote, + MachineId: readFileContent(machineIdPath), + ReshUuid: readFileContent(reshUuidPath), } sendRecord(rec, strconv.Itoa(config.Port)) } @@ -153,35 +171,25 @@ func sendRecord(r common.Record, port string) { } } -func getMachineId() string { - dat, err := ioutil.ReadFile("/etc/machine-id") +func readFileContent(path string) string { + dat, err := ioutil.ReadFile(path) if err != nil { - log.Fatal("failed to open /etc/machine-id") + log.Fatal("failed to open " + path) } return strings.TrimSuffix(string(dat), "\n") } -func getGitDir() string { - // assume we are in pwd - gitWorkTree := os.Getenv("GIT_WORK_TREE") - - if gitWorkTree != "" { - return gitWorkTree +func getGitDirs(cdup string, exitCode int, pwd string) (string, string) { + if exitCode != 0 { + return "", "" } - // 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() + abspath := filepath.Clean(filepath.Join(pwd, cdup)) + realpath, err := filepath.EvalSymlinks(abspath) 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") - } + log.Println("err while handling git dir paths:", err) + return "", "" } - return strings.TrimSuffix(string(out), "\n") + return abspath, realpath } func getTimezoneOffsetInSeconds(zone string) float64 { @@ -199,3 +207,41 @@ func getTimezoneOffsetInSeconds(zone string) float64 { secs := ((hours * 60) + mins) * 60 return float64(secs) } + +// func getGitRemote() string { +// out, err := exec.Command("git", "remote", "get-url", "origin").Output() +// if err != nil { +// if exitError, ok := err.(*exec.ExitError); ok { +// if exitError.ExitCode() == 128 { +// return "" +// } +// log.Fatal("git remote cmd failed") +// } else { +// log.Fatal("git remote cmd failed w/o exit code") +// } +// } +// return strings.TrimSuffix(string(out), "\n") +// } +// +// 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 rev-parse cmd failed") +// } else { +// log.Fatal("git rev-parse cmd failed w/o exit code") +// } +// } +// return strings.TrimSuffix(string(out), "\n") +// } diff --git a/common/resh-common.go b/common/resh-common.go index f1d87e1..70772e0 100644 --- a/common/resh-common.go +++ b/common/resh-common.go @@ -18,6 +18,7 @@ type Record struct { Term string `json:"term"` // non-posix"` + RealpathPwd string `json:"realpathPwd"` Pid int `json:"pid"` ShellPid int `json:"shellPid"` WindowId int `json:"windowId"` @@ -41,8 +42,11 @@ type Record struct { RealtimeSinceBoot float64 `json:"realtimeSinceBoot"` //Logs []string `json: "logs"` - GitWorkTree string `json:"gitWorkTree"` + GitDir string `json:"gitDir"` + GitRealDir string `json:"gitRealDir"` + GitOriginRemote string `json:"gitOriginRemote"` MachineId string `json:"machineId"` + ReshUuid string `json:"reshUuid"` } type Config struct {