2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-01-03 07:04:48 +01:00
|
|
|
|
2023-08-10 06:15:55 +02:00
|
|
|
package cli // import "miniflux.app/v2/cli"
|
2018-01-03 07:04:48 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
2023-08-10 06:15:55 +02:00
|
|
|
"miniflux.app/v2/config"
|
|
|
|
"miniflux.app/v2/database"
|
|
|
|
"miniflux.app/v2/locale"
|
|
|
|
"miniflux.app/v2/logger"
|
|
|
|
"miniflux.app/v2/storage"
|
|
|
|
"miniflux.app/v2/ui/static"
|
|
|
|
"miniflux.app/v2/version"
|
2018-01-03 07:04:48 +01:00
|
|
|
)
|
|
|
|
|
2018-10-09 06:00:00 +02:00
|
|
|
const (
|
2023-06-25 20:23:23 +02:00
|
|
|
flagInfoHelp = "Show build information"
|
2018-11-12 00:32:48 +01:00
|
|
|
flagVersionHelp = "Show application version"
|
|
|
|
flagMigrateHelp = "Run SQL migrations"
|
2019-09-26 03:55:16 +02:00
|
|
|
flagFlushSessionsHelp = "Flush all sessions (disconnect users)"
|
2018-11-12 00:32:48 +01:00
|
|
|
flagCreateAdminHelp = "Create admin user"
|
|
|
|
flagResetPasswordHelp = "Reset user password"
|
2018-10-09 06:00:00 +02:00
|
|
|
flagResetFeedErrorsHelp = "Clear all feed errors for all users"
|
2018-11-12 00:32:48 +01:00
|
|
|
flagDebugModeHelp = "Show debug logs"
|
2019-06-03 03:20:59 +02:00
|
|
|
flagConfigFileHelp = "Load configuration file"
|
|
|
|
flagConfigDumpHelp = "Print parsed configuration values"
|
2021-02-20 21:42:15 +01:00
|
|
|
flagHealthCheckHelp = `Perform a health check on the given endpoint (the value "auto" try to guess the health check endpoint).`
|
2023-06-25 20:23:23 +02:00
|
|
|
flagRefreshFeedsHelp = "Refresh a batch of feeds and exit"
|
|
|
|
flagRunCleanupTasksHelp = "Run cleanup tasks (delete old sessions and archives old entries)"
|
2018-10-09 06:00:00 +02:00
|
|
|
)
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
// Parse parses command line arguments.
|
|
|
|
func Parse() {
|
2018-10-09 06:00:00 +02:00
|
|
|
var (
|
2019-06-03 03:20:59 +02:00
|
|
|
err error
|
2018-11-12 00:32:48 +01:00
|
|
|
flagInfo bool
|
|
|
|
flagVersion bool
|
|
|
|
flagMigrate bool
|
|
|
|
flagFlushSessions bool
|
|
|
|
flagCreateAdmin bool
|
|
|
|
flagResetPassword bool
|
2018-10-09 06:00:00 +02:00
|
|
|
flagResetFeedErrors bool
|
2018-11-12 00:32:48 +01:00
|
|
|
flagDebugMode bool
|
2019-06-03 03:20:59 +02:00
|
|
|
flagConfigFile string
|
|
|
|
flagConfigDump bool
|
2021-02-20 21:42:15 +01:00
|
|
|
flagHealthCheck string
|
2023-06-25 20:23:23 +02:00
|
|
|
flagRefreshFeeds bool
|
|
|
|
flagRunCleanupTasks bool
|
2018-10-09 06:00:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
flag.BoolVar(&flagInfo, "info", false, flagInfoHelp)
|
|
|
|
flag.BoolVar(&flagInfo, "i", false, flagInfoHelp)
|
|
|
|
flag.BoolVar(&flagVersion, "version", false, flagVersionHelp)
|
|
|
|
flag.BoolVar(&flagVersion, "v", false, flagVersionHelp)
|
|
|
|
flag.BoolVar(&flagMigrate, "migrate", false, flagMigrateHelp)
|
2019-09-26 03:55:16 +02:00
|
|
|
flag.BoolVar(&flagFlushSessions, "flush-sessions", false, flagFlushSessionsHelp)
|
2018-10-09 06:00:00 +02:00
|
|
|
flag.BoolVar(&flagCreateAdmin, "create-admin", false, flagCreateAdminHelp)
|
|
|
|
flag.BoolVar(&flagResetPassword, "reset-password", false, flagResetPasswordHelp)
|
|
|
|
flag.BoolVar(&flagResetFeedErrors, "reset-feed-errors", false, flagResetFeedErrorsHelp)
|
2018-11-12 00:32:48 +01:00
|
|
|
flag.BoolVar(&flagDebugMode, "debug", false, flagDebugModeHelp)
|
2019-06-03 03:20:59 +02:00
|
|
|
flag.StringVar(&flagConfigFile, "config-file", "", flagConfigFileHelp)
|
|
|
|
flag.StringVar(&flagConfigFile, "c", "", flagConfigFileHelp)
|
|
|
|
flag.BoolVar(&flagConfigDump, "config-dump", false, flagConfigDumpHelp)
|
2021-02-20 21:42:15 +01:00
|
|
|
flag.StringVar(&flagHealthCheck, "healthcheck", "", flagHealthCheckHelp)
|
2023-06-25 20:23:23 +02:00
|
|
|
flag.BoolVar(&flagRefreshFeeds, "refresh-feeds", false, flagRefreshFeedsHelp)
|
|
|
|
flag.BoolVar(&flagRunCleanupTasks, "run-cleanup-tasks", false, flagRunCleanupTasksHelp)
|
2018-01-03 07:04:48 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2019-06-03 03:20:59 +02:00
|
|
|
cfg := config.NewParser()
|
|
|
|
|
|
|
|
if flagConfigFile != "" {
|
|
|
|
config.Opts, err = cfg.ParseFile(flagConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Opts, err = cfg.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
2019-06-02 03:18:09 +02:00
|
|
|
logger.Fatal("%v", err)
|
|
|
|
}
|
2018-02-24 03:26:34 +01:00
|
|
|
|
2019-06-03 03:20:59 +02:00
|
|
|
if flagConfigDump {
|
|
|
|
fmt.Print(config.Opts)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-09 02:16:12 +02:00
|
|
|
if config.Opts.LogDateTime() {
|
|
|
|
logger.EnableDateTime()
|
|
|
|
}
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
if flagDebugMode || config.Opts.HasDebugMode() {
|
2018-02-24 03:26:34 +01:00
|
|
|
logger.EnableDebug()
|
|
|
|
}
|
|
|
|
|
2021-02-20 21:42:15 +01:00
|
|
|
if flagHealthCheck != "" {
|
|
|
|
doHealthCheck(flagHealthCheck)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:00:00 +02:00
|
|
|
if flagInfo {
|
2018-01-03 07:04:48 +01:00
|
|
|
info()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:00:00 +02:00
|
|
|
if flagVersion {
|
2018-01-03 07:04:48 +01:00
|
|
|
fmt.Println(version.Version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
if config.Opts.IsDefaultDatabaseURL() {
|
|
|
|
logger.Info("The default value for DATABASE_URL is used")
|
|
|
|
}
|
|
|
|
|
2021-02-17 07:58:44 +01:00
|
|
|
logger.Debug("Loading translations...")
|
|
|
|
if err := locale.LoadCatalogMessages(); err != nil {
|
|
|
|
logger.Fatal("Unable to load translations: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 06:58:04 +01:00
|
|
|
logger.Debug("Loading static assets...")
|
|
|
|
if err := static.CalculateBinaryFileChecksums(); err != nil {
|
|
|
|
logger.Fatal("Unable to calculate binary files checksum: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := static.GenerateStylesheetsBundles(); err != nil {
|
2021-02-19 05:34:58 +01:00
|
|
|
logger.Fatal("Unable to generate Stylesheet bundles: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := static.GenerateJavascriptBundles(); err != nil {
|
|
|
|
logger.Fatal("Unable to generate Javascript bundles: %v", err)
|
2021-02-18 06:58:04 +01:00
|
|
|
}
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
db, err := database.NewConnectionPool(
|
|
|
|
config.Opts.DatabaseURL(),
|
|
|
|
config.Opts.DatabaseMinConns(),
|
|
|
|
config.Opts.DatabaseMaxConns(),
|
2021-05-24 04:32:34 +02:00
|
|
|
config.Opts.DatabaseConnectionLifetime(),
|
2019-06-02 03:18:09 +02:00
|
|
|
)
|
2018-12-03 06:19:09 +01:00
|
|
|
if err != nil {
|
2021-02-20 03:47:50 +01:00
|
|
|
logger.Fatal("Unable to initialize database connection pool: %v", err)
|
2018-12-03 06:19:09 +01:00
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2021-02-20 03:47:50 +01:00
|
|
|
store := storage.NewStorage(db)
|
|
|
|
|
|
|
|
if err := store.Ping(); err != nil {
|
|
|
|
logger.Fatal("Unable to connect to the database: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:00:00 +02:00
|
|
|
if flagMigrate {
|
2021-02-21 22:42:49 +01:00
|
|
|
if err := database.Migrate(db); err != nil {
|
|
|
|
logger.Fatal(`%v`, err)
|
|
|
|
}
|
2018-01-03 07:04:48 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:00:00 +02:00
|
|
|
if flagResetFeedErrors {
|
2018-06-30 23:22:45 +02:00
|
|
|
store.ResetFeedErrors()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:00:00 +02:00
|
|
|
if flagFlushSessions {
|
2018-01-03 07:04:48 +01:00
|
|
|
flushSessions(store)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:00:00 +02:00
|
|
|
if flagCreateAdmin {
|
2018-01-03 07:04:48 +01:00
|
|
|
createAdmin(store)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:00:00 +02:00
|
|
|
if flagResetPassword {
|
2018-01-03 07:18:24 +01:00
|
|
|
resetPassword(store)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:38:32 +02:00
|
|
|
// Run migrations and start the daemon.
|
2019-06-02 03:18:09 +02:00
|
|
|
if config.Opts.RunMigrations() {
|
2021-01-03 01:33:41 +01:00
|
|
|
if err := database.Migrate(db); err != nil {
|
|
|
|
logger.Fatal(`%v`, err)
|
|
|
|
}
|
2018-04-16 05:27:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:01:16 +01:00
|
|
|
if err := database.IsSchemaUpToDate(db); err != nil {
|
|
|
|
logger.Fatal(`You must run the SQL migrations, %v`, err)
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:38:32 +02:00
|
|
|
// Create admin user and start the daemon.
|
2019-06-02 03:18:09 +02:00
|
|
|
if config.Opts.CreateAdmin() {
|
2018-04-16 05:27:10 +02:00
|
|
|
createAdmin(store)
|
|
|
|
}
|
|
|
|
|
2023-06-25 20:23:23 +02:00
|
|
|
if flagRefreshFeeds {
|
|
|
|
refreshFeeds(store)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if flagRunCleanupTasks {
|
|
|
|
runCleanupTasks(store)
|
2023-06-25 07:06:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
startDaemon(store)
|
2018-01-03 07:04:48 +01:00
|
|
|
}
|