diff --git a/internal/commands/backup.go b/internal/commands/backup.go index 9368950fb..e35687ddf 100644 --- a/internal/commands/backup.go +++ b/internal/commands/backup.go @@ -15,7 +15,6 @@ import ( "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/photoprism" - "github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/pkg/clean" "github.com/photoprism/photoprism/pkg/fs" ) @@ -75,13 +74,12 @@ func backupAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/cleanup.go b/internal/commands/cleanup.go index d459917cf..82e7c876b 100644 --- a/internal/commands/cleanup.go +++ b/internal/commands/cleanup.go @@ -7,7 +7,6 @@ import ( "github.com/dustin/go-humanize/english" "github.com/urfave/cli" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/service" ) @@ -31,13 +30,12 @@ var cleanUpFlags = []cli.Flag{ func cleanUpAction(ctx *cli.Context) error { cleanupStart := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/commands.go b/internal/commands/commands.go index f2248642b..40b5b43e6 100644 --- a/internal/commands/commands.go +++ b/internal/commands/commands.go @@ -34,7 +34,6 @@ import ( "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/event" - "github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/pkg/fs" ) @@ -94,13 +93,12 @@ func childAlreadyRunning(filePath string) (pid int, running bool) { // CallWithDependencies calls a command action with initialized dependencies. func CallWithDependencies(ctx *cli.Context, action func(conf *config.Config) error) (err error) { - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/commands_test.go b/internal/commands/commands_test.go index e328f5a97..94e2a6f97 100644 --- a/internal/commands/commands_test.go +++ b/internal/commands/commands_test.go @@ -5,9 +5,11 @@ import ( "testing" "github.com/sirupsen/logrus" + "github.com/urfave/cli" "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/event" + "github.com/photoprism/photoprism/internal/service" ) func TestMain(m *testing.M) { @@ -16,10 +18,13 @@ func TestMain(m *testing.M) { event.AuditLog = log c := config.NewTestConfig("commands") + service.SetConfig(c) + + InitConfig = func(ctx *cli.Context) (*config.Config, error) { + return c, c.Init() + } code := m.Run() - _ = c.CloseDb() - os.Exit(code) } diff --git a/internal/commands/config.go b/internal/commands/config.go new file mode 100644 index 000000000..6a02d014e --- /dev/null +++ b/internal/commands/config.go @@ -0,0 +1,15 @@ +package commands + +import ( + "github.com/urfave/cli" + + "github.com/photoprism/photoprism/internal/config" + "github.com/photoprism/photoprism/internal/service" +) + +// InitConfig initializes the command config. +var InitConfig = func(ctx *cli.Context) (*config.Config, error) { + c := config.NewConfig(ctx) + service.SetConfig(c) + return c, c.Init() +} diff --git a/internal/commands/convert.go b/internal/commands/convert.go index c6e9a6073..e3053a0cf 100644 --- a/internal/commands/convert.go +++ b/internal/commands/convert.go @@ -31,20 +31,19 @@ var ConvertCommand = cli.Command{ func convertAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) - - if !conf.SidecarWritable() { - return config.ErrReadOnly - } + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } + if !conf.SidecarWritable() { + return config.ErrReadOnly + } + conf.RegisterDb() defer conf.Shutdown() diff --git a/internal/commands/copy.go b/internal/commands/copy.go index 1478f1fbf..a3ebc463f 100644 --- a/internal/commands/copy.go +++ b/internal/commands/copy.go @@ -34,21 +34,20 @@ var CopyCommand = cli.Command{ func copyAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) - - // very if copy directory exist and is writable - if conf.ReadOnly() { - return config.ErrReadOnly - } + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } + // very if copy directory exist and is writable + if conf.ReadOnly() { + return config.ErrReadOnly + } + conf.InitDb() defer conf.Shutdown() diff --git a/internal/commands/faces.go b/internal/commands/faces.go index f8bed0c89..6e995508b 100644 --- a/internal/commands/faces.go +++ b/internal/commands/faces.go @@ -79,13 +79,12 @@ var FacesCommand = cli.Command{ func facesStatsAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/import.go b/internal/commands/import.go index 470ab4fe3..17e923954 100644 --- a/internal/commands/import.go +++ b/internal/commands/import.go @@ -34,21 +34,20 @@ var ImportCommand = cli.Command{ func importAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) - - // very if copy directory exist and is writable - if conf.ReadOnly() { - return config.ErrReadOnly - } + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } + // very if copy directory exist and is writable + if conf.ReadOnly() { + return config.ErrReadOnly + } + conf.InitDb() defer conf.Shutdown() diff --git a/internal/commands/index.go b/internal/commands/index.go index 00312543f..74fa931e9 100644 --- a/internal/commands/index.go +++ b/internal/commands/index.go @@ -9,7 +9,6 @@ import ( "github.com/dustin/go-humanize/english" "github.com/urfave/cli" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/pkg/clean" @@ -44,14 +43,12 @@ var indexFlags = []cli.Flag{ func indexAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/index_test.go b/internal/commands/index_test.go index 5c5ff9514..00b69d66e 100644 --- a/internal/commands/index_test.go +++ b/internal/commands/index_test.go @@ -44,11 +44,10 @@ func TestIndexCommand(t *testing.T) { time.Sleep(time.Second) + // Check command output. if l != "" { - // Expected index command output. assert.NotContains(t, l, "error") assert.NotContains(t, l, "warning") - assert.NotContains(t, l, "failed") assert.Contains(t, l, "closed database connection") } else { t.Fatal("log output missing") diff --git a/internal/commands/migrations.go b/internal/commands/migrations.go index 498e7c08e..7e81334fb 100644 --- a/internal/commands/migrations.go +++ b/internal/commands/migrations.go @@ -53,12 +53,12 @@ var MigrationsCommand = cli.Command{ // migrationsStatusAction lists the status of schema migration. func migrationsStatusAction(ctx *cli.Context) error { - conf := config.NewConfig(ctx) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/moments.go b/internal/commands/moments.go index 793eac33e..551fbdcc6 100644 --- a/internal/commands/moments.go +++ b/internal/commands/moments.go @@ -6,7 +6,6 @@ import ( "github.com/urfave/cli" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/service" ) @@ -21,13 +20,12 @@ var MomentsCommand = cli.Command{ func momentsAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/optimize.go b/internal/commands/optimize.go index 2dff59e1b..2b3a8df32 100644 --- a/internal/commands/optimize.go +++ b/internal/commands/optimize.go @@ -6,9 +6,7 @@ import ( "github.com/urfave/cli" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/entity" - "github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/internal/workers" ) @@ -29,13 +27,12 @@ var OptimizeCommand = cli.Command{ func optimizeAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/passwd.go b/internal/commands/passwd.go index f422ea052..4eeb1013a 100644 --- a/internal/commands/passwd.go +++ b/internal/commands/passwd.go @@ -12,9 +12,7 @@ import ( "github.com/urfave/cli" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/entity" - "github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/pkg/clean" ) @@ -27,13 +25,12 @@ var PasswdCommand = cli.Command{ // passwdAction updates a password. func passwdAction(ctx *cli.Context) error { - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/places.go b/internal/commands/places.go index e6efdd227..b3bc94108 100644 --- a/internal/commands/places.go +++ b/internal/commands/places.go @@ -36,13 +36,12 @@ var PlacesCommand = cli.Command{ // placesUpdateAction fetches updated location data. func placesUpdateAction(ctx *cli.Context) error { // Load config. - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/purge.go b/internal/commands/purge.go index e7a28bed7..2516a1a06 100644 --- a/internal/commands/purge.go +++ b/internal/commands/purge.go @@ -9,7 +9,6 @@ import ( "github.com/dustin/go-humanize/english" "github.com/urfave/cli" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/pkg/clean" @@ -39,13 +38,12 @@ var purgeFlags = []cli.Flag{ func purgeAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/reset.go b/internal/commands/reset.go index baf5e4421..26d4f190d 100644 --- a/internal/commands/reset.go +++ b/internal/commands/reset.go @@ -39,12 +39,12 @@ var ResetCommand = cli.Command{ // resetAction resets the index and removes sidecar files after confirmation. func resetAction(ctx *cli.Context) error { - conf := config.NewConfig(ctx) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/restore.go b/internal/commands/restore.go index fa24aae60..d7ec07c76 100644 --- a/internal/commands/restore.go +++ b/internal/commands/restore.go @@ -75,12 +75,12 @@ func restoreAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) + conf, err := InitConfig(ctx) _, cancel := context.WithCancel(context.Background()) defer cancel() - if err := conf.Init(); err != nil { + if err != nil { return err } diff --git a/internal/commands/start.go b/internal/commands/start.go index 86f19cc7f..ab8e34808 100644 --- a/internal/commands/start.go +++ b/internal/commands/start.go @@ -13,11 +13,9 @@ import ( "github.com/urfave/cli" "github.com/photoprism/photoprism/internal/auto" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/mutex" "github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/server" - "github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/internal/session" "github.com/photoprism/photoprism/internal/workers" "github.com/photoprism/photoprism/pkg/clean" @@ -48,8 +46,11 @@ var startFlags = []cli.Flag{ // startAction starts the web server and initializes the daemon. func startAction(ctx *cli.Context) error { - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) + + if err != nil { + return err + } if ctx.IsSet("config") { fmt.Printf("Name Value\n") diff --git a/internal/commands/stop.go b/internal/commands/stop.go index e63793a54..07d2384e0 100644 --- a/internal/commands/stop.go +++ b/internal/commands/stop.go @@ -6,7 +6,6 @@ import ( "github.com/sevlyar/go-daemon" "github.com/urfave/cli" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/pkg/clean" ) @@ -20,7 +19,11 @@ var StopCommand = cli.Command{ // stopAction stops the daemon if it is running. func stopAction(ctx *cli.Context) error { - conf := config.NewConfig(ctx) + conf, err := InitConfig(ctx) + + if err != nil { + return err + } log.Infof("looking for pid in %s", clean.Log(conf.PIDFilename())) diff --git a/internal/commands/thumbs.go b/internal/commands/thumbs.go index 3e26ea958..02201c1a9 100644 --- a/internal/commands/thumbs.go +++ b/internal/commands/thumbs.go @@ -1,11 +1,11 @@ package commands import ( + "context" "time" "github.com/urfave/cli" - "github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/pkg/clean" ) @@ -31,10 +31,12 @@ var ThumbsCommand = cli.Command{ func thumbsAction(ctx *cli.Context) error { start := time.Now() - conf := config.NewConfig(ctx) - service.SetConfig(conf) + conf, err := InitConfig(ctx) - if err := conf.Init(); err != nil { + _, cancel := context.WithCancel(context.Background()) + defer cancel() + + if err != nil { return err }