Skip to content

Commit c0b6f99

Browse files
committed
Validate --only & --hide parameters
1 parent 9335e29 commit c0b6f99

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

main.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,36 @@ func parseStyle(styleOpt string) (table.Style, error) {
9696

9797
// parseCommaSeparatedValues parses comma separated string into a map.
9898
func parseCommaSeparatedValues(values string) map[string]struct{} {
99-
items := make(map[string]struct{})
100-
for _, value := range strings.Split(values, ",") {
101-
value = strings.TrimSpace(value)
102-
if len(value) == 0 {
99+
m := make(map[string]struct{})
100+
for _, v := range strings.Split(values, ",") {
101+
v = strings.TrimSpace(v)
102+
if len(v) == 0 {
103103
continue
104104
}
105-
value = strings.ToLower(value)
106105

107-
items[value] = struct{}{}
106+
v = strings.ToLower(v)
107+
m[v] = struct{}{}
108108
}
109-
return items
109+
return m
110+
}
111+
112+
// validateGroups validates the parsed group maps.
113+
func validateGroups(m map[string]struct{}) error {
114+
for k := range m {
115+
found := false
116+
for _, g := range groups {
117+
if g == k {
118+
found = true
119+
break
120+
}
121+
}
122+
123+
if !found {
124+
return fmt.Errorf("unknown device group: %s", k)
125+
}
126+
}
127+
128+
return nil
110129
}
111130

112131
func main() {
@@ -189,6 +208,16 @@ func main() {
189208
HiddenFilesystems: parseCommaSeparatedValues(*hideFs),
190209
OnlyFilesystems: parseCommaSeparatedValues(*onlyFs),
191210
}
211+
err = validateGroups(filters.HiddenDevices)
212+
if err != nil {
213+
fmt.Println(err)
214+
os.Exit(1)
215+
}
216+
err = validateGroups(filters.OnlyDevices)
217+
if err != nil {
218+
fmt.Println(err)
219+
os.Exit(1)
220+
}
192221

193222
// validate arguments
194223
if len(flag.Args()) > 0 {

0 commit comments

Comments
 (0)