Skip to content

Commit

Permalink
adding config command (#244)
Browse files Browse the repository at this point in the history
* adding config command

* PR comment

* fix typo
  • Loading branch information
jt-dd authored Sep 11, 2024
1 parent 3f4ca04 commit 62d3726
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cmd/kubehound/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"os"

"github.com/DataDog/KubeHound/pkg/cmd"
"github.com/DataDog/KubeHound/pkg/telemetry/log"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var (
configPath string
)

var (
configCmd = &cobra.Command{
Use: "config",
Short: "Show the current configuration",
Long: `[devOnly] Show the current configuration`,
PreRunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", true, true)
},
RunE: func(cobraCmd *cobra.Command, args []string) error {
// Adding datadog setup
khCfg, err := cmd.GetConfig()
if err != nil {
return fmt.Errorf("get config: %w", err)
}

yamlData, err := yaml.Marshal(&khCfg)

if err != nil {
return fmt.Errorf("marshaling khCfg: %w", err)
}

if configPath != "" {
f, err := os.Create(configPath)
if err != nil {
return fmt.Errorf("creating file: %w", err)
}

_, err = f.Write(yamlData)
if err != nil {
return fmt.Errorf("writing to file: %w", err)
}

log.I.Infof("Configuration saved to %s\n", configPath)

return nil
}

fmt.Println("---") //nolint:forbidigo
fmt.Println(string(yamlData)) //nolint:forbidigo

return nil
},
}
)

func init() {
configCmd.Flags().StringVar(&configPath, "path", "", "path to dump current KubeHound configuration")

rootCmd.AddCommand(configCmd)
}

0 comments on commit 62d3726

Please sign in to comment.