parseCategories converts a comma-separated string into a Category slice. "all" returns all categories.
(s string)
| 65 | // parseCategories converts a comma-separated string into a Category slice. |
| 66 | // "all" returns all categories. |
| 67 | func parseCategories(s string) ([]types.Category, error) { |
| 68 | s = strings.TrimSpace(s) |
| 69 | if strings.EqualFold(s, "all") { |
| 70 | return types.AllCategories, nil |
| 71 | } |
| 72 | |
| 73 | categoryMap := make(map[string]types.Category, len(types.AllCategories)) |
| 74 | for _, c := range types.AllCategories { |
| 75 | categoryMap[c.String()] = c |
| 76 | } |
| 77 | |
| 78 | var categories []types.Category |
| 79 | for _, name := range strings.Split(s, ",") { |
| 80 | name = strings.TrimSpace(strings.ToLower(name)) |
| 81 | if name == "" { |
| 82 | continue |
| 83 | } |
| 84 | c, ok := categoryMap[name] |
| 85 | if !ok { |
| 86 | return nil, fmt.Errorf("unknown category: %q, available: all|%s", name, categoryNames()) |
| 87 | } |
| 88 | categories = append(categories, c) |
| 89 | } |
| 90 | if len(categories) == 0 { |
| 91 | return nil, fmt.Errorf("no categories specified") |
| 92 | } |
| 93 | return categories, nil |
| 94 | } |
| 95 | |
| 96 | func categoryNames() string { |
| 97 | names := make([]string, len(types.AllCategories)) |
no test coverage detected