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 ( import (
"context" "context"
"fmt" "fmt"
"io"
"os"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -16,13 +18,20 @@ var listCmd = &cobra.Command{
Short: "List all .gitignore files in the root template repository", Short: "List all .gitignore files in the root template repository",
Long: `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. 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 also provide a pattern to filter the results.
You can use this command to quickly find all available .gitignore templates.
Example: Example:
gignore --root=<path> list`, gignore list
Run: func(cmd *cobra.Command, _ []string) { gignore list python`,
err := listTemplates(cmd.Context()) RunE: func(cmd *cobra.Command, args []string) error {
cobra.CheckErr(err) 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. // 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) client := getClientFromContext(ctx)
templates, err := client.List() templates, err := client.List(patterns...)
if err != nil { if err != nil {
return err return err
} }
if len(templates) == 0 {
fmt.Println("No templates found.")
return nil
}
var output strings.Builder var output strings.Builder
for _, template := range templates { for _, template := range templates {
output.WriteString(template + "\n") output.WriteString(template + "\n")
} }
fmt.Print(output.String()) fmt.Fprint(out, output.String())
return nil return nil
} }

View File

@ -3,9 +3,9 @@ package gignore
import ( import (
"io" "io"
"github.com/charmbracelet/log"
"github.com/onyx-and-iris/gignore/internal/filewriter" "github.com/onyx-and-iris/gignore/internal/filewriter"
"github.com/onyx-and-iris/gignore/internal/registry" "github.com/onyx-and-iris/gignore/internal/registry"
log "github.com/sirupsen/logrus"
) )
//go:generate go run cmd/gen/main.go //go:generate go run cmd/gen/main.go
@ -16,7 +16,7 @@ const DefaultTemplateDirectory = "gitignoreio"
// Client is a client for managing .gitignore templates. // Client is a client for managing .gitignore templates.
type Client struct { type Client struct {
registry *registry.TemplateRegistry registry *registry.TemplateRegistry
writer io.Writer Writer io.Writer
} }
// New creates a new Client with the provided options. // 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. // List returns a list of available .gitignore templates.
func (c *Client) List() ([]string, error) { func (c *Client) List(patterns ...string) ([]string, error) {
return c.registry.List() 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. // Create generates a .gitignore file from the specified template.
@ -69,7 +85,7 @@ func (c *Client) Create(template string) error {
return err return err
} }
_, err = c.writer.Write(content) _, err = c.Writer.Write(content)
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,6 +6,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"strings"
"github.com/charmbracelet/log"
) )
//go:embed templates //go:embed templates
@ -51,18 +54,26 @@ func (t *TemplateRegistry) Get(name string) ([]byte, error) {
} }
// List lists all the gitignore templates in the registry. // 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 var paths []string
rootPath := fmt.Sprintf("templates/%s", root)
log.Debugf("Finding templates containing substring '%s' in %s...", substring, rootPath)
err := fs.WalkDir( err := fs.WalkDir(
t.templates, t.templates,
fmt.Sprintf("templates/%s", t.Directory), rootPath,
func(path string, d fs.DirEntry, err error) error { func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
} }
if !d.IsDir() { if !d.IsDir() {
if substring != "" {
if strings.Contains(d.Name(), substring) {
paths = append(paths, path)
}
return nil
}
paths = append(paths, path) paths = append(paths, path)
} }