From 09360bab6eab60bc6886a17a7d0f973bcd024652 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 1 May 2025 03:49:16 +0100 Subject: [PATCH] list may now accept search pattern arguments --- cmd/gignore/list.go | 32 +++++++++++++++++++++++--------- gignore.go | 26 +++++++++++++++++++++----- internal/registry/registry.go | 15 +++++++++++++-- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/cmd/gignore/list.go b/cmd/gignore/list.go index d309f38..8098b9f 100644 --- a/cmd/gignore/list.go +++ b/cmd/gignore/list.go @@ -5,6 +5,8 @@ package main import ( "context" "fmt" + "io" + "os" "strings" "github.com/spf13/cobra" @@ -16,13 +18,20 @@ var listCmd = &cobra.Command{ Short: "List all .gitignore files in the root template repository", Long: `List all .gitignore files in the root template repository. This command will search the root template repository for .gitignore files and print their paths to the console. -The root template repository can be specified using the --root flag. -You can use this command to quickly find all available .gitignore templates. +You can also provide a pattern to filter the results. Example: - gignore --root= list`, - Run: func(cmd *cobra.Command, _ []string) { - err := listTemplates(cmd.Context()) - cobra.CheckErr(err) + gignore list + gignore list python`, + RunE: func(cmd *cobra.Command, args []string) error { + patterns := args + if len(patterns) == 0 { + patterns = []string{""} + } + err := listTemplates(cmd.Context(), os.Stdout, patterns...) + if err != nil { + return fmt.Errorf("failed to list templates: %w", err) + } + return nil }, } @@ -32,18 +41,23 @@ func init() { } // listTemplates retrieves and prints all .gitignore templates available from the gignore client. -func listTemplates(ctx context.Context) error { +func listTemplates(ctx context.Context, out io.Writer, patterns ...string) error { client := getClientFromContext(ctx) - templates, err := client.List() + templates, err := client.List(patterns...) if err != nil { return err } + if len(templates) == 0 { + fmt.Println("No templates found.") + return nil + } + var output strings.Builder for _, template := range templates { output.WriteString(template + "\n") } - fmt.Print(output.String()) + fmt.Fprint(out, output.String()) return nil } diff --git a/gignore.go b/gignore.go index 6a96f10..3916f5e 100644 --- a/gignore.go +++ b/gignore.go @@ -3,9 +3,9 @@ package gignore import ( "io" + "github.com/charmbracelet/log" "github.com/onyx-and-iris/gignore/internal/filewriter" "github.com/onyx-and-iris/gignore/internal/registry" - log "github.com/sirupsen/logrus" ) //go:generate go run cmd/gen/main.go @@ -16,7 +16,7 @@ const DefaultTemplateDirectory = "gitignoreio" // Client is a client for managing .gitignore templates. type Client struct { registry *registry.TemplateRegistry - writer io.Writer + Writer io.Writer } // New creates a new Client with the provided options. @@ -34,8 +34,24 @@ func New(options ...Option) *Client { } // List returns a list of available .gitignore templates. -func (c *Client) List() ([]string, error) { - return c.registry.List() +func (c *Client) List(patterns ...string) ([]string, error) { + var paths []string + for _, pattern := range patterns { + p, err := c.registry.List(c.registry.Directory, pattern) + if err != nil { + return nil, err + } + paths = append(paths, p...) + + if c.registry.Directory != DefaultTemplateDirectory { + p, err = c.registry.List(DefaultTemplateDirectory, pattern) + if err != nil { + return nil, err + } + paths = append(paths, p...) + } + } + return paths, nil } // Create generates a .gitignore file from the specified template. @@ -69,7 +85,7 @@ func (c *Client) Create(template string) error { return err } - _, err = c.writer.Write(content) + _, err = c.Writer.Write(content) if err != nil { return err } diff --git a/internal/registry/registry.go b/internal/registry/registry.go index c4403a1..58358d3 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -6,6 +6,9 @@ import ( "errors" "fmt" "io/fs" + "strings" + + "github.com/charmbracelet/log" ) //go:embed templates @@ -51,18 +54,26 @@ func (t *TemplateRegistry) Get(name string) ([]byte, error) { } // List lists all the gitignore templates in the registry. -func (t *TemplateRegistry) List() ([]string, error) { +func (t *TemplateRegistry) List(root, substring string) ([]string, error) { var paths []string + rootPath := fmt.Sprintf("templates/%s", root) + log.Debugf("Finding templates containing substring '%s' in %s...", substring, rootPath) err := fs.WalkDir( t.templates, - fmt.Sprintf("templates/%s", t.Directory), + rootPath, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } if !d.IsDir() { + if substring != "" { + if strings.Contains(d.Name(), substring) { + paths = append(paths, path) + } + return nil + } paths = append(paths, path) }