From d8984df882e1d84449bf30c6a10532c79d02fbc5 Mon Sep 17 00:00:00 2001 From: Lennart Jablonka Date: Tue, 6 Sep 2022 21:25:11 +0200 Subject: [PATCH] initial commit --- go.mod | 5 ++++ go.sum | 6 +++++ main.go | 24 ++++++++++++++++++ pay/pay.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 pay/pay.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c423022 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module dev.ccchb.de/humm/z1 + +go 1.19 + +require git.sr.ht/~sircmpwn/getopt v1.0.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8e2792a --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..fd82d14 --- /dev/null +++ b/main.go @@ -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:]) +} diff --git a/pay/pay.go b/pay/pay.go new file mode 100644 index 0000000..e3ac685 --- /dev/null +++ b/pay/pay.go @@ -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 can’t 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) + } +}