CLI: Refactor tests and config initialization

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2022-10-04 12:27:40 +02:00
parent e2b315d79d
commit c7be7c43eb
22 changed files with 85 additions and 84 deletions

View File

@@ -15,7 +15,6 @@ import (
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/pkg/clean" "github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs" "github.com/photoprism/photoprism/pkg/fs"
) )
@@ -75,13 +74,12 @@ func backupAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -7,7 +7,6 @@ import (
"github.com/dustin/go-humanize/english" "github.com/dustin/go-humanize/english"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/internal/service"
) )
@@ -31,13 +30,12 @@ var cleanUpFlags = []cli.Flag{
func cleanUpAction(ctx *cli.Context) error { func cleanUpAction(ctx *cli.Context) error {
cleanupStart := time.Now() cleanupStart := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -34,7 +34,6 @@ import (
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/pkg/fs" "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. // CallWithDependencies calls a command action with initialized dependencies.
func CallWithDependencies(ctx *cli.Context, action func(conf *config.Config) error) (err error) { func CallWithDependencies(ctx *cli.Context, action func(conf *config.Config) error) (err error) {
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -5,9 +5,11 @@ import (
"testing" "testing"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/service"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@@ -16,10 +18,13 @@ func TestMain(m *testing.M) {
event.AuditLog = log event.AuditLog = log
c := config.NewTestConfig("commands") c := config.NewTestConfig("commands")
service.SetConfig(c)
InitConfig = func(ctx *cli.Context) (*config.Config, error) {
return c, c.Init()
}
code := m.Run() code := m.Run()
_ = c.CloseDb()
os.Exit(code) os.Exit(code)
} }

View File

@@ -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()
}

View File

@@ -31,20 +31,19 @@ var ConvertCommand = cli.Command{
func convertAction(ctx *cli.Context) error { func convertAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
if !conf.SidecarWritable() {
return config.ErrReadOnly
}
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }
if !conf.SidecarWritable() {
return config.ErrReadOnly
}
conf.RegisterDb() conf.RegisterDb()
defer conf.Shutdown() defer conf.Shutdown()

View File

@@ -34,21 +34,20 @@ var CopyCommand = cli.Command{
func copyAction(ctx *cli.Context) error { func copyAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
// very if copy directory exist and is writable
if conf.ReadOnly() {
return config.ErrReadOnly
}
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }
// very if copy directory exist and is writable
if conf.ReadOnly() {
return config.ErrReadOnly
}
conf.InitDb() conf.InitDb()
defer conf.Shutdown() defer conf.Shutdown()

View File

@@ -79,13 +79,12 @@ var FacesCommand = cli.Command{
func facesStatsAction(ctx *cli.Context) error { func facesStatsAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -34,21 +34,20 @@ var ImportCommand = cli.Command{
func importAction(ctx *cli.Context) error { func importAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
// very if copy directory exist and is writable
if conf.ReadOnly() {
return config.ErrReadOnly
}
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }
// very if copy directory exist and is writable
if conf.ReadOnly() {
return config.ErrReadOnly
}
conf.InitDb() conf.InitDb()
defer conf.Shutdown() defer conf.Shutdown()

View File

@@ -9,7 +9,6 @@ import (
"github.com/dustin/go-humanize/english" "github.com/dustin/go-humanize/english"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/pkg/clean" "github.com/photoprism/photoprism/pkg/clean"
@@ -44,14 +43,12 @@ var indexFlags = []cli.Flag{
func indexAction(ctx *cli.Context) error { func indexAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -44,11 +44,10 @@ func TestIndexCommand(t *testing.T) {
time.Sleep(time.Second) time.Sleep(time.Second)
// Check command output.
if l != "" { if l != "" {
// Expected index command output.
assert.NotContains(t, l, "error") assert.NotContains(t, l, "error")
assert.NotContains(t, l, "warning") assert.NotContains(t, l, "warning")
assert.NotContains(t, l, "failed")
assert.Contains(t, l, "closed database connection") assert.Contains(t, l, "closed database connection")
} else { } else {
t.Fatal("log output missing") t.Fatal("log output missing")

View File

@@ -53,12 +53,12 @@ var MigrationsCommand = cli.Command{
// migrationsStatusAction lists the status of schema migration. // migrationsStatusAction lists the status of schema migration.
func migrationsStatusAction(ctx *cli.Context) error { func migrationsStatusAction(ctx *cli.Context) error {
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -6,7 +6,6 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/internal/service"
) )
@@ -21,13 +20,12 @@ var MomentsCommand = cli.Command{
func momentsAction(ctx *cli.Context) error { func momentsAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -6,9 +6,7 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/internal/workers" "github.com/photoprism/photoprism/internal/workers"
) )
@@ -29,13 +27,12 @@ var OptimizeCommand = cli.Command{
func optimizeAction(ctx *cli.Context) error { func optimizeAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -12,9 +12,7 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/pkg/clean" "github.com/photoprism/photoprism/pkg/clean"
) )
@@ -27,13 +25,12 @@ var PasswdCommand = cli.Command{
// passwdAction updates a password. // passwdAction updates a password.
func passwdAction(ctx *cli.Context) error { func passwdAction(ctx *cli.Context) error {
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -36,13 +36,12 @@ var PlacesCommand = cli.Command{
// placesUpdateAction fetches updated location data. // placesUpdateAction fetches updated location data.
func placesUpdateAction(ctx *cli.Context) error { func placesUpdateAction(ctx *cli.Context) error {
// Load config. // Load config.
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -9,7 +9,6 @@ import (
"github.com/dustin/go-humanize/english" "github.com/dustin/go-humanize/english"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/pkg/clean" "github.com/photoprism/photoprism/pkg/clean"
@@ -39,13 +38,12 @@ var purgeFlags = []cli.Flag{
func purgeAction(ctx *cli.Context) error { func purgeAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -39,12 +39,12 @@ var ResetCommand = cli.Command{
// resetAction resets the index and removes sidecar files after confirmation. // resetAction resets the index and removes sidecar files after confirmation.
func resetAction(ctx *cli.Context) error { func resetAction(ctx *cli.Context) error {
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -75,12 +75,12 @@ func restoreAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
_, cancel := context.WithCancel(context.Background()) _, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
if err := conf.Init(); err != nil { if err != nil {
return err return err
} }

View File

@@ -13,11 +13,9 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/auto" "github.com/photoprism/photoprism/internal/auto"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/mutex" "github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/photoprism" "github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/server" "github.com/photoprism/photoprism/internal/server"
"github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/internal/session" "github.com/photoprism/photoprism/internal/session"
"github.com/photoprism/photoprism/internal/workers" "github.com/photoprism/photoprism/internal/workers"
"github.com/photoprism/photoprism/pkg/clean" "github.com/photoprism/photoprism/pkg/clean"
@@ -48,8 +46,11 @@ var startFlags = []cli.Flag{
// startAction starts the web server and initializes the daemon. // startAction starts the web server and initializes the daemon.
func startAction(ctx *cli.Context) error { func startAction(ctx *cli.Context) error {
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
if err != nil {
return err
}
if ctx.IsSet("config") { if ctx.IsSet("config") {
fmt.Printf("Name Value\n") fmt.Printf("Name Value\n")

View File

@@ -6,7 +6,6 @@ import (
"github.com/sevlyar/go-daemon" "github.com/sevlyar/go-daemon"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/pkg/clean" "github.com/photoprism/photoprism/pkg/clean"
) )
@@ -20,7 +19,11 @@ var StopCommand = cli.Command{
// stopAction stops the daemon if it is running. // stopAction stops the daemon if it is running.
func stopAction(ctx *cli.Context) error { 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())) log.Infof("looking for pid in %s", clean.Log(conf.PIDFilename()))

View File

@@ -1,11 +1,11 @@
package commands package commands
import ( import (
"context"
"time" "time"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/service" "github.com/photoprism/photoprism/internal/service"
"github.com/photoprism/photoprism/pkg/clean" "github.com/photoprism/photoprism/pkg/clean"
) )
@@ -31,10 +31,12 @@ var ThumbsCommand = cli.Command{
func thumbsAction(ctx *cli.Context) error { func thumbsAction(ctx *cli.Context) error {
start := time.Now() start := time.Now()
conf := config.NewConfig(ctx) conf, err := InitConfig(ctx)
service.SetConfig(conf)
if err := conf.Init(); err != nil { _, cancel := context.WithCancel(context.Background())
defer cancel()
if err != nil {
return err return err
} }