initial commit

This commit is contained in:
Lennart Jablonka 2022-09-06 21:25:11 +02:00
commit d8984df882
4 changed files with 106 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module dev.ccchb.de/humm/z1
go 1.19
require git.sr.ht/~sircmpwn/getopt v1.0.0

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
git.sr.ht/~sircmpwn/getopt v1.0.0 h1:/pRHjO6/OCbBF4puqD98n6xtPEgE//oq5U8NXjP7ROc=
git.sr.ht/~sircmpwn/getopt v1.0.0/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

24
main.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"fmt"
"os"
"dev.ccchb.de/humm/z1/pay"
)
var commands = map[string]func([]string){
"pay": pay.Main,
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s pay [args ...]\n", os.Args[0])
os.Exit(1)
}
func main() {
if len(os.Args) < 2 {
usage()
}
commands[os.Args[1]](os.Args[1:])
}

71
pay/pay.go Normal file
View File

@ -0,0 +1,71 @@
package pay
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"git.sr.ht/~sircmpwn/getopt"
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s pay [-h host] [-a user:password] user amount\n", os.Args[0])
os.Exit(1)
}
func Main(args []string) {
host := "https://usualsuspect:freundschaft@kasse.z1.ccchb.de"
// auth := "usualsuspect:freundschaft"
getopt.Usage = usage
opts, optind, err := getopt.Getopts(args, "h:a:")
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
for _, opt := range opts {
switch opt.Option {
case 'h':
host = opt.Value
case 'a':
// auth = opt.Value
}
}
if len(args) != optind+2 {
usage()
}
args = args[optind:]
user := args[0]
amount, err := strconv.Atoi(args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "%s: amount: %v\n", os.Args[0], err)
os.Exit(1)
}
req := struct {
Amount int `json:"amount"`
}{amount}
json, err := json.Marshal(req)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: generating JSON: %v\n", os.Args[0], err)
os.Exit(1)
}
// BUG: user is not URI encoded
// BUG: user cant be the name
resp, err := http.Post(host+"/api/user/"+user+"/transaction", "application/json", strings.NewReader(string(json)))
if err != nil {
fmt.Fprintf(os.Stderr, "%s: HTTP: %v\n", os.Args[0], err)
os.Exit(1)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Fprintf(os.Stderr, "%s: unexpected status code: %d\n", os.Args[0], resp.StatusCode)
io.Copy(os.Stderr, resp.Body)
}
}