CLI: Improve "find" command to support more output formats #3222

It is now also possible to limit the number of results (10k by default).

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2023-06-12 19:07:41 +02:00
parent a891da7dec
commit 39e26b7f0e
3 changed files with 88 additions and 57 deletions

View file

@ -45,6 +45,7 @@ var PhotoPrism = []cli.Command{
StopCommand,
StatusCommand,
IndexCommand,
FindCommand,
ImportCommand,
CopyCommand,
FacesCommand,
@ -65,7 +66,6 @@ var PhotoPrism = []cli.Command{
ShowCommand,
VersionCommand,
ShowConfigCommand,
SearchCommand,
ConnectCommand,
}

87
internal/commands/find.go Normal file
View file

@ -0,0 +1,87 @@
package commands
import (
"context"
"fmt"
"strings"
"github.com/dustin/go-humanize"
"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/search"
"github.com/photoprism/photoprism/pkg/report"
"github.com/photoprism/photoprism/pkg/sortby"
)
// FindCommand configures the command name, flags, and action.
var FindCommand = cli.Command{
Name: "find",
Usage: "Finds indexed files that match the specified search filter",
ArgsUsage: "filter",
Flags: append(report.CliFlags, cli.UintFlag{
Name: "n",
Usage: "maximum number of search `RESULTS`",
Value: 10000,
}),
Action: findAction,
}
// findAction finds indexed files that match the specified search filter.
func findAction(ctx *cli.Context) error {
conf, err := InitConfig(ctx)
_, cancel := context.WithCancel(context.Background())
defer cancel()
if err != nil {
return err
}
conf.RegisterDb()
defer conf.Shutdown()
frm := form.SearchPhotos{
Query: strings.TrimSpace(ctx.Args().First()),
Primary: false,
Merged: false,
Count: ctx.Int("n"),
Offset: 0,
Order: sortby.Name,
}
results, _, err := search.Photos(frm)
if err != nil {
return err
}
format := report.CliFormat(ctx)
// Display just the filename?
if format == report.Default {
for _, found := range results {
fmt.Println(found.FileName)
}
return nil
}
cols := []string{"File Name", "Mime Type", "Size", "SHA1 Hash"}
rows := make([][]string, 0, len(results))
for _, found := range results {
v := []string{found.FileName, found.FileMime, humanize.Bytes(uint64(found.FileSize)), found.FileHash}
rows = append(rows, v)
}
result, err := report.RenderFormat(rows, cols, format)
if err != nil {
return err
}
fmt.Println(result)
return nil
}

View file

@ -1,56 +0,0 @@
package commands
import (
"context"
"time"
"strings"
"fmt"
"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/search"
"github.com/photoprism/photoprism/internal/entity"
)
// SearchCommand registers the search cli command.
var SearchCommand = cli.Command{
Name: "search",
Usage: "Searches in library using filters",
ArgsUsage: "search-query",
Action: searchAction,
}
// searchAction searches all photos in library
func searchAction(ctx *cli.Context) error {
start := time.Now()
conf, err := InitConfig(ctx)
_, cancel := context.WithCancel(context.Background())
defer cancel()
if err != nil {
return err
}
conf.InitDb()
defer conf.Shutdown()
form := form.SearchPhotos{Query: strings.TrimSpace(ctx.Args().First()), Primary: false, Merged: false}
photos, _, err := search.Photos(form)
for _, photo := range photos {
p := entity.Photo{ID: photo.ID}
p.PreloadFiles()
for _, file := range p.Files {
fmt.Printf("%s\n", file.FileName)
}
}
elapsed := time.Since(start)
log.Infof("searched in %s", elapsed)
return nil
}