feat: add filter command add "list" subcommand (#159)

* feat: add filter command add "list" subcommand to display available filters

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: create specific file to filterList

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: rename ListAnalyzers to ListFilters

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
This commit is contained in:
HOLLEVILLE Matthis 2023-03-30 20:58:41 +02:00 committed by GitHub
parent 9f0cb6240e
commit 6e17c9e285
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 0 deletions

View File

@ -47,6 +47,7 @@ If you install gcc as suggested, the problem will persist. Therefore, you need t
* Currently the default AI provider is OpenAI, you will need to generate an API key from [OpenAI](https://openai.com) * Currently the default AI provider is OpenAI, you will need to generate an API key from [OpenAI](https://openai.com)
* You can do this by running `k8sgpt generate` to open a browser link to generate it * You can do this by running `k8sgpt generate` to open a browser link to generate it
* Run `k8sgpt auth` to set it in k8sgpt. * Run `k8sgpt auth` to set it in k8sgpt.
* Run `k8sgpt filters` to manage filters.
* Run `k8sgpt analyze` to run a scan. * Run `k8sgpt analyze` to run a scan.
* And use `k8sgpt analyze --explain` to get a more detailed explanation of the issues. * And use `k8sgpt analyze --explain` to get a more detailed explanation of the issues.
@ -76,6 +77,7 @@ Available Commands:
analyze This command will find problems within your Kubernetes cluster analyze This command will find problems within your Kubernetes cluster
auth Authenticate with your chosen backend auth Authenticate with your chosen backend
completion Generate the autocompletion script for the specified shell completion Generate the autocompletion script for the specified shell
filters Manage filters for analyzing Kubernetes resources
generate Generate Key for your chosen backend (opens browser) generate Generate Key for your chosen backend (opens browser)
help Help about any command help Help about any command
version Print the version number of k8sgpt version Print the version number of k8sgpt
@ -98,6 +100,12 @@ k8sgpt auth
k8sgpt analyze --explain k8sgpt analyze --explain
``` ```
_List filters_
```
k8sgpt filters list
```
_Filter on resource_ _Filter on resource_
``` ```

23
cmd/filters/filters.go Normal file
View File

@ -0,0 +1,23 @@
package filters
import (
"github.com/spf13/cobra"
)
var FiltersCmd = &cobra.Command{
Use: "filters",
Aliases: []string{"filters"},
Short: "Manage filters for analyzing Kubernetes resources",
Long: `The filters command allows you to manage filters that are used to analyze Kubernetes resources.
You can list available filters to analyze resources.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
return
}
},
}
func init() {
FiltersCmd.AddCommand(filterListCmd)
}

View File

@ -0,0 +1,21 @@
package filters
import (
"fmt"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/spf13/cobra"
)
var filterListCmd = &cobra.Command{
Use: "list",
Short: "List available filters",
Long: `The list command displays a list of available filters that can be used to analyze Kubernetes resources.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Available filters : \n")
for _, analyzer := range analyzer.ListFilters() {
fmt.Printf("> %s\n", color.GreenString(analyzer))
}
},
}

View File

@ -4,6 +4,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
"github.com/k8sgpt-ai/k8sgpt/cmd/generate" "github.com/k8sgpt-ai/k8sgpt/cmd/generate"
"k8s.io/client-go/util/homedir" "k8s.io/client-go/util/homedir"
@ -51,6 +52,7 @@ func init() {
} }
rootCmd.AddCommand(auth.AuthCmd) rootCmd.AddCommand(auth.AuthCmd)
rootCmd.AddCommand(analyze.AnalyzeCmd) rootCmd.AddCommand(analyze.AnalyzeCmd)
rootCmd.AddCommand(filters.FiltersCmd)
rootCmd.AddCommand(generate.GenerateCmd) rootCmd.AddCommand(generate.GenerateCmd)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k8sgpt.yaml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k8sgpt.yaml)")
rootCmd.PersistentFlags().StringVar(&kubecontext, "kubecontext", "", "Kubernetes context to use. Only required if out-of-cluster.") rootCmd.PersistentFlags().StringVar(&kubecontext, "kubecontext", "", "Kubernetes context to use. Only required if out-of-cluster.")

View File

@ -81,3 +81,11 @@ func ParseViaAI(ctx context.Context, config *AnalysisConfiguration,
} }
return response, nil return response, nil
} }
func ListFilters() []string {
keys := make([]string, 0, len(analyzerMap))
for k := range analyzerMap {
keys = append(keys, k)
}
return keys
}