List command: Provide option to whitelist and blacklist groups for listing
parent
f146baf9e0
commit
d97a10d1b3
|
@ -0,0 +1,59 @@
|
|||
package checks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// ListChecks lists the names and desc of all checks in the group if found
|
||||
// lists all checks in the registry if group is not specified
|
||||
func ListChecks(c *cli.Context) error {
|
||||
allChecks, err := filter(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, check := range allChecks {
|
||||
fmt.Printf("%s : %s\n", check.Name(), check.Description())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func filter(c *cli.Context) ([]Check, error) {
|
||||
whitelist := c.StringSlice("g")
|
||||
blacklist := c.StringSlice("G")
|
||||
if len(whitelist) > 0 && len(blacklist) > 0 {
|
||||
return nil, fmt.Errorf("cannot specify both g and G flags")
|
||||
}
|
||||
|
||||
if len(whitelist) > 0 {
|
||||
return GetGroups(whitelist), nil
|
||||
} else if len(blacklist) > 0 {
|
||||
return getChecksNotInGroups(blacklist), nil
|
||||
} else {
|
||||
return List(), nil
|
||||
}
|
||||
}
|
||||
|
||||
// getChecksInGroups retrieves all checks within specified set of groups
|
||||
// returns all checks in the registry if `groups` is unspecified
|
||||
func getChecksNotInGroups(groups []string) []Check {
|
||||
allGroups := ListGroups()
|
||||
var ret []Check
|
||||
for _, group := range allGroups {
|
||||
if !contains(groups, group) {
|
||||
ret = append(ret, GetGroup(group)...)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func contains(groups []string, group string) bool {
|
||||
for _, g := range groups {
|
||||
if g == group {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -85,6 +85,21 @@ func GetGroup(name string) []Check {
|
|||
return ret
|
||||
}
|
||||
|
||||
// GetGroups returns checks that belong to any of the specified group.
|
||||
func GetGroups(groups []string) []Check {
|
||||
registry.mu.RLock()
|
||||
defer registry.mu.RUnlock()
|
||||
var ret []Check
|
||||
for _, group := range groups {
|
||||
if checks, ok := registry.groups[group]; ok {
|
||||
ret = append(ret, checks...)
|
||||
}
|
||||
// else do we log an error?
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
// Get retrieves the specified check from the registry,
|
||||
// throws an error if not found
|
||||
func Get(name string) (Check, error) {
|
||||
|
|
|
@ -36,12 +36,16 @@ func main() {
|
|||
Name: "list",
|
||||
Usage: "list all checks in the registry",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "group, g",
|
||||
Usage: "list all checks in group `GROUP`",
|
||||
cli.StringSliceFlag{
|
||||
Name: "g",
|
||||
Usage: "list all checks in groups `GROUP1, GROUP2`",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "G",
|
||||
Usage: "list all checks not in groups `GROUP1, GROUP2`",
|
||||
},
|
||||
},
|
||||
Action: listChecks,
|
||||
Action: checks.ListChecks,
|
||||
},
|
||||
{
|
||||
Name: "run",
|
||||
|
@ -74,18 +78,6 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
// listChecks lists the names and desc of all checks in the group if found
|
||||
// lists all checks in the registry if group is not specified
|
||||
func listChecks(c *cli.Context) error {
|
||||
group := c.String("group")
|
||||
allChecks := getChecks(group)
|
||||
for _, check := range allChecks {
|
||||
fmt.Printf("%s : %s\n", check.Name(), check.Description())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runChecks(c *cli.Context) error {
|
||||
group := c.String("group")
|
||||
name := c.String("name")
|
||||
|
@ -185,7 +177,7 @@ func filter(level checks.Severity, diagnostics []checks.Diagnostic) []checks.Dia
|
|||
}
|
||||
|
||||
// getChecks retrieves all checks within given group
|
||||
// returns all checks in the registry if group in unspecified
|
||||
// returns all checks in the registry if group is unspecified
|
||||
func getChecks(group string) []checks.Check {
|
||||
if group == "" {
|
||||
return checks.List()
|
||||
|
|
Loading…
Reference in New Issue