74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
package data
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"golang.org/x/term"
|
|
)
|
|
|
|
func getUsername() (string, error) {
|
|
user := os.Getenv("DB_USER")
|
|
if user == "" {
|
|
var err error
|
|
fmt.Printf("DB Benutzer: ")
|
|
user, err = bufio.NewReader(os.Stdin).ReadString('\n')
|
|
if err != nil {
|
|
return "", fmt.Errorf("error reading username: %v", err)
|
|
}
|
|
}
|
|
return strings.TrimSpace(user), nil
|
|
}
|
|
|
|
func getPassword() (string, error) {
|
|
pass := os.Getenv("DB_PASS")
|
|
if pass == "" {
|
|
fmt.Printf("DB Passwort: ")
|
|
bytePass, err := term.ReadPassword(int(syscall.Stdin))
|
|
if err != nil {
|
|
return "", fmt.Errorf("error reading password: %v", err)
|
|
}
|
|
fmt.Println()
|
|
pass = strings.TrimSpace(string(bytePass))
|
|
}
|
|
return pass, nil
|
|
}
|
|
|
|
func getCredentials() (string, string, error) {
|
|
user, err := getUsername()
|
|
if err != nil {
|
|
return "", "", fmt.Errorf("error getting username: %v", err)
|
|
}
|
|
|
|
pass, err := getPassword()
|
|
if err != nil {
|
|
return "", "", fmt.Errorf("error getting password: %v", err)
|
|
}
|
|
|
|
return user, pass, nil
|
|
}
|
|
|
|
func checkUserStrings(user, first, last string) (string, int, bool) {
|
|
userLen := 15
|
|
nameLen := 50
|
|
|
|
if len(user) > userLen {
|
|
return user, userLen, false
|
|
} else if len(first) > nameLen {
|
|
return first, nameLen, false
|
|
} else if len(last) > nameLen {
|
|
return last, nameLen, false
|
|
} else {
|
|
return "", 0, true
|
|
}
|
|
}
|
|
|
|
func permissionsOK(writer, editor, admin bool) bool {
|
|
return writer && !editor && !admin ||
|
|
!writer && editor && !admin ||
|
|
!writer && !editor && admin
|
|
}
|