Skip to content

Commit

Permalink
feat: update filters add & remove to be more consistent
Browse files Browse the repository at this point in the history
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed Mar 31, 2023
1 parent 0a12448 commit 9aa0e89
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ k8sgpt filters add [filter(s)]
### Examples :

- Simple filter : `k8sgpt filters add Service`
- Multiple filters : `k8sgpt filters add Ingress Pod`
- Multiple filters : `k8sgpt filters add Ingress,Pod`

_Add default filters_

Expand All @@ -120,7 +120,7 @@ k8sgpt filters remove [filter(s)]
### Examples :

- Simple filter : `k8sgpt filters remove Service`
- Multiple filters : `k8sgpt filters remove Ingress Pod`
- Multiple filters : `k8sgpt filters remove Ingress,Pod`

_Run a scan with the default analyzers_

Expand Down
9 changes: 5 additions & 4 deletions cmd/filters/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ var addCmd = &cobra.Command{
Use: "add [filter(s)]",
Short: "Adds one or more new filters.",
Long: `The add command adds one or more new filters to the default set of filters used by the analyze.`,
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filters := strings.Split(args[0], ",")

// Verify filter exist
invalidFilters := []string{}
for _, f := range args {
for _, f := range filters {
foundFilter := false
for _, filter := range analyzer.ListFilters() {
if filter == f {
Expand All @@ -44,7 +45,7 @@ var addCmd = &cobra.Command{
defaultFilters = []string{}
}

mergedFilters := append(defaultFilters, args...)
mergedFilters := append(defaultFilters, filters...)

uniqueFilters, dupplicatedFilters := util.RemoveDuplicates(mergedFilters)

Expand All @@ -60,6 +61,6 @@ var addCmd = &cobra.Command{
color.Red("Error writing config file: %s", err.Error())
os.Exit(1)
}
color.Green("Filter %s added", strings.Join(args, ", "))
color.Green("Filter %s added", strings.Join(filters, ", "))
},
}
7 changes: 4 additions & 3 deletions cmd/filters/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ var removeCmd = &cobra.Command{
Use: "remove [filter(s)]",
Short: "Remove one or more filters.",
Long: `The add command remove one or more filters to the default set of filters used by the analyze.`,
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filters := strings.Split(args[0], ",")

// Get defined default_filters
defaultFilters := viper.GetStringSlice("default_filters")
Expand All @@ -24,7 +25,7 @@ var removeCmd = &cobra.Command{
}

// verify dupplicate filters example: k8sgpt filters remove Pod Pod
uniqueFilters, dupplicatedFilters := util.RemoveDuplicates(args)
uniqueFilters, dupplicatedFilters := util.RemoveDuplicates(filters)
if len(dupplicatedFilters) != 0 {
color.Red("Duplicate filters found: %s", strings.Join(dupplicatedFilters, ", "))
os.Exit(1)
Expand Down Expand Up @@ -57,6 +58,6 @@ var removeCmd = &cobra.Command{
color.Red("Error writing config file: %s", err.Error())
os.Exit(1)
}
color.Green("Filter(s) %s removed", strings.Join(args, ", "))
color.Green("Filter(s) %s removed", strings.Join(filters, ", "))
},
}

0 comments on commit 9aa0e89

Please sign in to comment.