list may now accept search pattern arguments

This commit is contained in:
onyx-and-iris 2025-05-01 03:49:16 +01:00
parent 825dd3d408
commit 09360bab6e
3 changed files with 57 additions and 16 deletions

View File

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

View File

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

View File

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