cli.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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
  5. import (
  6. "flag"
  7. "fmt"
  8. "github.com/miniflux/miniflux/config"
  9. "github.com/miniflux/miniflux/daemon"
  10. "github.com/miniflux/miniflux/database"
  11. "github.com/miniflux/miniflux/logger"
  12. "github.com/miniflux/miniflux/storage"
  13. "github.com/miniflux/miniflux/version"
  14. )
  15. // Parse parses command line arguments.
  16. func Parse() {
  17. flagInfo := flag.Bool("info", false, "Show application information")
  18. flagVersion := flag.Bool("version", false, "Show application version")
  19. flagMigrate := flag.Bool("migrate", false, "Migrate database schema")
  20. flagFlushSessions := flag.Bool("flush-sessions", false, "Flush all sessions (disconnect users)")
  21. flagCreateAdmin := flag.Bool("create-admin", false, "Create admin user")
  22. flagResetPassword := flag.Bool("reset-password", false, "Reset user password")
  23. flagResetFeedErrors := flag.Bool("reset-feed-errors", false, "Clear all feed errors for all users")
  24. flagDebugMode := flag.Bool("debug", false, "Enable debug mode (more verbose output)")
  25. flag.Parse()
  26. cfg := config.NewConfig()
  27. if *flagDebugMode || cfg.HasDebugMode() {
  28. logger.EnableDebug()
  29. }
  30. db, err := database.NewConnectionPool(cfg.DatabaseURL(), cfg.DatabaseMinConns(), cfg.DatabaseMaxConns())
  31. if err != nil {
  32. logger.Fatal("Unable to connect to the database: %v", err)
  33. }
  34. defer db.Close()
  35. store := storage.NewStorage(db)
  36. if *flagInfo {
  37. info()
  38. return
  39. }
  40. if *flagVersion {
  41. fmt.Println(version.Version)
  42. return
  43. }
  44. if *flagMigrate {
  45. database.Migrate(db)
  46. return
  47. }
  48. if *flagResetFeedErrors {
  49. store.ResetFeedErrors()
  50. return
  51. }
  52. if *flagFlushSessions {
  53. flushSessions(store)
  54. return
  55. }
  56. if *flagCreateAdmin {
  57. createAdmin(store)
  58. return
  59. }
  60. if *flagResetPassword {
  61. resetPassword(store)
  62. return
  63. }
  64. // Run migrations and start the deamon.
  65. if cfg.RunMigrations() {
  66. database.Migrate(db)
  67. }
  68. // Create admin user and start the deamon.
  69. if cfg.CreateAdmin() {
  70. createAdmin(store)
  71. }
  72. daemon.Run(cfg, store)
  73. }