fix(pkg): accept positional search patterns
All checks were successful
Security Scan / security (push) Successful in 18s
All checks were successful
Security Scan / security (push) Successful in 18s
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
0595bf7e0f
commit
7fda1cf320
2 changed files with 36 additions and 4 deletions
|
|
@ -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 == "" {
|
||||
|
|
|
|||
24
cmd/core/pkgcmd/cmd_search_test.go
Normal file
24
cmd/core/pkgcmd/cmd_search_test.go
Normal file
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue