save history as json, retab

pull/3/head
Simon Let 7 years ago
parent 2f6f5d32a3
commit 6516e34337
  1. 12
      common/resh-common.go
  2. 29
      daemon/resh-daemon.go

@ -1,12 +1,12 @@
package common
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`
CmdLine string `json:"cmdLine"`
Pwd string `json:"pwd"`
GitWorkTree string `json:"gitWorkTree"`
Shell string `json:"shell"`
ExitCode int `json:"exitCode"`
//Logs []string `json: "logs"`
}
type Config struct {

@ -23,7 +23,8 @@ func main() {
usr, _ := user.Current()
dir := usr.HomeDir
pidfilePath := filepath.Join(dir, ".resh/resh.pid")
configPath := filepath.Join(dir, "/.config/resh.toml")
configPath := filepath.Join(dir, ".config/resh.toml")
outputPath := filepath.Join(dir, ".resh/history.json")
var config common.Config
if _, err := toml.DecodeFile(configPath, &config); err != nil {
@ -51,7 +52,7 @@ func main() {
if err != nil {
log.Fatal("Could not create pidfile", err)
}
server(config.Port)
runServer(config.Port, outputPath)
err = os.Remove(pidfilePath)
if err != nil {
log.Println("Could not delete pidfile", err)
@ -63,7 +64,11 @@ func statusHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Status OK\n")
}
func recordHandler(w http.ResponseWriter, r *http.Request) {
type recordHandler struct {
OutputPath string
}
func (h *recordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK\n"))
record := common.Record{}
@ -79,17 +84,29 @@ func recordHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Payload: ", jsn)
return
}
f, err := os.OpenFile(h.OutputPath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println("Could not open file", err)
return
}
defer f.Close()
_, err = f.Write(append(jsn, []byte("\n")...))
if err != nil {
log.Printf("Error while writing: %v, %s\n", record, 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)
}
func server(port int) {
func runServer(port int, outputPath string) {
http.HandleFunc("/status", statusHandler)
http.HandleFunc("/record", recordHandler)
http.Handle("/record", &recordHandler{OutputPath: outputPath})
http.ListenAndServe(":" + strconv.Itoa(port) , nil)
}

Loading…
Cancel
Save