A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

156 lines
4.0 KiB

  1. /*
  2. * Copyright © 2018-2019 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package main
  11. import (
  12. "flag"
  13. "fmt"
  14. "os"
  15. "strings"
  16. "github.com/gorilla/mux"
  17. "github.com/writeas/web-core/log"
  18. "github.com/writeas/writefreely"
  19. )
  20. func main() {
  21. // General options usable with other commands
  22. debugPtr := flag.Bool("debug", false, "Enables debug logging.")
  23. configFile := flag.String("c", "config.ini", "The configuration file to use")
  24. // Setup actions
  25. createConfig := flag.Bool("create-config", false, "Creates a basic configuration and exits")
  26. doConfig := flag.Bool("config", false, "Run the configuration process")
  27. configSections := flag.String("sections", "server db app", "Which sections of the configuration to go through (requires --config), "+
  28. "valid values are any combination of 'server', 'db' and 'app' "+
  29. "example: writefreely --config --sections \"db app\"")
  30. genKeys := flag.Bool("gen-keys", false, "Generate encryption and authentication keys")
  31. createSchema := flag.Bool("init-db", false, "Initialize app database")
  32. migrate := flag.Bool("migrate", false, "Migrate the database")
  33. // Admin actions
  34. createAdmin := flag.String("create-admin", "", "Create an admin with the given username:password")
  35. createUser := flag.String("create-user", "", "Create a regular user with the given username:password")
  36. deleteUsername := flag.String("delete-user", "", "Delete a user with the given username")
  37. resetPassUser := flag.String("reset-pass", "", "Reset the given user's password")
  38. outputVersion := flag.Bool("v", false, "Output the current version")
  39. flag.Parse()
  40. app := writefreely.NewApp(*configFile)
  41. if *outputVersion {
  42. writefreely.OutputVersion()
  43. os.Exit(0)
  44. } else if *createConfig {
  45. err := writefreely.CreateConfig(app)
  46. if err != nil {
  47. log.Error(err.Error())
  48. os.Exit(1)
  49. }
  50. os.Exit(0)
  51. } else if *doConfig {
  52. writefreely.DoConfig(app, *configSections)
  53. os.Exit(0)
  54. } else if *genKeys {
  55. err := writefreely.GenerateKeyFiles(app)
  56. if err != nil {
  57. log.Error(err.Error())
  58. os.Exit(1)
  59. }
  60. os.Exit(0)
  61. } else if *createSchema {
  62. err := writefreely.CreateSchema(app)
  63. if err != nil {
  64. log.Error(err.Error())
  65. os.Exit(1)
  66. }
  67. os.Exit(0)
  68. } else if *createAdmin != "" {
  69. username, password, err := userPass(*createAdmin, true)
  70. if err != nil {
  71. log.Error(err.Error())
  72. os.Exit(1)
  73. }
  74. err = writefreely.CreateUser(app, username, password, true)
  75. if err != nil {
  76. log.Error(err.Error())
  77. os.Exit(1)
  78. }
  79. os.Exit(0)
  80. } else if *createUser != "" {
  81. username, password, err := userPass(*createUser, false)
  82. if err != nil {
  83. log.Error(err.Error())
  84. os.Exit(1)
  85. }
  86. err = writefreely.CreateUser(app, username, password, false)
  87. if err != nil {
  88. log.Error(err.Error())
  89. os.Exit(1)
  90. }
  91. os.Exit(0)
  92. } else if *resetPassUser != "" {
  93. err := writefreely.ResetPassword(app, *resetPassUser)
  94. if err != nil {
  95. log.Error(err.Error())
  96. os.Exit(1)
  97. }
  98. os.Exit(0)
  99. } else if *deleteUsername != "" {
  100. err := writefreely.DoDeleteAccount(app, *deleteUsername)
  101. if err != nil {
  102. log.Error(err.Error())
  103. os.Exit(1)
  104. }
  105. os.Exit(0)
  106. } else if *migrate {
  107. err := writefreely.Migrate(app)
  108. if err != nil {
  109. log.Error(err.Error())
  110. os.Exit(1)
  111. }
  112. os.Exit(0)
  113. }
  114. // Initialize the application
  115. var err error
  116. log.Info("Starting %s...", writefreely.FormatVersion())
  117. app, err = writefreely.Initialize(app, *debugPtr)
  118. if err != nil {
  119. log.Error("%s", err)
  120. os.Exit(1)
  121. }
  122. // Set app routes
  123. r := mux.NewRouter()
  124. writefreely.InitRoutes(app, r)
  125. app.InitStaticRoutes(r)
  126. // Serve the application
  127. writefreely.Serve(app, r)
  128. }
  129. func userPass(credStr string, isAdmin bool) (user string, pass string, err error) {
  130. creds := strings.Split(credStr, ":")
  131. if len(creds) != 2 {
  132. c := "user"
  133. if isAdmin {
  134. c = "admin"
  135. }
  136. err = fmt.Errorf("usage: writefreely --create-%s username:password", c)
  137. return
  138. }
  139. user = creds[0]
  140. pass = creds[1]
  141. return
  142. }