2018-01-03 01:04:48 -05:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package cli // import "miniflux.app/cli"
|
2018-01-03 01:04:48 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/config"
|
|
|
|
"miniflux.app/database"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/storage"
|
|
|
|
"miniflux.app/version"
|
2018-01-03 01:04:48 -05:00
|
|
|
)
|
|
|
|
|
2018-10-09 00:00:00 -04:00
|
|
|
const (
|
2018-11-11 18:32:48 -05:00
|
|
|
flagInfoHelp = "Show application information"
|
|
|
|
flagVersionHelp = "Show application version"
|
|
|
|
flagMigrateHelp = "Run SQL migrations"
|
2019-09-25 21:55:16 -04:00
|
|
|
flagFlushSessionsHelp = "Flush all sessions (disconnect users)"
|
2018-11-11 18:32:48 -05:00
|
|
|
flagCreateAdminHelp = "Create admin user"
|
|
|
|
flagResetPasswordHelp = "Reset user password"
|
2018-10-09 00:00:00 -04:00
|
|
|
flagResetFeedErrorsHelp = "Clear all feed errors for all users"
|
2018-11-11 18:32:48 -05:00
|
|
|
flagDebugModeHelp = "Show debug logs"
|
2019-06-02 21:20:59 -04:00
|
|
|
flagConfigFileHelp = "Load configuration file"
|
|
|
|
flagConfigDumpHelp = "Print parsed configuration values"
|
2018-10-09 00:00:00 -04:00
|
|
|
)
|
|
|
|
|
2018-01-03 01:04:48 -05:00
|
|
|
// Parse parses command line arguments.
|
|
|
|
func Parse() {
|
2018-10-09 00:00:00 -04:00
|
|
|
var (
|
2019-06-02 21:20:59 -04:00
|
|
|
err error
|
2018-11-11 18:32:48 -05:00
|
|
|
flagInfo bool
|
|
|
|
flagVersion bool
|
|
|
|
flagMigrate bool
|
|
|
|
flagFlushSessions bool
|
|
|
|
flagCreateAdmin bool
|
|
|
|
flagResetPassword bool
|
2018-10-09 00:00:00 -04:00
|
|
|
flagResetFeedErrors bool
|
2018-11-11 18:32:48 -05:00
|
|
|
flagDebugMode bool
|
2019-06-02 21:20:59 -04:00
|
|
|
flagConfigFile string
|
|
|
|
flagConfigDump bool
|
2018-10-09 00:00:00 -04: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-25 21:55:16 -04:00
|
|
|
flag.BoolVar(&flagFlushSessions, "flush-sessions", false, flagFlushSessionsHelp)
|
2018-10-09 00:00:00 -04: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-11 18:32:48 -05:00
|
|
|
flag.BoolVar(&flagDebugMode, "debug", false, flagDebugModeHelp)
|
2019-06-02 21:20:59 -04:00
|
|
|
flag.StringVar(&flagConfigFile, "config-file", "", flagConfigFileHelp)
|
|
|
|
flag.StringVar(&flagConfigFile, "c", "", flagConfigFileHelp)
|
|
|
|
flag.BoolVar(&flagConfigDump, "config-dump", false, flagConfigDumpHelp)
|
2018-01-03 01:04:48 -05:00
|
|
|
flag.Parse()
|
|
|
|
|
2019-06-02 21:20:59 -04: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-01 21:18:09 -04:00
|
|
|
logger.Fatal("%v", err)
|
|
|
|
}
|
2018-02-23 21:26:34 -05:00
|
|
|
|
2019-06-02 21:20:59 -04:00
|
|
|
if flagConfigDump {
|
|
|
|
fmt.Print(config.Opts)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-08 20:16:12 -04:00
|
|
|
if config.Opts.LogDateTime() {
|
|
|
|
logger.EnableDateTime()
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:18:09 -04:00
|
|
|
if flagDebugMode || config.Opts.HasDebugMode() {
|
2018-02-23 21:26:34 -05:00
|
|
|
logger.EnableDebug()
|
|
|
|
}
|
|
|
|
|
2018-10-09 00:00:00 -04:00
|
|
|
if flagInfo {
|
2018-01-03 01:04:48 -05:00
|
|
|
info()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 00:00:00 -04:00
|
|
|
if flagVersion {
|
2018-01-03 01:04:48 -05:00
|
|
|
fmt.Println(version.Version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:18:09 -04:00
|
|
|
if config.Opts.IsDefaultDatabaseURL() {
|
|
|
|
logger.Info("The default value for DATABASE_URL is used")
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := database.NewConnectionPool(
|
|
|
|
config.Opts.DatabaseURL(),
|
|
|
|
config.Opts.DatabaseMinConns(),
|
|
|
|
config.Opts.DatabaseMaxConns(),
|
|
|
|
)
|
2018-12-03 00:19:09 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("Unable to connect to the database: %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2018-10-09 00:00:00 -04:00
|
|
|
if flagMigrate {
|
2018-08-01 23:28:45 -04:00
|
|
|
database.Migrate(db)
|
2018-01-03 01:04:48 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:19:09 -05:00
|
|
|
store := storage.NewStorage(db)
|
|
|
|
|
2018-10-09 00:00:00 -04:00
|
|
|
if flagResetFeedErrors {
|
2018-06-30 17:22:45 -04:00
|
|
|
store.ResetFeedErrors()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 00:00:00 -04:00
|
|
|
if flagFlushSessions {
|
2018-01-03 01:04:48 -05:00
|
|
|
flushSessions(store)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 00:00:00 -04:00
|
|
|
if flagCreateAdmin {
|
2018-01-03 01:04:48 -05:00
|
|
|
createAdmin(store)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 00:00:00 -04:00
|
|
|
if flagResetPassword {
|
2018-01-03 01:18:24 -05:00
|
|
|
resetPassword(store)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-15 23:27:10 -04:00
|
|
|
// Run migrations and start the deamon.
|
2019-06-01 21:18:09 -04:00
|
|
|
if config.Opts.RunMigrations() {
|
2018-08-01 23:28:45 -04:00
|
|
|
database.Migrate(db)
|
2018-04-15 23:27:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create admin user and start the deamon.
|
2019-06-01 21:18:09 -04:00
|
|
|
if config.Opts.CreateAdmin() {
|
2018-04-15 23:27:10 -04:00
|
|
|
createAdmin(store)
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:18:09 -04:00
|
|
|
startDaemon(store)
|
2018-01-03 01:04:48 -05:00
|
|
|
}
|