From 3496fe5f8651b0565f5adb11c3f5b9e51754b613 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 28 Oct 2023 08:57:15 +0200 Subject: [PATCH] =?UTF-8?q?package=20data=20weiter=20aufger=C3=A4umt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/data/dataStructs.go | 6 ++++ packages/data/{db.go => dbFuncs.go} | 51 ---------------------------- packages/data/helperFuncs.go | 52 +++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 51 deletions(-) rename packages/data/{db.go => dbFuncs.go} (86%) create mode 100644 packages/data/helperFuncs.go diff --git a/packages/data/dataStructs.go b/packages/data/dataStructs.go index 1a00dd7..6c56059 100644 --- a/packages/data/dataStructs.go +++ b/packages/data/dataStructs.go @@ -1,5 +1,11 @@ package data +import "database/sql" + +type DB struct { + *sql.DB +} + type Person struct { ID int64 FirstName string diff --git a/packages/data/db.go b/packages/data/dbFuncs.go similarity index 86% rename from packages/data/db.go rename to packages/data/dbFuncs.go index 38afb6f..16dbbf6 100644 --- a/packages/data/db.go +++ b/packages/data/dbFuncs.go @@ -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) diff --git a/packages/data/helperFuncs.go b/packages/data/helperFuncs.go new file mode 100644 index 0000000..88f7655 --- /dev/null +++ b/packages/data/helperFuncs.go @@ -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 +}