Merge branch 'master' into develop

This commit is contained in:
Matt Baer 2019-01-27 14:54:50 -05:00
commit 261a6fefd6
2 changed files with 45 additions and 28 deletions

View File

@ -32,7 +32,9 @@ It's designed to be flexible and share your writing widely, so it's built around
* Start a blog for yourself, or host a community of writers
* Form larger federated networks, and interact over modern protocols like ActivityPub
* Write on a fast, dead-simple, and distraction-free editor
* Format text with Markdown, and organize posts with hashtags
* [Format text](https://howto.write.as/getting-started) with Markdown
* [Organize posts](https://howto.write.as/organization) with hashtags
* Create [static pages](https://howto.write.as/creating-a-static-page)
* Publish drafts and let others proofread them by sharing a private link
* Create multiple lightweight blogs under a single account
* Export all data in plain text files
@ -102,7 +104,7 @@ Ready to hack on your site? Here's a quick overview.
### Setting up
```bash
go get github.com/writeas/writefreely/cmd/writefreely
go get -d github.com/writeas/writefreely/cmd/writefreely
```
Configure your site, create your database, and import the schema [as shown above](#quick-start). Then generate the remaining files you'll need:
@ -148,7 +150,7 @@ working on it, though!
## Contributing
We gladly welcome contributions to WriteFreely, whether in the form of [code](https://github.com/writeas/writefreely/blob/master/CONTRIBUTING.md#contributing-to-writefreely), [bug reports](https://github.com/writeas/writefreely/issues/new?template=bug_report.md), [feature requests](https://discuss.write.as/c/feedback/feature-requests), [translations](https://poeditor.com/join/project/TIZ6HFRFdE), or documentation improvements.
We gladly welcome contributions to WriteFreely, whether in the form of [code](https://github.com/writeas/writefreely/blob/master/CONTRIBUTING.md#contributing-to-writefreely), [bug reports](https://github.com/writeas/writefreely/issues/new?template=bug_report.md), [feature requests](https://discuss.write.as/c/feedback/feature-requests), [translations](https://poeditor.com/join/project/TIZ6HFRFdE), or [documentation](https://github.com/writefreely/documentation) improvements.
Before contributing anything, please read our [Contributing Guide](https://github.com/writeas/writefreely/blob/master/CONTRIBUTING.md#contributing-to-writefreely). It describes the correct channels for submitting contributions and any potential requirements.

65
app.go
View File

@ -239,7 +239,11 @@ func Serve() {
defer shutdown(app)
if !app.db.DatabaseInitialized() {
adminInitDatabase(app)
err = adminInitDatabase(app)
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
u := &User{
@ -294,11 +298,26 @@ func Serve() {
loadConfig(app)
connectToDatabase(app)
defer shutdown(app)
adminInitDatabase(app)
err := adminInitDatabase(app)
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
os.Exit(0)
} 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 != "" {
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 != "" {
// Connect to the database
loadConfig(app)
@ -513,12 +532,15 @@ func shutdown(app *app) {
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
creds := strings.Split(credStr, ":")
if len(creds) != 2 {
log.Error("usage: writefreely --create-admin username:password")
os.Exit(1)
c := "user"
if isAdmin {
c = "admin"
}
return fmt.Errorf("usage: writefreely --create-%s username:password", c)
}
loadConfig(app)
@ -530,14 +552,12 @@ func adminCreateUser(app *app, credStr string, isAdmin bool) {
if isAdmin {
// Abort if trying to create admin user, but one already exists
if firstUser != nil {
log.Error("Admin user already exists (%s). Create a regular user with: writefreely --create-user", firstUser.Username)
os.Exit(1)
return fmt.Errorf("Admin user already exists (%s). Create a regular user with: writefreely --create-user", firstUser.Username)
}
} else {
// Abort if trying to create regular user, but no admin exists yet
if firstUser == nil {
log.Error("No admin user exists yet. Create an admin first with: writefreely --create-admin")
os.Exit(1)
return fmt.Errorf("No admin user exists yet. Create an admin first with: writefreely --create-admin")
}
}
@ -555,15 +575,13 @@ func adminCreateUser(app *app, credStr string, isAdmin bool) {
}
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)
os.Exit(1)
return fmt.Errorf("Username %s is invalid, reserved, or shorter than configured minimum length (%d characters).", usernameDesc, app.cfg.App.MinUsernameLen)
}
// Hash the password
hashedPass, err := auth.HashPass([]byte(password))
if err != nil {
log.Error("Unable to hash password: %v", err)
os.Exit(1)
return fmt.Errorf("Unable to hash password: %v", err)
}
u := &User{
@ -579,14 +597,13 @@ func adminCreateUser(app *app, credStr string, isAdmin bool) {
log.Info("Creating %s %s...", userType, usernameDesc)
err = app.db.CreateUser(u, desiredUsername)
if err != nil {
log.Error("Unable to create user: %s", err)
os.Exit(1)
return fmt.Errorf("Unable to create user: %s", err)
}
log.Info("Done!")
os.Exit(0)
return nil
}
func adminInitDatabase(app *app) {
func adminInitDatabase(app *app) error {
schemaFileName := "schema.sql"
if app.cfg.Database.Type == driverSQLite {
schemaFileName = "sqlite.sql"
@ -594,8 +611,7 @@ func adminInitDatabase(app *app) {
schema, err := Asset(schemaFileName)
if err != nil {
log.Error("Unable to load schema file: %v", err)
os.Exit(1)
return fmt.Errorf("Unable to load schema file: %v", err)
}
tblReg := regexp.MustCompile("CREATE TABLE (IF NOT EXISTS )?`([a-z_]+)`")
@ -623,10 +639,9 @@ func adminInitDatabase(app *app) {
log.Info("Updating appmigrations table...")
err = migrations.SetInitialMigrations(migrations.NewDatastore(app.db.DB, app.db.driverName))
if err != nil {
log.Error("Unable to set initial migrations: %v", err)
os.Exit(1)
return fmt.Errorf("Unable to set initial migrations: %v", err)
}
log.Info("Done.")
os.Exit(0)
log.Info("Done.")
return nil
}