-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add & remove default filter(s) to analyze.
Signed-off-by: Matthis Holleville <[email protected]>
- Loading branch information
1 parent
6e17c9e
commit 32ddf66
Showing
6 changed files
with
195 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package filters | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/util" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var filtersAddCmd = &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), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
// Verify filter exist | ||
invalidFilters := []string{} | ||
for _, f := range args { | ||
foundFilter := false | ||
for _, filter := range analyzer.ListFilters() { | ||
if filter == f { | ||
foundFilter = true | ||
break | ||
} | ||
} | ||
if !foundFilter { | ||
invalidFilters = append(invalidFilters, f) | ||
} | ||
} | ||
|
||
if len(invalidFilters) != 0 { | ||
color.Red("Filter %s does not exist. Please use k8sgpt filters list", strings.Join(invalidFilters, ", ")) | ||
os.Exit(1) | ||
} | ||
|
||
// Get defined default_filters | ||
defaultFilters := viper.GetStringSlice("default_filters") | ||
if len(defaultFilters) == 0 { | ||
defaultFilters = []string{} | ||
} | ||
|
||
mergedFilters := append(defaultFilters, args...) | ||
|
||
uniqueFilters, dupplicateFilters := util.RemoveDuplicates(mergedFilters) | ||
|
||
// Verify dupplicate | ||
if len(dupplicateFilters) != 0 { | ||
color.Red("Duplicate filters found: %s", strings.Join(dupplicateFilters, ", ")) | ||
os.Exit(1) | ||
} | ||
|
||
viper.Set("default_filters", uniqueFilters) | ||
|
||
if err := viper.WriteConfig(); err != nil { | ||
color.Red("Error writing config file: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
color.Green("Filter %s added", strings.Join(args, ", ")) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package filters | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/k8sgpt-ai/k8sgpt/pkg/util" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var filtersRemoveCmd = &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), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
// Get defined default_filters | ||
defaultFilters := viper.GetStringSlice("default_filters") | ||
if len(defaultFilters) == 0 { | ||
defaultFilters = []string{} | ||
} | ||
|
||
// verify dupplicate filters example: k8sgpt filters remove Pod Pod | ||
uniqueFilters, dupplicateFilters := util.RemoveDuplicates(args) | ||
if len(dupplicateFilters) != 0 { | ||
color.Red("Duplicate filters found: %s", strings.Join(dupplicateFilters, ", ")) | ||
os.Exit(1) | ||
} | ||
|
||
// Verify if filter exist in config file and update default_filter | ||
filterNotFound := []string{} | ||
for _, filter := range uniqueFilters { | ||
foundFilter := false | ||
for i, f := range defaultFilters { | ||
if f == filter { | ||
foundFilter = true | ||
defaultFilters = append(defaultFilters[:i], defaultFilters[i+1:]...) | ||
break | ||
} | ||
} | ||
if !foundFilter { | ||
filterNotFound = append(filterNotFound, filter) | ||
} | ||
} | ||
|
||
if len(filterNotFound) != 0 { | ||
color.Red("Filter(s) %s does not exist in configuration file. Please use k8sgpt filters add.", strings.Join(filterNotFound, ", ")) | ||
os.Exit(1) | ||
} | ||
|
||
viper.Set("default_filters", defaultFilters) | ||
|
||
if err := viper.WriteConfig(); err != nil { | ||
color.Red("Error writing config file: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
color.Green("Filter(s) %s removed", strings.Join(args, ", ")) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters