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.
 
 
 
 
 

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