MCPcopy
hub / github.com/cli/cli / MatchCategory

Function MatchCategory

pkg/cmd/discussion/shared/categories.go:14–32  ·  view source on GitHub ↗

MatchCategory finds a category by name or slug (case-insensitive). It prefers an exact slug match over a name match, so users are encouraged to use slugs for unambiguous lookups.

(input string, categories []client.DiscussionCategory)

Source from the content-addressed store, hash-verified

12// It prefers an exact slug match over a name match, so users are
13// encouraged to use slugs for unambiguous lookups.
14func MatchCategory(input string, categories []client.DiscussionCategory) (*client.DiscussionCategory, error) {
15 for i := range categories {
16 if strings.EqualFold(categories[i].Slug, input) {
17 return &categories[i], nil
18 }
19 }
20 for i := range categories {
21 if strings.EqualFold(categories[i].Name, input) {
22 return &categories[i], nil
23 }
24 }
25
26 slugs := make([]string, len(categories))
27 for i, c := range categories {
28 slugs[i] = c.Slug
29 }
30 slices.Sort(slugs)
31 return nil, fmt.Errorf("unknown category: %q; must be one of: %s", input, strings.Join(slugs, ", "))
32}

Callers 3

listRunFunction · 0.92
createRunFunction · 0.92
editRunFunction · 0.92

Calls 2

JoinMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected