cli.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package cli // import "miniflux.app/cli"
  5. import (
  6. "flag"
  7. "fmt"
  8. "miniflux.app/config"
  9. "miniflux.app/database"
  10. "miniflux.app/logger"
  11. "miniflux.app/storage"
  12. "miniflux.app/version"
  13. )
  14. const (
  15. flagInfoHelp = "Show application information"
  16. flagVersionHelp = "Show application version"
  17. flagMigrateHelp = "Run SQL migrations"
  18. flagFlsuhSessionsHelp = "Flush all sessions (disconnect users)"
  19. flagCreateAdminHelp = "Create admin user"
  20. flagResetPasswordHelp = "Reset user password"
  21. flagResetFeedErrorsHelp = "Clear all feed errors for all users"
  22. flagDebugModeHelp = "Show debug logs"
  23. flagConfigFileHelp = "Load configuration file"
  24. flagConfigDumpHelp = "Print parsed configuration values"
  25. )
  26. // Parse parses command line arguments.
  27. func Parse() {
  28. var (
  29. err error
  30. flagInfo bool
  31. flagVersion bool
  32. flagMigrate bool
  33. flagFlushSessions bool
  34. flagCreateAdmin bool
  35. flagResetPassword bool
  36. flagResetFeedErrors bool
  37. flagDebugMode bool
  38. flagConfigFile string
  39. flagConfigDump bool
  40. )
  41. flag.BoolVar(&flagInfo, "info", false, flagInfoHelp)
  42. flag.BoolVar(&flagInfo, "i", false, flagInfoHelp)
  43. flag.BoolVar(&flagVersion, "version", false, flagVersionHelp)
  44. flag.BoolVar(&flagVersion, "v", false, flagVersionHelp)
  45. flag.BoolVar(&flagMigrate, "migrate", false, flagMigrateHelp)
  46. flag.BoolVar(&flagFlushSessions, "flush-sessions", false, flagFlsuhSessionsHelp)
  47. flag.BoolVar(&flagCreateAdmin, "create-admin", false, flagCreateAdminHelp)
  48. flag.BoolVar(&flagResetPassword, "reset-password", false, flagResetPasswordHelp)
  49. flag.BoolVar(&flagResetFeedErrors, "reset-feed-errors", false, flagResetFeedErrorsHelp)
  50. flag.BoolVar(&flagDebugMode, "debug", false, flagDebugModeHelp)
  51. flag.StringVar(&flagConfigFile, "config-file", "", flagConfigFileHelp)
  52. flag.StringVar(&flagConfigFile, "c", "", flagConfigFileHelp)
  53. flag.BoolVar(&flagConfigDump, "config-dump", false, flagConfigDumpHelp)
  54. flag.Parse()
  55. cfg := config.NewParser()
  56. if flagConfigFile != "" {
  57. config.Opts, err = cfg.ParseFile(flagConfigFile)
  58. if err != nil {
  59. logger.Fatal("%v", err)
  60. }
  61. }
  62. config.Opts, err = cfg.ParseEnvironmentVariables()
  63. if err != nil {
  64. logger.Fatal("%v", err)
  65. }
  66. if flagConfigDump {
  67. fmt.Print(config.Opts)
  68. return
  69. }
  70. if flagDebugMode || config.Opts.HasDebugMode() {
  71. logger.EnableDebug()
  72. }
  73. if flagInfo {
  74. info()
  75. return
  76. }
  77. if flagVersion {
  78. fmt.Println(version.Version)
  79. return
  80. }
  81. if config.Opts.IsDefaultDatabaseURL() {
  82. logger.Info("The default value for DATABASE_URL is used")
  83. }
  84. db, err := database.NewConnectionPool(
  85. config.Opts.DatabaseURL(),
  86. config.Opts.DatabaseMinConns(),
  87. config.Opts.DatabaseMaxConns(),
  88. )
  89. if err != nil {
  90. logger.Fatal("Unable to connect to the database: %v", err)
  91. }
  92. defer db.Close()
  93. if flagMigrate {
  94. database.Migrate(db)
  95. return
  96. }
  97. store := storage.NewStorage(db)
  98. if flagResetFeedErrors {
  99. store.ResetFeedErrors()
  100. return
  101. }
  102. if flagFlushSessions {
  103. flushSessions(store)
  104. return
  105. }
  106. if flagCreateAdmin {
  107. createAdmin(store)
  108. return
  109. }
  110. if flagResetPassword {
  111. resetPassword(store)
  112. return
  113. }
  114. // Run migrations and start the deamon.
  115. if config.Opts.RunMigrations() {
  116. database.Migrate(db)
  117. }
  118. // Create admin user and start the deamon.
  119. if config.Opts.CreateAdmin() {
  120. createAdmin(store)
  121. }
  122. startDaemon(store)
  123. }