cli.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. )
  24. // Parse parses command line arguments.
  25. func Parse() {
  26. var (
  27. flagInfo bool
  28. flagVersion bool
  29. flagMigrate bool
  30. flagFlushSessions bool
  31. flagCreateAdmin bool
  32. flagResetPassword bool
  33. flagResetFeedErrors bool
  34. flagDebugMode bool
  35. )
  36. flag.BoolVar(&flagInfo, "info", false, flagInfoHelp)
  37. flag.BoolVar(&flagInfo, "i", false, flagInfoHelp)
  38. flag.BoolVar(&flagVersion, "version", false, flagVersionHelp)
  39. flag.BoolVar(&flagVersion, "v", false, flagVersionHelp)
  40. flag.BoolVar(&flagMigrate, "migrate", false, flagMigrateHelp)
  41. flag.BoolVar(&flagFlushSessions, "flush-sessions", false, flagFlsuhSessionsHelp)
  42. flag.BoolVar(&flagCreateAdmin, "create-admin", false, flagCreateAdminHelp)
  43. flag.BoolVar(&flagResetPassword, "reset-password", false, flagResetPasswordHelp)
  44. flag.BoolVar(&flagResetFeedErrors, "reset-feed-errors", false, flagResetFeedErrorsHelp)
  45. flag.BoolVar(&flagDebugMode, "debug", false, flagDebugModeHelp)
  46. flag.Parse()
  47. if err := config.ParseConfig(); err != nil {
  48. logger.Fatal("%v", err)
  49. }
  50. if flagDebugMode || config.Opts.HasDebugMode() {
  51. logger.EnableDebug()
  52. }
  53. if flagInfo {
  54. info()
  55. return
  56. }
  57. if flagVersion {
  58. fmt.Println(version.Version)
  59. return
  60. }
  61. if config.Opts.IsDefaultDatabaseURL() {
  62. logger.Info("The default value for DATABASE_URL is used")
  63. }
  64. db, err := database.NewConnectionPool(
  65. config.Opts.DatabaseURL(),
  66. config.Opts.DatabaseMinConns(),
  67. config.Opts.DatabaseMaxConns(),
  68. )
  69. if err != nil {
  70. logger.Fatal("Unable to connect to the database: %v", err)
  71. }
  72. defer db.Close()
  73. if flagMigrate {
  74. database.Migrate(db)
  75. return
  76. }
  77. store := storage.NewStorage(db)
  78. if flagResetFeedErrors {
  79. store.ResetFeedErrors()
  80. return
  81. }
  82. if flagFlushSessions {
  83. flushSessions(store)
  84. return
  85. }
  86. if flagCreateAdmin {
  87. createAdmin(store)
  88. return
  89. }
  90. if flagResetPassword {
  91. resetPassword(store)
  92. return
  93. }
  94. // Run migrations and start the deamon.
  95. if config.Opts.RunMigrations() {
  96. database.Migrate(db)
  97. }
  98. // Create admin user and start the deamon.
  99. if config.Opts.CreateAdmin() {
  100. createAdmin(store)
  101. }
  102. startDaemon(store)
  103. }