Skip to content

Commit 8bbe129

Browse files
committed
tmp
1 parent aecfb65 commit 8bbe129

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

pkg/choices/choice.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package choices
2+
3+
import (
4+
"strings"
5+
)
6+
7+
type Choice struct {
8+
Name string
9+
Variant string
10+
Group string
11+
}
12+
13+
func (c Choice) Format() string {
14+
s := c.Name
15+
if c.Variant != "" {
16+
s += "/" + c.Variant
17+
}
18+
if c.Group != "" {
19+
s += "@" + c.Group
20+
}
21+
return s
22+
}
23+
24+
func (c *Choice) Match(s string) bool {
25+
if c == nil {
26+
return false
27+
}
28+
29+
other := Parse(s)
30+
switch {
31+
case c.Name != "" && c.Name != other.Name,
32+
c.Variant != "" && c.Variant != other.Variant,
33+
c.Group != "" && c.Group != other.Group:
34+
return false
35+
}
36+
return true
37+
}
38+
39+
func Parse(s string) Choice {
40+
nameVariant, group, _ := strings.Cut(s, "@")
41+
cName, variant, _ := strings.Cut(nameVariant, "/")
42+
return Choice{
43+
Name: cName,
44+
Variant: variant,
45+
Group: group,
46+
}
47+
}

pkg/choices/choices.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func Set(c Choice) error {
4545
return fmt.Errorf("invalid name: %q", c.Format())
4646
}
4747

48-
if c.Group == "" {
49-
return fmt.Errorf("missing group: %q", c.Format())
48+
if c.Variant == "" && c.Group == "" {
49+
return fmt.Errorf("at least one of variant or group must be set: %q", c.Format())
5050
}
5151

5252
configDir, err := xdg.UserConfigDir()

0 commit comments

Comments
 (0)