Return error from adminCreateUser()

...instead of using os.Exit(). This makes it more apparent what's going
on, less error-prone, and more consistent with other command logic paths.
This commit is contained in:
Matt Baer 2019-01-26 11:06:58 -05:00
parent 08667d8978
commit c11ede53d9

36
app.go
View file

@ -300,9 +300,19 @@ func Serve() {
} }
os.Exit(0) os.Exit(0)
} else if *createAdmin != "" { } else if *createAdmin != "" {
adminCreateUser(app, *createAdmin, true) err := adminCreateUser(app, *createAdmin, true)
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
os.Exit(0)
} else if *createUser != "" { } else if *createUser != "" {
adminCreateUser(app, *createUser, false) err := adminCreateUser(app, *createUser, false)
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
os.Exit(0)
} else if *resetPassUser != "" { } else if *resetPassUser != "" {
// Connect to the database // Connect to the database
loadConfig(app) loadConfig(app)
@ -517,12 +527,11 @@ func shutdown(app *app) {
app.db.Close() app.db.Close()
} }
func adminCreateUser(app *app, credStr string, isAdmin bool) { func adminCreateUser(app *app, credStr string, isAdmin bool) error {
// Create an admin user with --create-admin // Create an admin user with --create-admin
creds := strings.Split(credStr, ":") creds := strings.Split(credStr, ":")
if len(creds) != 2 { if len(creds) != 2 {
log.Error("usage: writefreely --create-admin username:password") return fmt.Errorf("usage: writefreely --create-admin username:password")
os.Exit(1)
} }
loadConfig(app) loadConfig(app)
@ -534,14 +543,12 @@ func adminCreateUser(app *app, credStr string, isAdmin bool) {
if isAdmin { if isAdmin {
// Abort if trying to create admin user, but one already exists // Abort if trying to create admin user, but one already exists
if firstUser != nil { if firstUser != nil {
log.Error("Admin user already exists (%s). Create a regular user with: writefreely --create-user", firstUser.Username) return fmt.Errorf("Admin user already exists (%s). Create a regular user with: writefreely --create-user", firstUser.Username)
os.Exit(1)
} }
} else { } else {
// Abort if trying to create regular user, but no admin exists yet // Abort if trying to create regular user, but no admin exists yet
if firstUser == nil { if firstUser == nil {
log.Error("No admin user exists yet. Create an admin first with: writefreely --create-admin") return fmt.Errorf("No admin user exists yet. Create an admin first with: writefreely --create-admin")
os.Exit(1)
} }
} }
@ -559,15 +566,13 @@ func adminCreateUser(app *app, credStr string, isAdmin bool) {
} }
if !author.IsValidUsername(app.cfg, username) { if !author.IsValidUsername(app.cfg, username) {
log.Error("Username %s is invalid, reserved, or shorter than configured minimum length (%d characters).", usernameDesc, app.cfg.App.MinUsernameLen) return fmt.Errorf("Username %s is invalid, reserved, or shorter than configured minimum length (%d characters).", usernameDesc, app.cfg.App.MinUsernameLen)
os.Exit(1)
} }
// Hash the password // Hash the password
hashedPass, err := auth.HashPass([]byte(password)) hashedPass, err := auth.HashPass([]byte(password))
if err != nil { if err != nil {
log.Error("Unable to hash password: %v", err) return fmt.Errorf("Unable to hash password: %v", err)
os.Exit(1)
} }
u := &User{ u := &User{
@ -583,11 +588,10 @@ func adminCreateUser(app *app, credStr string, isAdmin bool) {
log.Info("Creating %s %s...", userType, usernameDesc) log.Info("Creating %s %s...", userType, usernameDesc)
err = app.db.CreateUser(u, desiredUsername) err = app.db.CreateUser(u, desiredUsername)
if err != nil { if err != nil {
log.Error("Unable to create user: %s", err) return fmt.Errorf("Unable to create user: %s", err)
os.Exit(1)
} }
log.Info("Done!") log.Info("Done!")
os.Exit(0) return nil
} }
func adminInitDatabase(app *app) error { func adminInitDatabase(app *app) error {