From d77b99ba4218df75751063699a714c004bd78f47 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Thu, 2 May 2019 00:35:32 +0200 Subject: [PATCH] move stuff around, draft of daemon --- Makefile | 19 ++++++++++----- src/bashrc.sh => bashrc.sh | 0 {src => collect}/resh-collect.go | 21 +++------------- common/resh-common.go | 37 ++++++++++++++++++++++++++++ daemon/resh-daemon.go | 41 ++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 24 deletions(-) rename src/bashrc.sh => bashrc.sh (100%) rename {src => collect}/resh-collect.go (76%) create mode 100644 common/resh-common.go create mode 100644 daemon/resh-daemon.go diff --git a/Makefile b/Makefile index d73dabd..06dc880 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,25 @@ SHELL=/bin/bash -build: submodules resh-collect +build: submodules resh-collect resh-daemon 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/ + cp submodules/bash-preexec/bash-preexec.sh ~/.bash-preexec.sh -f + cp bashrc.sh ~/.resh/bashrc -f + cp resh-* ~/.resh/bin/ -f 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 +# -pkill resh-daemon +# resh-daemon & + + +resh-daemon: daemon/resh-daemon.go common/resh-common.go + go build -o $@ $< + +resh-collect: collect/resh-collect.go common/resh-common.go + go build -o $@ $< -resh-collect: src/resh-collect.go - go build -o resh-collect src/resh-collect.go $(HOME)/.resh: mkdir $(HOME)/.resh diff --git a/src/bashrc.sh b/bashrc.sh similarity index 100% rename from src/bashrc.sh rename to bashrc.sh diff --git a/src/resh-collect.go b/collect/resh-collect.go similarity index 76% rename from src/resh-collect.go rename to collect/resh-collect.go index 5b902e8..2ee89fd 100644 --- a/src/resh-collect.go +++ b/collect/resh-collect.go @@ -1,23 +1,14 @@ package main import ( - "fmt" "os" "os/exec" "log" "strconv" "strings" + common "github.com/curusarn/resh/common" ) -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 { @@ -26,14 +17,14 @@ func main() { } pwd := os.Args[2] cmdLine := os.Args[3] - rec := record{ + rec := common.Record{ CmdLine: cmdLine, Pwd: pwd, GitWorkTree: getGitDir(), Shell: os.Getenv("SHELL"), ExitCode: exitCode, } - rec.send() + rec.Send() } func getGitDir() string { @@ -59,9 +50,3 @@ func getGitDir() string { 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) -} diff --git a/common/resh-common.go b/common/resh-common.go new file mode 100644 index 0000000..9f3fef4 --- /dev/null +++ b/common/resh-common.go @@ -0,0 +1,37 @@ +package common + +import ( + "bytes" + "encoding/json" + "net/http" + "log" +) + +type Record struct { + CmdLine string `json: cmdLine` + Pwd string `json: pwd` + GitWorkTree string `json: gitWorkTree` + Shell string `json: shell` + ExitCode int `json: exitCode` + Logs []string `json: logs` +} + +func (r Record) Send() { + recJson, err := json.Marshal(r) + if err != nil { + log.Fatal("1", err) + } + + req, err := http.NewRequest("POST", "http://localhost:8888", + bytes.NewBuffer(recJson)) + if err != nil { + log.Fatal("2", err) + } + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + _, err = client.Do(req) + if err != nil { + log.Fatal("3", err) + } +} diff --git a/daemon/resh-daemon.go b/daemon/resh-daemon.go new file mode 100644 index 0000000..4782314 --- /dev/null +++ b/daemon/resh-daemon.go @@ -0,0 +1,41 @@ +package main + +import ( + "encoding/json" + "log" + "io/ioutil" + "net/http" + common "github.com/curusarn/resh/common" +) + +func main() { + server() +} + +func recordHandler(w http.ResponseWriter, r *http.Request) { + record := common.Record{} + + jsn, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Println("Error reading the body", err) + return + } + + err = json.Unmarshal(jsn, &record) + if err != nil { + log.Println("Decoding error: ", err) + return + } + + log.Printf("Received: %v\n", record) + // fmt.Println("cmd:", r.CmdLine) + // fmt.Println("pwd:", r.Pwd) + // fmt.Println("git:", r.GitWorkTree) + // fmt.Println("exit_code:", r.ExitCode) + w.Write([]byte("OK\n")) +} + +func server() { + http.HandleFunc("/", recordHandler) + http.ListenAndServe(":8888", nil) +}