Display script list

This commit is contained in:
Wancat
2022-10-19 09:32:20 +08:00
parent 25503867f9
commit 1f82bd4bcb
3 changed files with 30 additions and 14 deletions

20
main.go
View File

@@ -2,7 +2,6 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
@@ -26,14 +25,19 @@ type TxData struct {
} }
func init() { func init() {
ledgerTpl = template.Must(template.ParseGlob("tx/*.txt")) ledgerTpl = template.Must(template.ParseGlob("tx/*"))
log.Println(ledgerTpl.DefinedTemplates())
htmlTpl = template.Must(template.ParseGlob("templates/*.html")) htmlTpl = template.Must(template.ParseGlob("templates/*.html"))
} }
func main() { func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
htmlTpl.ExecuteTemplate(w, "index.html", ledgerTpl) htmlTpl.ExecuteTemplate(w, "index.html", struct {
Templates []*template.Template
Scripts map[string][]string
}{
ledgerTpl.Templates(),
SCRIPTS,
})
}) })
http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) {
@@ -41,7 +45,6 @@ func main() {
http.Error(w, err.Error(), 400) http.Error(w, err.Error(), 400)
return return
} }
fmt.Println(r.Form)
tx, err := newTx(r.Form) tx, err := newTx(r.Form)
if err != nil { if err != nil {
http.Error(w, err.Error(), 400) http.Error(w, err.Error(), 400)
@@ -75,7 +78,8 @@ func main() {
}) })
http.HandleFunc("/exec", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/exec", func(w http.ResponseWriter, r *http.Request) {
if err := executeScript(w, "register"); err != nil { name := r.FormValue("name")
if err := executeScript(w, name); err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
log.Println(err) log.Println(err)
return return
@@ -87,7 +91,7 @@ func main() {
} }
func newTx(params url.Values) (result string, err error) { func newTx(params url.Values) (result string, err error) {
name := params.Get("action") action := params.Get("action")
data := TxData{ data := TxData{
Date: time.Now().Format("2006/01/02"), Date: time.Now().Format("2006/01/02"),
Amount: params.Get("amount"), Amount: params.Get("amount"),
@@ -95,7 +99,7 @@ func newTx(params url.Values) (result string, err error) {
Name: params.Get("name"), Name: params.Get("name"),
} }
var buf bytes.Buffer var buf bytes.Buffer
err = ledgerTpl.ExecuteTemplate(&buf, name, data) err = ledgerTpl.ExecuteTemplate(&buf, action, data)
return buf.String(), nil return buf.String(), nil
} }

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"io" "io"
"os/exec" "os/exec"
) )
@@ -8,10 +9,15 @@ import (
var SCRIPTS = map[string][]string{ var SCRIPTS = map[string][]string{
"balance": {"b"}, "balance": {"b"},
"register": {"r"}, "register": {"r"},
"expenses this month": {"b", "expenses", "-b", "this month"},
} }
func executeScript(w io.Writer, name string) (err error) { func executeScript(w io.Writer, name string) (err error) {
cmd := exec.Command("ledger", append([]string{"-f", LEDGER_FILE}, SCRIPTS[name]...)...) script, ok := SCRIPTS[name]
if !ok {
return fmt.Errorf("%s script not found", name)
}
cmd := exec.Command("ledger", append([]string{"-f", LEDGER_FILE}, script...)...)
cmd.Stdout = w cmd.Stdout = w
cmd.Stderr = w cmd.Stderr = w
return cmd.Run() return cmd.Run()

View File

@@ -6,15 +6,21 @@
<body> <body>
<h1>Ledger Quick Note</h1> <h1>Ledger Quick Note</h1>
<form action="/new" method="POST"> <form action="/new" method="POST">
<label>Action: <select name="action"> <label>Action:
{{ range .Templates }} {{ range .Templates }}
<option value="{{ .Name }}">{{ .Name }}</option> <input type="radio" name="action" value="{{ .Name }}">{{ .Name }}</option>
{{ end }} {{ end }}
</select></label><br> </label><br>
<label>Amount: <input name="amount" type="number"></label><br> <label>Amount: <input name="amount" type="number"></label><br>
<label>Account: <input name="account" type="text"></label></br> <label>Account: <input name="account" type="text"></label></br>
<label>Tx Name: <input name="name" type="text"></label></br> <label>Tx Name: <input name="name" type="text"></label></br>
<input type="submit"> <input type="submit">
</form> </form>
<h2>Scripts</h2>
<ul>
{{ range $k, $v := .Scripts }}
<li><a href="/exec?name={{ $k }}">{{ $k }}</a></li>
{{ end }}
</ul>
</body> </body>
</html> </html>