forked from k8sgpt-ai/k8sgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request k8sgpt-ai#161 from matthisholleville/feature/add-a…
…nd-remove-filters feat: add & remove default filter(s) to analyze.
- Loading branch information
Showing
7 changed files
with
248 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
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 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.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
inputFilters := strings.Split(args[0], ",") | ||
availableFilters := analyzer.ListFilters() | ||
|
||
// Verify filter exist | ||
invalidFilters := []string{} | ||
for _, f := range inputFilters { | ||
if f == "" { | ||
color.Red("Filter cannot be empty. Please use correct syntax.") | ||
os.Exit(1) | ||
} | ||
foundFilter := false | ||
for _, filter := range availableFilters { | ||
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 active_filters | ||
activeFilters := viper.GetStringSlice("active_filters") | ||
if len(activeFilters) == 0 { | ||
activeFilters = availableFilters | ||
} | ||
|
||
mergedFilters := append(activeFilters, inputFilters...) | ||
|
||
uniqueFilters, dupplicatedFilters := util.RemoveDuplicates(mergedFilters) | ||
|
||
// Verify dupplicate | ||
if len(dupplicatedFilters) != 0 { | ||
color.Red("Duplicate filters found: %s", strings.Join(dupplicatedFilters, ", ")) | ||
os.Exit(1) | ||
} | ||
|
||
viper.Set("active_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(inputFilters, ", ")) | ||
}, | ||
} |
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,72 @@ | ||
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 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.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
inputFilters := strings.Split(args[0], ",") | ||
|
||
// Get defined active_filters | ||
activeFilters := viper.GetStringSlice("active_filters") | ||
if len(activeFilters) == 0 { | ||
activeFilters = analyzer.ListFilters() | ||
} | ||
|
||
// Check if input input filters is not empty | ||
for _, f := range inputFilters { | ||
if f == "" { | ||
color.Red("Filter cannot be empty. Please use correct syntax.") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// verify dupplicate filters example: k8sgpt filters remove Pod Pod | ||
uniqueFilters, dupplicatedFilters := util.RemoveDuplicates(inputFilters) | ||
if len(dupplicatedFilters) != 0 { | ||
color.Red("Duplicate filters found: %s", strings.Join(dupplicatedFilters, ", ")) | ||
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 activeFilters { | ||
if f == filter { | ||
foundFilter = true | ||
activeFilters = append(activeFilters[:i], activeFilters[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("active_filters", activeFilters) | ||
|
||
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(inputFilters, ", ")) | ||
}, | ||
} |
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