Compare commits

..

4 Commits

Author SHA1 Message Date
Wancat
48051a5874 Fix typo, add screenshots 2022-10-19 11:22:42 +08:00
Wancat
2449e9b96a Add README & deployment examples 2022-10-19 11:09:35 +08:00
Wancat
6e69f90b4e Add working directory & binding address options 2022-10-19 10:33:37 +08:00
Wancat
b79b82798d Add ledger initial file option, add meta viewport 2022-10-19 10:15:59 +08:00
15 changed files with 156 additions and 28 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.env
test.txt
ledger-quicknote

94
README.md Normal file
View File

@@ -0,0 +1,94 @@
# Ledger Quick Note
![screenshot](screenshots/home.png)
**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.
![new action](screenshots/action.png)
Adjust your transaction
![confirm](screenshots/confirm.png)
Result page
![result](screenshots/success.png)
### 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.
![execute result](screenshots/exec.png)
## 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
View 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
View 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;
}
}

View 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

29
main.go
View File

@@ -2,11 +2,14 @@ package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"text/template"
"time"
@@ -15,7 +18,10 @@ import (
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 {
Name string
@@ -27,6 +33,11 @@ type TxData struct {
func init() {
ledgerTpl = template.Must(template.ParseGlob("tx/*"))
htmlTpl = template.Must(template.ParseGlob("templates/*.html"))
flag.StringVar(&LEDGER_FILE, "f", "example.txt", "ledger journal file to write")
flag.StringVar(&LEDGER_INIT, "i", "", "ledger initiation file")
flag.StringVar(&WORKING_DIR, "w", "", "ledger working directory")
flag.StringVar(&HOST, "b", "127.0.0.1:8000", "binding address")
flag.Parse()
}
func main() {
@@ -86,8 +97,8 @@ func main() {
}
})
log.Println("Listen on http://localhost:8000")
log.Fatal(http.ListenAndServe(":8000", nil))
log.Printf("Listen on %s", HOST)
log.Fatal(http.ListenAndServe(HOST, nil))
}
func newTx(params url.Values) (result string, err error) {
@@ -114,3 +125,15 @@ func appendToFile(tx string) (err error) {
_, err = io.Copy(f, buf)
return err
}
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

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
screenshots/confirm.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
screenshots/exec.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
screenshots/home.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
screenshots/success.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@@ -1,24 +0,0 @@
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

@@ -2,6 +2,7 @@
<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>

View File

@@ -2,9 +2,10 @@
<html>
<head>
<title>Ledger Quick Note</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Comfirm new Tx</h1>
<h1>Confirm new Tx</h1>
<form action="/submit" method="POST">
<textarea name="tx" rows="15" cols="40">{{ .Tx }}</textarea><br>
<input type="submit">

View File

@@ -2,6 +2,7 @@
<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>