From d97a10d1b3275dc88a627729f1a519cfb56fc95f Mon Sep 17 00:00:00 2001 From: Varsha Varadarajan Date: Fri, 28 Jun 2019 09:42:21 -0400 Subject: [PATCH] List command: Provide option to whitelist and blacklist groups for listing --- checks/commands.go | 59 +++++++++++++++++++++++++++++++++++++++++ checks/registry.go | 15 +++++++++++ cmd/clusterlint/main.go | 26 +++++++----------- 3 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 checks/commands.go diff --git a/checks/commands.go b/checks/commands.go new file mode 100644 index 0000000..4cf8bf1 --- /dev/null +++ b/checks/commands.go @@ -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 +} diff --git a/checks/registry.go b/checks/registry.go index 93c7069..bbbe732 100644 --- a/checks/registry.go +++ b/checks/registry.go @@ -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) { diff --git a/cmd/clusterlint/main.go b/cmd/clusterlint/main.go index a8a0a3c..9578955 100644 --- a/cmd/clusterlint/main.go +++ b/cmd/clusterlint/main.go @@ -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()