cli.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/daemon"
  10. "miniflux.app/database"
  11. "miniflux.app/logger"
  12. "miniflux.app/storage"
  13. "miniflux.app/version"
  14. )
  15. const (
  16. flagInfoHelp = "Show application information"
  17. flagVersionHelp = "Show application version"
  18. flagMigrateHelp = "Run SQL migrations"
  19. flagFlsuhSessionsHelp = "Flush all sessions (disconnect users)"
  20. flagCreateAdminHelp = "Create admin user"
  21. flagResetPasswordHelp = "Reset user password"
  22. flagResetFeedErrorsHelp = "Clear all feed errors for all users"
  23. flagDebugModeHelp = "Show debug logs"
  24. )
  25. // Parse parses command line arguments.
  26. func Parse() {
  27. var (
  28. flagInfo bool
  29. flagVersion bool
  30. flagMigrate bool
  31. flagFlushSessions bool
  32. flagCreateAdmin bool
  33. flagResetPassword bool
  34. flagResetFeedErrors bool
  35. flagDebugMode bool
  36. )
  37. flag.BoolVar(&flagInfo, "info", false, flagInfoHelp)
  38. flag.BoolVar(&flagInfo, "i", false, flagInfoHelp)
  39. flag.BoolVar(&flagVersion, "version", false, flagVersionHelp)
  40. flag.BoolVar(&flagVersion, "v", false, flagVersionHelp)
  41. flag.BoolVar(&flagMigrate, "migrate", false, flagMigrateHelp)
  42. flag.BoolVar(&flagFlushSessions, "flush-sessions", false, flagFlsuhSessionsHelp)
  43. flag.BoolVar(&flagCreateAdmin, "create-admin", false, flagCreateAdminHelp)
  44. flag.BoolVar(&flagResetPassword, "reset-password", false, flagResetPasswordHelp)
  45. flag.BoolVar(&flagResetFeedErrors, "reset-feed-errors", false, flagResetFeedErrorsHelp)
  46. flag.BoolVar(&flagDebugMode,"debug", false, flagDebugModeHelp)
  47. flag.Parse()
  48. cfg := config.NewConfig()
  49. if flagDebugMode || cfg.HasDebugMode() {
  50. logger.EnableDebug()
  51. }
  52. db, err := database.NewConnectionPool(cfg.DatabaseURL(), cfg.DatabaseMinConns(), cfg.DatabaseMaxConns())
  53. if err != nil {
  54. logger.Fatal("Unable to connect to the database: %v", err)
  55. }
  56. defer db.Close()
  57. store := storage.NewStorage(db)
  58. if flagInfo {
  59. info()
  60. return
  61. }
  62. if flagVersion {
  63. fmt.Println(version.Version)
  64. return
  65. }
  66. if flagMigrate {
  67. database.Migrate(db)
  68. return
  69. }
  70. if flagResetFeedErrors {
  71. store.ResetFeedErrors()
  72. return
  73. }
  74. if flagFlushSessions {
  75. flushSessions(store)
  76. return
  77. }
  78. if flagCreateAdmin {
  79. createAdmin(store)
  80. return
  81. }
  82. if flagResetPassword {
  83. resetPassword(store)
  84. return
  85. }
  86. // Run migrations and start the deamon.
  87. if cfg.RunMigrations() {
  88. database.Migrate(db)
  89. }
  90. // Create admin user and start the deamon.
  91. if cfg.CreateAdmin() {
  92. createAdmin(store)
  93. }
  94. daemon.Run(cfg, store)
  95. }