move stuff around, draft of daemon

pull/1/head
Simon Let 7 years ago
parent f1cb7bfc75
commit d77b99ba42
  1. 19
      Makefile
  2. 0
      bashrc.sh
  3. 21
      collect/resh-collect.go
  4. 37
      common/resh-common.go
  5. 41
      daemon/resh-daemon.go

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

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

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

@ -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)
}
Loading…
Cancel
Save