Run scripts
This commit is contained in:
8
main.go
8
main.go
@@ -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
18
scripts.go
Normal 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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user