Skip to content

Commit 971df41

Browse files
committed
feat: expose regex to filter templates in list- commands
1 parent e77cb71 commit 971df41

8 files changed

Lines changed: 40 additions & 114 deletions

cmd/muxt/testdata/howto_list_template_callers_filter.txt renamed to cmd/muxt/testdata/howto_list_template_callers.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
# Tests filtering template callers with --template flag
22

3-
muxt list-template-callers --template=header
3+
muxt list-template-callers --match=header
44
stdout 'template "header" called by:'
55
! stdout 'template "footer" called by:'
66

7-
muxt list-template-callers --template=header --template=footer
7+
muxt list-template-callers --match=header --match=footer
88
stdout 'template "header" called by:'
99
stdout 'template "footer" called by:'
1010
! stdout 'template "layout" called by:'
1111

12+
# 'callers' is an alias for 'list-template-callers'
13+
muxt callers --match=header
14+
stdout 'template "header" called by:'
15+
! stdout 'template "footer" called by:'
16+
1217
-- go.mod --
1318
module server
1419

cmd/muxt/testdata/howto_list_template_callers_alias.txt

Lines changed: 0 additions & 50 deletions
This file was deleted.

cmd/muxt/testdata/howto_list_template_calls_filter.txt renamed to cmd/muxt/testdata/howto_list_template_calls.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
# Tests filtering template calls with --template flag
22

3-
muxt list-template-calls --template=layout
3+
muxt list-template-calls --match=layout
44
stdout 'template "layout" calls:'
55
! stdout 'template "page.gohtml" calls:'
66

7-
muxt list-template-calls --template=layout --template='page.gohtml'
7+
muxt list-template-calls --match=layout --match='page\.gohtml'
88
stdout 'template "layout" calls:'
99
stdout 'template "page.gohtml" calls:'
1010
! stdout 'template "header" calls:'
1111

12+
# 'calls' is an alias for 'list-template-calls'
13+
muxt calls --match=layout
14+
stdout 'template "layout" calls:'
15+
! stdout 'template "page.gohtml" calls:'
16+
1217
-- go.mod --
1318
module server
1419

cmd/muxt/testdata/howto_list_template_calls_alias.txt

Lines changed: 0 additions & 54 deletions
This file was deleted.

internal/analysis/analysis.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"go/token"
66
"go/types"
77
"path/filepath"
8+
"regexp"
89
"strings"
910
"text/template"
1011
)
@@ -18,10 +19,10 @@ var templates = template.Must(template.New("output").Funcs(template.FuncMap{
1819
}).ParseFS(outputTemplates, "*"))
1920

2021
// matchesAny returns true if value contains any of the filter patterns (case-insensitive substring match)
21-
func matchesAny(value string, filters []string) bool {
22+
func matchesAny(value string, filters []*regexp.Regexp) bool {
2223
valueLower := strings.ToLower(value)
2324
for _, filter := range filters {
24-
if strings.Contains(valueLower, strings.ToLower(filter)) {
25+
if filter.MatchString(valueLower) {
2526
return true
2627
}
2728
}

internal/analysis/template_callers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"html/template"
88
"io"
99
"maps"
10+
"regexp"
1011
"slices"
1112
"text/template/parse"
1213

@@ -18,7 +19,7 @@ import (
1819

1920
type TemplateCallersConfiguration struct {
2021
TemplatesVariable string
21-
FilterTemplates []string
22+
FilterTemplates []*regexp.Regexp
2223
}
2324

2425
type TemplateCallers struct {

internal/analysis/template_calls.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"html/template"
77
"io"
88
"maps"
9+
"regexp"
910
"slices"
1011
"text/template/parse"
1112

@@ -17,7 +18,7 @@ import (
1718

1819
type TemplateCallsConfiguration struct {
1920
TemplatesVariable string
20-
FilterTemplates []string
21+
FilterTemplates []*regexp.Regexp
2122
}
2223

2324
type TemplateCalls struct {

internal/cli/commands.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"log"
1212
"os"
1313
"path/filepath"
14+
"regexp"
1415
"runtime/debug"
1516
"strings"
1617

@@ -314,6 +315,7 @@ func writeCodeGenerationComment(w io.StringWriter, args []string) {
314315
func listTemplateCallersCommand(wd *string) *cobra.Command {
315316
var config analysis.TemplateCallersConfiguration
316317

318+
var patterns []string
317319
cmd := &cobra.Command{
318320
Use: listTemplateCallersCommandName,
319321
Aliases: []string{"callers"},
@@ -322,6 +324,13 @@ func listTemplateCallersCommand(wd *string) *cobra.Command {
322324
if config.TemplatesVariable == "" {
323325
config.TemplatesVariable = defaultTemplatesVariableName
324326
}
327+
for _, pattern := range patterns {
328+
pat, err := regexp.Compile(pattern)
329+
if err != nil {
330+
return err
331+
}
332+
config.FilterTemplates = append(config.FilterTemplates, pat)
333+
}
325334

326335
fileSet, pl, err := asteval.LoadPackages(*wd)
327336
if err != nil {
@@ -336,13 +345,14 @@ func listTemplateCallersCommand(wd *string) *cobra.Command {
336345
}
337346

338347
addUseTemplatesVarToFlagSet(cmd.Flags(), &config.TemplatesVariable)
339-
cmd.Flags().StringArrayVar(&config.FilterTemplates, "template", nil, "filter by template name (can specify multiple times)")
348+
cmd.Flags().StringArrayVar(&patterns, "match", nil, "filter by template name (can specify multiple regular expressions)")
340349

341350
return cmd
342351
}
343352

344353
func listTemplateCallsCommand(wd *string) *cobra.Command {
345354
var config analysis.TemplateCallsConfiguration
355+
var patterns []string
346356

347357
cmd := &cobra.Command{
348358
Use: listTemplateCallsCommandName,
@@ -352,6 +362,13 @@ func listTemplateCallsCommand(wd *string) *cobra.Command {
352362
if config.TemplatesVariable == "" {
353363
config.TemplatesVariable = defaultTemplatesVariableName
354364
}
365+
for _, pattern := range patterns {
366+
pat, err := regexp.Compile(pattern)
367+
if err != nil {
368+
return err
369+
}
370+
config.FilterTemplates = append(config.FilterTemplates, pat)
371+
}
355372

356373
_, pl, err := asteval.LoadPackages(*wd)
357374
if err != nil {
@@ -366,7 +383,7 @@ func listTemplateCallsCommand(wd *string) *cobra.Command {
366383
}
367384

368385
addUseTemplatesVarToFlagSet(cmd.Flags(), &config.TemplatesVariable)
369-
cmd.Flags().StringArrayVar(&config.FilterTemplates, "template", nil, "filter by template name (can specify multiple times)")
386+
cmd.Flags().StringArrayVar(&patterns, "match", nil, "filter by template name (can specify multiple regular expressions)")
370387

371388
return cmd
372389
}

0 commit comments

Comments
 (0)