From 7fda1cf320f7c803ed2a3b9d634d54dce8724d8f Mon Sep 17 00:00:00 2001 From: Virgil Date: Tue, 31 Mar 2026 19:54:26 +0000 Subject: [PATCH] fix(pkg): accept positional search patterns Co-Authored-By: Virgil --- cmd/core/pkgcmd/cmd_search.go | 16 ++++++++++++---- cmd/core/pkgcmd/cmd_search_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 cmd/core/pkgcmd/cmd_search_test.go diff --git a/cmd/core/pkgcmd/cmd_search.go b/cmd/core/pkgcmd/cmd_search.go index f9a6ad7..888f3e6 100644 --- a/cmd/core/pkgcmd/cmd_search.go +++ b/cmd/core/pkgcmd/cmd_search.go @@ -34,16 +34,14 @@ func addPkgSearchCommand(parent *cobra.Command) { Use: "search", Short: i18n.T("cmd.pkg.search.short"), Long: i18n.T("cmd.pkg.search.long"), + Args: cobra.RangeArgs(0, 1), RunE: func(cmd *cobra.Command, args []string) error { org := searchOrg - pattern := searchPattern + pattern := resolvePkgSearchPattern(searchPattern, args) limit := searchLimit if org == "" { org = "host-uk" } - if pattern == "" { - pattern = "*" - } if limit == 0 { limit = 50 } @@ -223,6 +221,16 @@ func formatPkgSearchUpdatedAt(raw string) string { return cli.FormatAge(updatedAt) } +func resolvePkgSearchPattern(flagPattern string, args []string) string { + if flagPattern != "" { + return flagPattern + } + if len(args) > 0 && strings.TrimSpace(args[0]) != "" { + return args[0] + } + return "*" +} + // matchGlob does simple glob matching with * wildcards func matchGlob(pattern, name string) bool { if pattern == "*" || pattern == "" { diff --git a/cmd/core/pkgcmd/cmd_search_test.go b/cmd/core/pkgcmd/cmd_search_test.go new file mode 100644 index 0000000..f0477fc --- /dev/null +++ b/cmd/core/pkgcmd/cmd_search_test.go @@ -0,0 +1,24 @@ +package pkgcmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestResolvePkgSearchPattern_Good(t *testing.T) { + t.Run("uses flag pattern when set", func(t *testing.T) { + got := resolvePkgSearchPattern("core-*", []string{"api"}) + assert.Equal(t, "core-*", got) + }) + + t.Run("uses positional pattern when flag is empty", func(t *testing.T) { + got := resolvePkgSearchPattern("", []string{"api"}) + assert.Equal(t, "api", got) + }) + + t.Run("defaults to wildcard when nothing is provided", func(t *testing.T) { + got := resolvePkgSearchPattern("", nil) + assert.Equal(t, "*", got) + }) +} -- 2.45.3