Skip to content

Commit

Permalink
feat: add filter command add "list" subcommand (#159)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
matthisholleville committed Mar 30, 2023
1 parent 9f0cb62 commit 6e17c9e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* 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 filters` to manage filters.
* Run `k8sgpt analyze` to run a scan.
* And use `k8sgpt analyze --explain` to get a more detailed explanation of the issues.

Expand Down Expand Up @@ -76,6 +77,7 @@ Available Commands:
analyze This command will find problems within your Kubernetes cluster
auth Authenticate with your chosen backend
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)
help Help about any command
version Print the version number of k8sgpt
Expand All @@ -98,6 +100,12 @@ k8sgpt auth
k8sgpt analyze --explain
```

_List filters_

```
k8sgpt filters list
```

_Filter on resource_

```
Expand Down
23 changes: 23 additions & 0 deletions cmd/filters/filters.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions cmd/filters/filtersList.go
Original file line number Diff line number Diff line change
@@ -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))
}
},
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"

"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
"k8s.io/client-go/util/homedir"

Expand Down Expand Up @@ -51,6 +52,7 @@ func init() {
}
rootCmd.AddCommand(auth.AuthCmd)
rootCmd.AddCommand(analyze.AnalyzeCmd)
rootCmd.AddCommand(filters.FiltersCmd)
rootCmd.AddCommand(generate.GenerateCmd)
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.")
Expand Down
8 changes: 8 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ func ParseViaAI(ctx context.Context, config *AnalysisConfiguration,
}
return response, nil
}

func ListFilters() []string {
keys := make([]string, 0, len(analyzerMap))
for k := range analyzerMap {
keys = append(keys, k)
}
return keys
}

0 comments on commit 6e17c9e

Please sign in to comment.