package data weiter aufgeräumt
This commit is contained in:
parent
10247722c8
commit
3496fe5f86
@ -1,5 +1,11 @@
|
||||
package data
|
||||
|
||||
import "database/sql"
|
||||
|
||||
type DB struct {
|
||||
*sql.DB
|
||||
}
|
||||
|
||||
type Person struct {
|
||||
ID int64
|
||||
FirstName string
|
||||
|
@ -1,63 +1,13 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
*sql.DB
|
||||
Name string
|
||||
}
|
||||
|
||||
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("getUsername: bufio.NewReader(os.Stdin).ReadString('\n'): %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("getCredentials: term.ReadPassword(int(syscall.Stdin)): %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("getCredentials: getUsername(): %v", err)
|
||||
}
|
||||
|
||||
pass, err := getPassword()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("getCredentials: getPassword(): %v", err)
|
||||
}
|
||||
|
||||
return user, pass, nil
|
||||
}
|
||||
|
||||
func OpenDB(dbName string) (*DB, error) {
|
||||
var err error
|
||||
db := new(DB)
|
||||
@ -69,7 +19,6 @@ func OpenDB(dbName string) (*DB, error) {
|
||||
return nil, fmt.Errorf("Open: getCredentials(): %v\n", err)
|
||||
}
|
||||
|
||||
db.Name = dbName
|
||||
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Open: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
|
52
packages/data/helperFuncs.go
Normal file
52
packages/data/helperFuncs.go
Normal file
@ -0,0 +1,52 @@
|
||||
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("getUsername: bufio.NewReader(os.Stdin).ReadString('\n'): %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("getCredentials: term.ReadPassword(int(syscall.Stdin)): %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("getCredentials: getUsername(): %v", err)
|
||||
}
|
||||
|
||||
pass, err := getPassword()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("getCredentials: getPassword(): %v", err)
|
||||
}
|
||||
|
||||
return user, pass, nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user