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/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
}

View file

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

View file

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

View file

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

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 {
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()

View file

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

View file

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

View file

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

View file

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

View file

@ -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")

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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")

View file

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

View file

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