2023-10-28 08:57:15 +02:00
|
|
|
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 {
|
2023-10-30 11:38:56 +01:00
|
|
|
return "", fmt.Errorf("error: getUsername: bufio.NewReader().ReadString(): %v", err)
|
2023-10-28 08:57:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2023-10-30 11:38:56 +01:00
|
|
|
return "", fmt.Errorf("error: getPassword: term.ReadPassword(): %v", err)
|
2023-10-28 08:57:15 +02:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
pass = strings.TrimSpace(string(bytePass))
|
|
|
|
}
|
|
|
|
return pass, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCredentials() (string, string, error) {
|
|
|
|
user, err := getUsername()
|
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return "", "", fmt.Errorf("error: getCredentials: getUsername(): %v", err)
|
2023-10-28 08:57:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pass, err := getPassword()
|
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return "", "", fmt.Errorf("error: getCredentials: getPassword(): %v", err)
|
2023-10-28 08:57:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return user, pass, nil
|
|
|
|
}
|