Break up CreateUser credentials into two args

Now the func takes a username and password instead of a single string.
This commit is contained in:
Matt Baer 2019-05-12 18:42:57 -04:00
parent 9e43b04f04
commit 77c8152786
2 changed files with 32 additions and 16 deletions

16
app.go
View File

@ -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, "")

View File

@ -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
}