From c92074003cfd873d678e24ade4062b5af58df4b1 Mon Sep 17 00:00:00 2001 From: boyska Date: Fri, 28 Sep 2018 00:55:12 +0200 Subject: [PATCH] db options + refactoring db config should be taken from environment, but using `flag` was easier --- cmd/panelcli/cli.go | 43 ++++++++++++++++++++++++++++--------------- db/db.go | 4 ++-- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/cmd/panelcli/cli.go b/cmd/panelcli/cli.go index 55b23e6..f3ff280 100644 --- a/cmd/panelcli/cli.go +++ b/cmd/panelcli/cli.go @@ -11,18 +11,28 @@ import ( "github.com/howeyc/gopass" ) +type commandFunc func([]string) error + +var globalOptions struct { + verbose bool + pgOptions pg.Options +} + +var cmdMap map[string]commandFunc + +func init() { + cmdMap = map[string]commandFunc{ + "setup": cmdSetup, + "adduser": cmdAddUser, + } +} + func getDB() paneldb.DB { - pgdb := pg.Connect(&pg.Options{ - User: "panel", - Password: "panelpass", - Database: "feeds", - }) + pgdb := pg.Connect(&globalOptions.pgOptions) db := paneldb.DB{PgDB: pgdb} return db } -type commandFunc func([]string) error - func cmdSetup(_args []string) error { db := getDB() err := db.Setup() @@ -30,7 +40,9 @@ func cmdSetup(_args []string) error { fmt.Fprintln(os.Stderr, err) os.Exit(1) } - fmt.Println("All done") + if globalOptions.verbose { + fmt.Println("Tables created") + } os.Exit(0) return nil } @@ -77,16 +89,13 @@ func cmdAddUser(args []string) error { fmt.Fprintln(os.Stderr, err) os.Exit(1) } - fmt.Printf("Success: user %s added\n", user) + if globalOptions.verbose { + fmt.Printf("Success: user %s added\n", user) + } os.Exit(0) return nil } -var cmdMap = map[string]commandFunc{ - "setup": cmdSetup, - "adduser": cmdAddUser, -} - func usage(w io.Writer) { fmt.Fprintf(w, "Usage: %s SUBCOMMAND [command args...]\n", os.Args[0]) fmt.Fprintln(w, "\nSubcommands:") @@ -96,8 +105,12 @@ func usage(w io.Writer) { } func main() { + flag.StringVar(&globalOptions.pgOptions.User, "db-user", "panel", "The user to connect to database to") + flag.StringVar(&globalOptions.pgOptions.Password, "db-pass", "", "database password") + flag.StringVar(&globalOptions.pgOptions.Database, "db-name", "feeds", "database name") + flag.StringVar(&globalOptions.pgOptions.Addr, "db-addr", "127.0.0.1:5432", "database network address") + flag.BoolVar(&globalOptions.verbose, "verbose", false, "verbose output") flag.Parse() - fmt.Println(flag.Args()) if len(flag.Args()) < 1 { fmt.Fprintln(os.Stderr, "Not enough arguments!") usage(os.Stderr) diff --git a/db/db.go b/db/db.go index 2d00a8e..5e0d57b 100644 --- a/db/db.go +++ b/db/db.go @@ -36,7 +36,7 @@ func (db *DB) Setup() error { func (db *DB) UserAdd(username, email, pwd string) error { stmt, err := db.PgDB.Prepare("INSERT INTO users.users (username, email, password) VALUES ($1, $2, $3)") if err != nil { - panic("Bad statement in UserAdd") + return errors.Wrap(err, "Error preparing insert statement") } pwhash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost) if err != nil { @@ -44,7 +44,7 @@ func (db *DB) UserAdd(username, email, pwd string) error { } _, err = stmt.Exec(username, email, string(pwhash)) if err != nil { - return errors.Wrapf(err, "Error adding user `%s`", email) + return errors.Wrapf(err, "Error adding user `%s`", username) } return nil }