resh uuid, git, abspaths and realpaths

pull/3/head
Simon Let 7 years ago
parent 8b40a332c1
commit 3b90a42240
  1. 7
      Makefile
  2. 13
      bashrc.sh
  3. 96
      collect/resh-collect.go
  4. 6
      common/resh-common.go

@ -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

@ -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" \

@ -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,6 +119,7 @@ func main() {
Term: *term,
// non-posix
RealpathPwd: *realPwd,
Pid: *pid,
ShellPid: *shellPid,
WindowId: *windowId,
@ -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")
// }

@ -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 {

Loading…
Cancel
Save