Run scripts

This commit is contained in:
Wancat
2022-10-19 09:12:33 +08:00
parent 268b488fea
commit 25503867f9
3 changed files with 29 additions and 0 deletions

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/lancatlin/ledger-quicknote
go 1.19

View File

@@ -74,6 +74,14 @@ func main() {
} }
}) })
http.HandleFunc("/exec", func(w http.ResponseWriter, r *http.Request) {
if err := executeScript(w, "register"); err != nil {
http.Error(w, err.Error(), 500)
log.Println(err)
return
}
})
log.Println("Listen on http://localhost:8000") log.Println("Listen on http://localhost:8000")
log.Fatal(http.ListenAndServe(":8000", nil)) log.Fatal(http.ListenAndServe(":8000", nil))
} }

18
scripts.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"io"
"os/exec"
)
var SCRIPTS = map[string][]string{
"balance": {"b"},
"register": {"r"},
}
func executeScript(w io.Writer, name string) (err error) {
cmd := exec.Command("ledger", append([]string{"-f", LEDGER_FILE}, SCRIPTS[name]...)...)
cmd.Stdout = w
cmd.Stderr = w
return cmd.Run()
}