@ -21,6 +21,7 @@ import (
"github.com/gorilla/sessions"
"github.com/gorilla/sessions"
"github.com/manifoldco/promptui"
"github.com/manifoldco/promptui"
"github.com/writeas/go-strip-markdown"
"github.com/writeas/go-strip-markdown"
"github.com/writeas/web-core/auth"
"github.com/writeas/web-core/converter"
"github.com/writeas/web-core/converter"
"github.com/writeas/web-core/log"
"github.com/writeas/web-core/log"
"github.com/writeas/writefreely/config"
"github.com/writeas/writefreely/config"
@ -178,6 +179,7 @@ func Serve() {
doConfig := flag . Bool ( "config" , false , "Run the configuration process" )
doConfig := flag . Bool ( "config" , false , "Run the configuration process" )
genKeys := flag . Bool ( "gen-keys" , false , "Generate encryption and authentication keys" )
genKeys := flag . Bool ( "gen-keys" , false , "Generate encryption and authentication keys" )
createSchema := flag . Bool ( "init-db" , false , "Initialize app database" )
createSchema := flag . Bool ( "init-db" , false , "Initialize app database" )
createAdmin := flag . String ( "create-admin" , "" , "Create an admin with the given username:password" )
resetPassUser := flag . String ( "reset-pass" , "" , "Reset the given user's password" )
resetPassUser := flag . String ( "reset-pass" , "" , "Reset the given user's password" )
outputVersion := flag . Bool ( "v" , false , "Output the current version" )
outputVersion := flag . Bool ( "v" , false , "Output the current version" )
flag . Parse ( )
flag . Parse ( )
@ -281,6 +283,54 @@ func Serve() {
}
}
}
}
os . Exit ( 0 )
os . Exit ( 0 )
} else if * createAdmin != "" {
// Create an admin user with --create-admin
creds := strings . Split ( * createAdmin , ":" )
if len ( creds ) != 2 {
log . Error ( "usage: writefreely --create-admin username:password" )
os . Exit ( 1 )
}
log . Info ( "Loading configuration..." )
cfg , err := config . Load ( )
if err != nil {
log . Error ( "Unable to load configuration: %v" , err )
os . Exit ( 1 )
}
app . cfg = cfg
connectToDatabase ( app )
defer shutdown ( app )
// Ensure an admin / first user doesn't already exist
if u , _ := app . db . GetUserByID ( 1 ) ; u != nil {
log . Error ( "Admin user already exists (%s). Aborting." , u . Username )
os . Exit ( 1 )
}
// Create the user
username := creds [ 0 ]
password := creds [ 1 ]
hashedPass , err := auth . HashPass ( [ ] byte ( password ) )
if err != nil {
log . Error ( "Unable to hash password: %v" , err )
os . Exit ( 1 )
}
u := & User {
Username : username ,
HashedPass : hashedPass ,
Created : time . Now ( ) . Truncate ( time . Second ) . UTC ( ) ,
}
log . Info ( "Creating user %s...\n" , u . Username )
err = app . db . CreateUser ( u , "" )
if err != nil {
log . Error ( "Unable to create user: %s" , err )
os . Exit ( 1 )
}
log . Info ( "Done!" )
os . Exit ( 0 )
} else if * resetPassUser != "" {
} else if * resetPassUser != "" {
// Connect to the database
// Connect to the database
log . Info ( "Loading configuration..." )
log . Info ( "Loading configuration..." )