From 2277916d3046f3192a4fe7e8d3ad8a6dd22a4afa Mon Sep 17 00:00:00 2001 From: Luca Date: Fri, 22 Jul 2022 19:52:41 +0200 Subject: [PATCH] Implement login prompt --- .gitignore | 1 + go.mod | 11 +++++++++ go.sum | 14 +++++++++++ internal/matrix/client.go | 9 +++++++ internal/matrix/login.go | 8 +++++++ internal/util/prompt.go | 49 +++++++++++++++++++++++++++++++++++++++ main.go | 47 +++++++++++++++++++++++++++++++++++++ 7 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100644 go.sum create mode 100644 internal/matrix/client.go create mode 100644 internal/matrix/login.go create mode 100644 internal/util/prompt.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47df990 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +matrix-pretix diff --git a/go.mod b/go.mod index a504bdb..aae5cec 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,14 @@ module git.luj0ga.de/franconian/matrix-pretix go 1.18 + +require ( + golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 + maunium.net/go/mautrix v0.11.0 +) + +require ( + golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 // indirect + golang.org/x/net v0.0.0-20220513224357-95641704303c // indirect + golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1ca47d9 --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 h1:NUzdAbFtCJSXU20AOXgeqaUwg8Ypg4MPYmL+d+rsB5c= +golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20220513224357-95641704303c h1:nF9mHSvoKBLkQNQhJZNsc66z2UzAMUbLGjC95CF3pU0= +golang.org/x/net v0.0.0-20220513224357-95641704303c/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +maunium.net/go/mautrix v0.11.0 h1:B1FBHcvE4Mud+AC+zgNQQOw0JxSVrt40watCejhVA7w= +maunium.net/go/mautrix v0.11.0/go.mod h1:K29EcHwsNg6r7fMfwvi0GHQ9o5wSjqB9+Q8RjCIQEjA= diff --git a/internal/matrix/client.go b/internal/matrix/client.go new file mode 100644 index 0000000..d91cd57 --- /dev/null +++ b/internal/matrix/client.go @@ -0,0 +1,9 @@ +package matrix + +import ( + "maunium.net/go/mautrix" +) + +type Client struct { + client *mautrix.Client +} diff --git a/internal/matrix/login.go b/internal/matrix/login.go new file mode 100644 index 0000000..ee268ba --- /dev/null +++ b/internal/matrix/login.go @@ -0,0 +1,8 @@ +package matrix + +import "fmt" + +func Login(identifier, password string) error { + fmt.Println(identifier, password) + return nil +} diff --git a/internal/util/prompt.go b/internal/util/prompt.go new file mode 100644 index 0000000..65f377c --- /dev/null +++ b/internal/util/prompt.go @@ -0,0 +1,49 @@ +package util + +import ( + "bufio" + "fmt" + "os" + "strings" + "syscall" + + "golang.org/x/term" +) + +type Prompter struct { + scanner *bufio.Scanner +} + +func NewPrompter(scanner *bufio.Scanner) *Prompter { + return &Prompter{scanner} +} + +func (p Prompter) Prompt(prompt string) (string, error) { + printPrompt(prompt) + + if p.scanner.Scan() { + return strings.TrimSpace(p.scanner.Text()), nil + } + + if err := p.scanner.Err(); err != nil { + return "", err + } + + return "", fmt.Errorf("%s must not be empty", prompt) +} + +func PromptForPassword(prompt string) (string, error) { + printPrompt(prompt) + + password, err := term.ReadPassword(int(syscall.Stdin)) + fmt.Fprintln(os.Stderr) + if err != nil { + return "", err + } + + return strings.TrimSpace(string(password)), nil +} + +func printPrompt(prompt string) { + fmt.Fprint(os.Stderr, prompt, ": ") +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..3a32b7b --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "bufio" + "fmt" + "os" + + "git.luj0ga.de/franconian/matrix-pretix/internal/matrix" + "git.luj0ga.de/franconian/matrix-pretix/internal/util" +) + +func login() { + prompter := util.NewPrompter(bufio.NewScanner(os.Stdin)) + + identifier, err := prompter.Prompt("user id") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + password, err := util.PromptForPassword("password") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + err = matrix.Login(identifier, password) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func run() { +} + +func main() { + switch { + case len(os.Args) == 2 && os.Args[1] == "login": + login() + case len(os.Args) == 1: + run() + default: + fmt.Fprintf(os.Stderr, "usage: %s [ \"\" | \"login\" ]\n", os.Args[0]) + os.Exit(1) + } +}