Compare commits

..

3 Commits

Author SHA1 Message Date
Wancat
1f82bd4bcb Display script list 2022-10-19 09:32:20 +08:00
Wancat
25503867f9 Run scripts 2022-10-19 09:12:33 +08:00
Wancat
268b488fea Add comfirm page, add account and name parameter 2022-10-19 08:52:30 +08:00
9 changed files with 144 additions and 37 deletions

3
go.mod Normal file
View File

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

95
main.go
View File

@@ -1,71 +1,116 @@
package main
import (
"fmt"
"bytes"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"text/template"
"time"
)
var tpl *template.Template
var ledgerTpl *template.Template
var htmlTpl *template.Template
const LEDGER_FILE = "test.txt"
type TxData struct {
Name string
Date string
Amount string
Account string
}
func init() {
var err error
tpl, err = template.ParseGlob("templates/*")
if err != nil {
panic(err)
}
log.Println(tpl.DefinedTemplates())
ledgerTpl = template.Must(template.ParseGlob("tx/*"))
htmlTpl = template.Must(template.ParseGlob("templates/*.html"))
}
func main() {
fmt.Println("Hi")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tpl.ExecuteTemplate(w, "index.html", tpl)
htmlTpl.ExecuteTemplate(w, "index.html", struct {
Templates []*template.Template
Scripts map[string][]string
}{
ledgerTpl.Templates(),
SCRIPTS,
})
})
http.HandleFunc("/action", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
panic(err)
http.Error(w, err.Error(), 400)
return
}
fmt.Println(r.Form)
err := renderTx(w, r.Form)
tx, err := newTx(r.Form)
if err != nil {
panic(err)
http.Error(w, err.Error(), 400)
log.Println(err, r.Form)
return
}
if err := htmlTpl.ExecuteTemplate(w, "new.html", struct {
Tx string
}{tx}); err != nil {
http.Error(w, err.Error(), 500)
}
})
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), 400)
return
}
tx := r.FormValue("tx")
if err := appendToFile(tx); err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := htmlTpl.ExecuteTemplate(w, "success.html", struct {
Tx string
}{tx}); err != nil {
http.Error(w, err.Error(), 500)
}
})
http.HandleFunc("/exec", func(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
if err := executeScript(w, name); err != nil {
http.Error(w, err.Error(), 500)
log.Println(err)
return
}
})
log.Println("Listen on http://localhost:8000")
log.Fatal(http.ListenAndServe(":8000", nil))
}
func renderTx(w io.Writer, params url.Values) (err error) {
name := params.Get("action")
func newTx(params url.Values) (result string, err error) {
action := params.Get("action")
data := TxData{
Date: time.Now().Format("2006/01/02"),
Amount: params.Get("amount"),
Account: params.Get("account"),
Name: params.Get("name"),
}
var buf bytes.Buffer
err = ledgerTpl.ExecuteTemplate(&buf, action, data)
return buf.String(), nil
}
func appendToFile(tx string) (err error) {
f, err := os.OpenFile(LEDGER_FILE, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
panic(err)
return err
}
defer f.Close()
if err = tpl.ExecuteTemplate(f, name, data); err != nil {
buf := strings.NewReader(strings.ReplaceAll(tx, "\r", "")) // Remove CR generated from browser
_, err = io.Copy(f, buf)
return err
}
if err = tpl.ExecuteTemplate(w, name, data); err != nil {
return err
}
return nil
}

24
scripts.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import (
"fmt"
"io"
"os/exec"
)
var SCRIPTS = map[string][]string{
"balance": {"b"},
"register": {"r"},
"expenses this month": {"b", "expenses", "-b", "this month"},
}
func executeScript(w io.Writer, name string) (err error) {
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()
}

View File

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

13
templates/new.html Normal file
View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Ledger Quick Note</title>
</head>
<body>
<h1>Comfirm new Tx</h1>
<form action="/submit" method="POST">
<textarea name="tx" rows="15" cols="40">{{ .Tx }}</textarea><br>
<input type="submit">
</form>
</body>
</html>

12
templates/success.html Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Ledger Quick Note</title>
</head>
<body>
<h1>Ledger Quick Note</h1>
<p><strong>Success</strong></p>
<pre><code>{{ .Tx }}</code></pre>
<p><a href="/">Back to home</a></p>
</body>
</html>

4
tx/expense.txt Normal file
View File

@@ -0,0 +1,4 @@
{{ .Date }} {{ with .Name }}{{ . }}{{ else }}{{ .Account }}{{ end }}
{{ .Account }} ${{ .Amount }}
cash