diff --git a/app.go b/app.go index 3cd416a..fbdd63b 100644 --- a/app.go +++ b/app.go @@ -516,18 +516,9 @@ func shutdown(app *App) { app.db.Close() } -// CreateUser creates a new admin or normal user from the given username:password string. -func CreateUser(app *App, credStr string, isAdmin bool) error { +// CreateUser creates a new admin or normal user from the given credentials. +func CreateUser(app *App, username, password string, isAdmin bool) error { // Create an admin user with --create-admin - creds := strings.Split(credStr, ":") - if len(creds) != 2 { - c := "user" - if isAdmin { - c = "admin" - } - return fmt.Errorf("usage: writefreely --create-%s username:password", c) - } - loadConfig(app) connectToDatabase(app) defer shutdown(app) @@ -547,9 +538,6 @@ func CreateUser(app *App, credStr string, isAdmin bool) error { } // Create the user - username := creds[0] - password := creds[1] - // Normalize and validate username desiredUsername := username username = getSlug(username, "") diff --git a/cmd/writefreely/main.go b/cmd/writefreely/main.go index 73866cb..6a32ad9 100644 --- a/cmd/writefreely/main.go +++ b/cmd/writefreely/main.go @@ -12,9 +12,11 @@ package main import ( "flag" + "fmt" "github.com/writeas/web-core/log" "github.com/writeas/writefreely" "os" + "strings" ) func main() { @@ -66,14 +68,24 @@ func main() { } os.Exit(0) } else if *createAdmin != "" { - err := writefreely.CreateUser(app, *createAdmin, true) + username, password, err := userPass(*createAdmin, true) + if err != nil { + log.Error(err.Error()) + os.Exit(1) + } + err = writefreely.CreateUser(app, username, password, true) if err != nil { log.Error(err.Error()) os.Exit(1) } os.Exit(0) } else if *createUser != "" { - err := writefreely.CreateUser(app, *createUser, false) + username, password, err := userPass(*createUser, false) + if err != nil { + log.Error(err.Error()) + os.Exit(1) + } + err = writefreely.CreateUser(app, username, password, false) if err != nil { log.Error(err.Error()) os.Exit(1) @@ -97,3 +109,19 @@ func main() { writefreely.Serve(app, *debugPtr) } + +func userPass(credStr string, isAdmin bool) (user string, pass string, err error) { + creds := strings.Split(credStr, ":") + if len(creds) != 2 { + c := "user" + if isAdmin { + c = "admin" + } + err = fmt.Errorf("usage: writefreely --create-%s username:password", c) + return + } + + user = creds[0] + pass = creds[1] + return +}