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.
 
 
 
 
 

143 lines
3.5 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. "github.com/gorilla/mux"
  15. "github.com/writeas/web-core/log"
  16. "github.com/writeas/writefreely"
  17. "os"
  18. "strings"
  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. genKeys := flag.Bool("gen-keys", false, "Generate encryption and authentication keys")
  28. createSchema := flag.Bool("init-db", false, "Initialize app database")
  29. migrate := flag.Bool("migrate", false, "Migrate the database")
  30. // Admin actions
  31. createAdmin := flag.String("create-admin", "", "Create an admin with the given username:password")
  32. createUser := flag.String("create-user", "", "Create a regular user with the given username:password")
  33. resetPassUser := flag.String("reset-pass", "", "Reset the given user's password")
  34. outputVersion := flag.Bool("v", false, "Output the current version")
  35. flag.Parse()
  36. app := writefreely.NewApp(*configFile)
  37. if *outputVersion {
  38. writefreely.OutputVersion()
  39. os.Exit(0)
  40. } else if *createConfig {
  41. err := writefreely.CreateConfig(app)
  42. if err != nil {
  43. log.Error(err.Error())
  44. os.Exit(1)
  45. }
  46. os.Exit(0)
  47. } else if *doConfig {
  48. writefreely.DoConfig(app)
  49. os.Exit(0)
  50. } else if *genKeys {
  51. err := writefreely.GenerateKeyFiles(app)
  52. if err != nil {
  53. log.Error(err.Error())
  54. os.Exit(1)
  55. }
  56. os.Exit(0)
  57. } else if *createSchema {
  58. err := writefreely.CreateSchema(app)
  59. if err != nil {
  60. log.Error(err.Error())
  61. os.Exit(1)
  62. }
  63. os.Exit(0)
  64. } else if *createAdmin != "" {
  65. username, password, err := userPass(*createAdmin, true)
  66. if err != nil {
  67. log.Error(err.Error())
  68. os.Exit(1)
  69. }
  70. err = writefreely.CreateUser(app, username, password, true)
  71. if err != nil {
  72. log.Error(err.Error())
  73. os.Exit(1)
  74. }
  75. os.Exit(0)
  76. } else if *createUser != "" {
  77. username, password, err := userPass(*createUser, false)
  78. if err != nil {
  79. log.Error(err.Error())
  80. os.Exit(1)
  81. }
  82. err = writefreely.CreateUser(app, username, password, false)
  83. if err != nil {
  84. log.Error(err.Error())
  85. os.Exit(1)
  86. }
  87. os.Exit(0)
  88. } else if *resetPassUser != "" {
  89. err := writefreely.ResetPassword(app, *resetPassUser)
  90. if err != nil {
  91. log.Error(err.Error())
  92. os.Exit(1)
  93. }
  94. os.Exit(0)
  95. } else if *migrate {
  96. err := writefreely.Migrate(app)
  97. if err != nil {
  98. log.Error(err.Error())
  99. os.Exit(1)
  100. }
  101. os.Exit(0)
  102. }
  103. // Initialize the application
  104. var err error
  105. app, err = writefreely.Initialize(app, *debugPtr)
  106. if err != nil {
  107. log.Error("%s", err)
  108. os.Exit(1)
  109. }
  110. // Set app routes
  111. r := mux.NewRouter()
  112. app.InitRoutes(r)
  113. app.InitStaticRoutes(r)
  114. // Serve the application
  115. writefreely.Serve(app, r)
  116. }
  117. func userPass(credStr string, isAdmin bool) (user string, pass string, err error) {
  118. creds := strings.Split(credStr, ":")
  119. if len(creds) != 2 {
  120. c := "user"
  121. if isAdmin {
  122. c = "admin"
  123. }
  124. err = fmt.Errorf("usage: writefreely --create-%s username:password", c)
  125. return
  126. }
  127. user = creds[0]
  128. pass = creds[1]
  129. return
  130. }