diff --git a/main.go b/main.go index efe47d3..213e5e6 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "fmt" "io" "log" "net/http" @@ -26,14 +25,19 @@ type TxData struct { } func init() { - ledgerTpl = template.Must(template.ParseGlob("tx/*.txt")) - log.Println(ledgerTpl.DefinedTemplates()) + ledgerTpl = template.Must(template.ParseGlob("tx/*")) htmlTpl = template.Must(template.ParseGlob("templates/*.html")) } func main() { 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) { @@ -41,7 +45,6 @@ func main() { http.Error(w, err.Error(), 400) return } - fmt.Println(r.Form) tx, err := newTx(r.Form) if err != nil { http.Error(w, err.Error(), 400) @@ -75,7 +78,8 @@ func main() { }) 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) log.Println(err) return @@ -87,7 +91,7 @@ func main() { } func newTx(params url.Values) (result string, err error) { - name := params.Get("action") + action := params.Get("action") data := TxData{ Date: time.Now().Format("2006/01/02"), Amount: params.Get("amount"), @@ -95,7 +99,7 @@ func newTx(params url.Values) (result string, err error) { Name: params.Get("name"), } var buf bytes.Buffer - err = ledgerTpl.ExecuteTemplate(&buf, name, data) + err = ledgerTpl.ExecuteTemplate(&buf, action, data) return buf.String(), nil } diff --git a/scripts.go b/scripts.go index 16b3d2a..d775026 100644 --- a/scripts.go +++ b/scripts.go @@ -1,17 +1,23 @@ package main import ( + "fmt" "io" "os/exec" ) var SCRIPTS = map[string][]string{ - "balance": {"b"}, - "register": {"r"}, + "balance": {"b"}, + "register": {"r"}, + "expenses this month": {"b", "expenses", "-b", "this month"}, } 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.Stderr = w return cmd.Run() diff --git a/templates/index.html b/templates/index.html index 74b810f..f619a54 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,15 +6,21 @@

Ledger Quick Note

-
+



+

Scripts

+