add filter for categories

This commit is contained in:
Celogeek 2021-12-23 19:21:48 +01:00
parent 3ef95cd07b
commit f55505babb
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A

View File

@ -3,12 +3,14 @@ package piwigocli
import ( import (
"net/url" "net/url"
"os" "os"
"regexp"
"github.com/celogeek/piwigo-cli/internal/piwigo" "github.com/celogeek/piwigo-cli/internal/piwigo"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
) )
type CategoriesListCommand struct { type CategoriesListCommand struct {
Filter string `short:"x" long:"filter" description:"Regexp filter"`
} }
type GetCategoriesListResponse struct { type GetCategoriesListResponse struct {
@ -38,7 +40,12 @@ func (c *CategoriesListCommand) Execute(args []string) error {
t := table.NewWriter() t := table.NewWriter()
t.AppendHeader(table.Row{"Id", "Name", "Images", "Url"}) t.AppendHeader(table.Row{"Id", "Name", "Images", "Url"})
filter := regexp.MustCompile("")
if c.Filter != "" {
filter = regexp.MustCompile("(?i)" + c.Filter)
}
for _, category := range resp.Categories { for _, category := range resp.Categories {
if filter.MatchString(category.Name) {
t.AppendRow(table.Row{ t.AppendRow(table.Row{
category.Id, category.Id,
category.Name, category.Name,
@ -46,6 +53,7 @@ func (c *CategoriesListCommand) Execute(args []string) error {
category.Url, category.Url,
}) })
} }
}
t.SetOutputMirror(os.Stdout) t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleLight) t.SetStyle(table.StyleLight)