Compare commits
7 Commits
482c293dec
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
48051a5874 | ||
|
|
2449e9b96a | ||
|
|
6e69f90b4e | ||
|
|
b79b82798d | ||
|
|
1f82bd4bcb | ||
|
|
25503867f9 | ||
|
|
268b488fea |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
.env
|
.env
|
||||||
test.txt
|
test.txt
|
||||||
|
ledger-quicknote
|
||||||
|
|||||||
94
README.md
Normal file
94
README.md
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
# Ledger Quick Note
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
**Add ledger transactions on the fly!**
|
||||||
|
|
||||||
|
## Feature
|
||||||
|
|
||||||
|
### Create Transaction from Template
|
||||||
|
|
||||||
|
Add your transaction template in `tx/` (in Go's template syntax), and create transaction from them in the browser.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
Take some cash
|
||||||
|
```
|
||||||
|
{{ .Date }} * cash
|
||||||
|
expenses:cash ${{ .Amount }}
|
||||||
|
assets:cash
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Cash expenses
|
||||||
|
```
|
||||||
|
{{ .Date }} {{ with .Name }}{{ . }}{{ else }}{{ .Account }}{{ end }}
|
||||||
|
{{ .Account }} ${{ .Amount }}
|
||||||
|
expenses:cash
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Checkout `tx/` folder for more examples.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Adjust your transaction
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Result page
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
### Ledger Scripts
|
||||||
|
|
||||||
|
Run some commonly used ledger commands.
|
||||||
|
|
||||||
|
Define your commands in config.go
|
||||||
|
|
||||||
|
```go
|
||||||
|
var SCRIPTS = map[string][]string{
|
||||||
|
"balance assets": {"b", "assets", "-X", "$"},
|
||||||
|
"register": {"r", "--tail", "10"},
|
||||||
|
"balance this month": {"b", "-b", "this month"},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Rebuild binary everytime you make a change to `config.go`
|
||||||
|
|
||||||
|
Execute them and see the result in the browser.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
Requirements:
|
||||||
|
* go
|
||||||
|
* ledger (Only required when you use scripts)
|
||||||
|
|
||||||
|
Install requirements on Debian / Ubuntu based distro:
|
||||||
|
```
|
||||||
|
sudo apt install golang ledger
|
||||||
|
```
|
||||||
|
|
||||||
|
Install requirements on Arch based distro:
|
||||||
|
```
|
||||||
|
sudo pacman -S golang ledger
|
||||||
|
```
|
||||||
|
|
||||||
|
Clone the repo
|
||||||
|
|
||||||
|
```
|
||||||
|
git clone https://github.com/lancatlin/ledger-quicknote.git
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
go build
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
./ledger-quicknote
|
||||||
|
```
|
||||||
|
|
||||||
|
Checkout `deployment/` for Nginx & Systemd example configuration.
|
||||||
|
|
||||||
7
config.go
Normal file
7
config.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var SCRIPTS = map[string][]string{
|
||||||
|
"balance assets": {"b", "assets", "-X", "$"},
|
||||||
|
"register": {"r", "--tail", "10"},
|
||||||
|
"balance this month": {"b", "-b", "this month"},
|
||||||
|
}
|
||||||
12
deployment/nginx.conf
Normal file
12
deployment/nginx.conf
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
listen [::]:80;
|
||||||
|
server_name ledger.example.com;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
auth_basic "private zone";
|
||||||
|
auth_basic_user_file /etc/nginx/myusers;
|
||||||
|
|
||||||
|
proxy_pass http://127.0.0.1:8000;
|
||||||
|
}
|
||||||
|
}
|
||||||
12
deployment/systemd.service
Normal file
12
deployment/systemd.service
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Ledger Quick Note
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=www-data
|
||||||
|
Group=www-data
|
||||||
|
WorkingDirectory=/path/to/ledger-quicknote
|
||||||
|
ExecStart=/path/to/ledger-quicknote/ledger-quicknote -f /path/to/ledger/journal.txt -w /path/to/ledger
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
130
main.go
130
main.go
@@ -1,71 +1,139 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var tpl *template.Template
|
var ledgerTpl *template.Template
|
||||||
|
var htmlTpl *template.Template
|
||||||
|
|
||||||
const LEDGER_FILE = "test.txt"
|
var LEDGER_FILE string
|
||||||
|
var LEDGER_INIT string
|
||||||
|
var WORKING_DIR string
|
||||||
|
var HOST string
|
||||||
|
|
||||||
type TxData struct {
|
type TxData struct {
|
||||||
Date string
|
Name string
|
||||||
Amount string
|
Date string
|
||||||
|
Amount string
|
||||||
|
Account string
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
ledgerTpl = template.Must(template.ParseGlob("tx/*"))
|
||||||
tpl, err = template.ParseGlob("templates/*")
|
htmlTpl = template.Must(template.ParseGlob("templates/*.html"))
|
||||||
if err != nil {
|
flag.StringVar(&LEDGER_FILE, "f", "example.txt", "ledger journal file to write")
|
||||||
panic(err)
|
flag.StringVar(&LEDGER_INIT, "i", "", "ledger initiation file")
|
||||||
}
|
flag.StringVar(&WORKING_DIR, "w", "", "ledger working directory")
|
||||||
log.Println(tpl.DefinedTemplates())
|
flag.StringVar(&HOST, "b", "127.0.0.1:8000", "binding address")
|
||||||
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Hi")
|
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if err := r.ParseForm(); err != nil {
|
||||||
panic(err)
|
http.Error(w, err.Error(), 400)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(r.Form)
|
tx, err := newTx(r.Form)
|
||||||
err := renderTx(w, r.Form)
|
|
||||||
if err != nil {
|
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)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":8000", nil))
|
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.Printf("Listen on %s", HOST)
|
||||||
|
log.Fatal(http.ListenAndServe(HOST, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTx(w io.Writer, params url.Values) (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"),
|
||||||
|
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)
|
f, err := os.OpenFile(LEDGER_FILE, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
if err = tpl.ExecuteTemplate(f, name, data); err != nil {
|
|
||||||
return err
|
buf := strings.NewReader(strings.ReplaceAll(tx, "\r", "")) // Remove CR generated from browser
|
||||||
}
|
_, err = io.Copy(f, buf)
|
||||||
if err = tpl.ExecuteTemplate(w, name, data); err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
return nil
|
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{"--init-file", LEDGER_INIT, "--file", LEDGER_FILE}, script...)...)
|
||||||
|
cmd.Dir = WORKING_DIR
|
||||||
|
cmd.Stdout = w
|
||||||
|
cmd.Stderr = w
|
||||||
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
screenshots/action.png
Normal file
BIN
screenshots/action.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
BIN
screenshots/confirm.png
Normal file
BIN
screenshots/confirm.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
BIN
screenshots/exec.png
Normal file
BIN
screenshots/exec.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
BIN
screenshots/home.png
Normal file
BIN
screenshots/home.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
BIN
screenshots/success.png
Normal file
BIN
screenshots/success.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
@@ -2,19 +2,26 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Ledger Quick Note</title>
|
<title>Ledger Quick Note</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ledger Quick Note</h1>
|
<h1>Ledger Quick Note</h1>
|
||||||
<form action="/action" method="POST">
|
<form action="/new" method="POST">
|
||||||
<label>Action: <select name="action">
|
<label>Action:
|
||||||
{{ range .Templates }}
|
{{ range .Templates }}
|
||||||
{{ if ne .Name "index.html" }}
|
<input type="radio" name="action" value="{{ .Name }}">{{ .Name }}</option>
|
||||||
<option value="{{ .Name }}">{{ .Name }}</option>
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
</label><br>
|
||||||
</select></label>
|
<label>Amount: <input name="amount" type="number"></label><br>
|
||||||
<label>Amount: <input name="amount" type="number"></label>
|
<label>Account: <input name="account" 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>
|
||||||
|
|||||||
14
templates/new.html
Normal file
14
templates/new.html
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Ledger Quick Note</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Confirm new Tx</h1>
|
||||||
|
<form action="/submit" method="POST">
|
||||||
|
<textarea name="tx" rows="15" cols="40">{{ .Tx }}</textarea><br>
|
||||||
|
<input type="submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
13
templates/success.html
Normal file
13
templates/success.html
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Ledger Quick Note</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
</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
4
tx/expense.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{{ .Date }} {{ with .Name }}{{ . }}{{ else }}{{ .Account }}{{ end }}
|
||||||
|
{{ .Account }} ${{ .Amount }}
|
||||||
|
cash
|
||||||
|
|
||||||
Reference in New Issue
Block a user