diff --git a/cmd/piwigo-cli/images_tag.go b/cmd/piwigo-cli/images_tag.go index 7c736d8..4afd17e 100644 --- a/cmd/piwigo-cli/images_tag.go +++ b/cmd/piwigo-cli/images_tag.go @@ -15,11 +15,15 @@ import ( ) type ImagesTagCommand struct { - Id int `short:"i" long:"id" description:"image id to tag" required:"true"` - ExcludeTags string `short:"x" long:"exclude" description:"exclude tag from selection"` + Id int `short:"i" long:"id" description:"image id to tag"` + TagId int `short:"t" long:"tag-id" description:"look up for the first image of this tagId"` + TagName string `short:"T" long:"tag" description:"look up for the first image of this tagName"` + ExcludeTags string `short:"x" long:"exclude" description:"exclude tag from selection"` + KeepSurveyFilter bool `short:"k" long:"keep" description:"keep survey filter"` } func (c *ImagesTagCommand) Execute(args []string) error { + p := piwigo.Piwigo{} if err := p.LoadConfig(); err != nil { return err @@ -30,6 +34,33 @@ func (c *ImagesTagCommand) Execute(args []string) error { return err } + if c.Id == 0 { + data := &url.Values{} + + switch { + case c.TagId > 0: + data.Set("tag_id", fmt.Sprint(c.TagId)) + case c.TagName != "": + data.Set("tag_name", c.TagName) + default: + return fmt.Errorf("id or tagId or tagName are required") + } + + data.Set("order", "date_creation") + data.Set("per_page", "1") + var results struct { + Images []piwigotools.ImageDetails `json:"images"` + } + if err := p.Post("pwg.tags.getImages", data, &results); err != nil { + return err + } + + if len(results.Images) == 0 { + return fmt.Errorf("image not found") + } + c.Id = results.Images[0].Id + } + var imgDetails piwigotools.ImageDetails if err := p.Post("pwg.images.getInfo", &url.Values{ "image_id": []string{fmt.Sprint(c.Id)}, @@ -58,6 +89,7 @@ func (c *ImagesTagCommand) Execute(args []string) error { t := table.NewWriter() t.AppendRows([]table.Row{ + {"Id", imgDetails.Id}, {"Name", imgDetails.Name}, {"Url", imgDetails.Url}, {"CreatedAt", imgDetails.DateCreation}, @@ -75,7 +107,7 @@ func (c *ImagesTagCommand) Execute(args []string) error { exclude = regexp.MustCompile(c.ExcludeTags) } - sel := tags.Tags.Select(exclude) + sel := tags.Tags.Select(exclude, c.KeepSurveyFilter) fmt.Println("Selection:") for _, name := range sel.NamesWithAgeAt(&imgDetails.DateCreation) { diff --git a/internal/piwigo/piwigotools/tags.go b/internal/piwigo/piwigotools/tags.go index cf034a2..06361e4 100644 --- a/internal/piwigo/piwigotools/tags.go +++ b/internal/piwigo/piwigotools/tags.go @@ -52,7 +52,7 @@ func (t Tags) JoinIds(sep string) string { return strings.Join(ids, sep) } -func (t Tags) Select(exclude *regexp.Regexp) Tags { +func (t Tags) Select(exclude *regexp.Regexp, keepFilter bool) Tags { options := make([]string, len(t)) tags := map[string]*Tag{} for i, tag := range t { @@ -66,7 +66,7 @@ func (t Tags) Select(exclude *regexp.Regexp) Tags { Options: options, PageSize: 20, } - survey.AskOne(prompt, &answer, survey.WithKeepFilter(true)) + survey.AskOne(prompt, &answer, survey.WithKeepFilter(keepFilter)) result := make([]*Tag, len(answer)) for i, a := range answer {