db options + refactoring
db config should be taken from environment, but using `flag` was easier
This commit is contained in:
parent
474103612d
commit
c92074003c
2 changed files with 30 additions and 17 deletions
|
@ -11,18 +11,28 @@ import (
|
||||||
"github.com/howeyc/gopass"
|
"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 {
|
func getDB() paneldb.DB {
|
||||||
pgdb := pg.Connect(&pg.Options{
|
pgdb := pg.Connect(&globalOptions.pgOptions)
|
||||||
User: "panel",
|
|
||||||
Password: "panelpass",
|
|
||||||
Database: "feeds",
|
|
||||||
})
|
|
||||||
db := paneldb.DB{PgDB: pgdb}
|
db := paneldb.DB{PgDB: pgdb}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
type commandFunc func([]string) error
|
|
||||||
|
|
||||||
func cmdSetup(_args []string) error {
|
func cmdSetup(_args []string) error {
|
||||||
db := getDB()
|
db := getDB()
|
||||||
err := db.Setup()
|
err := db.Setup()
|
||||||
|
@ -30,7 +40,9 @@ func cmdSetup(_args []string) error {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Println("All done")
|
if globalOptions.verbose {
|
||||||
|
fmt.Println("Tables created")
|
||||||
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -77,16 +89,13 @@ func cmdAddUser(args []string) error {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
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)
|
os.Exit(0)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdMap = map[string]commandFunc{
|
|
||||||
"setup": cmdSetup,
|
|
||||||
"adduser": cmdAddUser,
|
|
||||||
}
|
|
||||||
|
|
||||||
func usage(w io.Writer) {
|
func usage(w io.Writer) {
|
||||||
fmt.Fprintf(w, "Usage: %s SUBCOMMAND [command args...]\n", os.Args[0])
|
fmt.Fprintf(w, "Usage: %s SUBCOMMAND [command args...]\n", os.Args[0])
|
||||||
fmt.Fprintln(w, "\nSubcommands:")
|
fmt.Fprintln(w, "\nSubcommands:")
|
||||||
|
@ -96,8 +105,12 @@ func usage(w io.Writer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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()
|
flag.Parse()
|
||||||
fmt.Println(flag.Args())
|
|
||||||
if len(flag.Args()) < 1 {
|
if len(flag.Args()) < 1 {
|
||||||
fmt.Fprintln(os.Stderr, "Not enough arguments!")
|
fmt.Fprintln(os.Stderr, "Not enough arguments!")
|
||||||
usage(os.Stderr)
|
usage(os.Stderr)
|
||||||
|
|
4
db/db.go
4
db/db.go
|
@ -36,7 +36,7 @@ func (db *DB) Setup() error {
|
||||||
func (db *DB) UserAdd(username, email, pwd string) 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)")
|
stmt, err := db.PgDB.Prepare("INSERT INTO users.users (username, email, password) VALUES ($1, $2, $3)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Bad statement in UserAdd")
|
return errors.Wrap(err, "Error preparing insert statement")
|
||||||
}
|
}
|
||||||
pwhash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
|
pwhash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -44,7 +44,7 @@ func (db *DB) UserAdd(username, email, pwd string) error {
|
||||||
}
|
}
|
||||||
_, err = stmt.Exec(username, email, string(pwhash))
|
_, err = stmt.Exec(username, email, string(pwhash))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "Error adding user `%s`", email)
|
return errors.Wrapf(err, "Error adding user `%s`", username)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue