Implement login prompt

This commit is contained in:
Luca 2022-07-22 19:52:41 +02:00
parent 6d22aa6ef5
commit 2277916d30
7 changed files with 139 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
matrix-pretix

11
go.mod
View File

@ -1,3 +1,14 @@
module git.luj0ga.de/franconian/matrix-pretix module git.luj0ga.de/franconian/matrix-pretix
go 1.18 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
)

14
go.sum Normal file
View File

@ -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=

View File

@ -0,0 +1,9 @@
package matrix
import (
"maunium.net/go/mautrix"
)
type Client struct {
client *mautrix.Client
}

8
internal/matrix/login.go Normal file
View File

@ -0,0 +1,8 @@
package matrix
import "fmt"
func Login(identifier, password string) error {
fmt.Println(identifier, password)
return nil
}

49
internal/util/prompt.go Normal file
View File

@ -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, ": ")
}

47
main.go Normal file
View File

@ -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)
}
}