add a --key to cscli config show (#1029)

* add a --key to cscli config show
This commit is contained in:
Thibault "bui" Koechlin 2021-10-28 09:32:59 +02:00 committed by GitHub
parent cf57c89177
commit 02886140a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/antonmedv/expr"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/csconfig"
@ -279,6 +281,10 @@ func NewConfigCmd() *cobra.Command {
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
DisableAutoGenTag: true, DisableAutoGenTag: true,
} }
var key string
type Env struct {
Config *csconfig.Config
}
var cmdConfigShow = &cobra.Command{ var cmdConfigShow = &cobra.Command{
Use: "show", Use: "show",
Short: "Displays current config", Short: "Displays current config",
@ -286,6 +292,36 @@ func NewConfigCmd() *cobra.Command {
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(0),
DisableAutoGenTag: true, DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if key != "" {
program, err := expr.Compile(key, expr.Env(Env{}))
if err != nil {
log.Fatal(err)
}
output, err := expr.Run(program, Env{Config: csConfig})
if err != nil {
log.Fatal(err)
}
switch csConfig.Cscli.Output {
case "human", "raw":
switch output.(type) {
case string:
fmt.Printf("%s\n", output)
case int:
fmt.Printf("%d\n", output)
default:
fmt.Printf("%v\n", output)
}
case "json":
data, err := json.MarshalIndent(output, "", " ")
if err != nil {
log.Fatalf("failed to marshal configuration: %s", err)
}
fmt.Printf("%s\n", string(data))
}
return
}
switch csConfig.Cscli.Output { switch csConfig.Cscli.Output {
case "human": case "human":
fmt.Printf("Global:\n") fmt.Printf("Global:\n")
@ -374,6 +410,7 @@ func NewConfigCmd() *cobra.Command {
} }
}, },
} }
cmdConfigShow.Flags().StringVar(&key, "key", "", "Display only this value (config.API.Server.ListenURI")
cmdConfig.AddCommand(cmdConfigShow) cmdConfig.AddCommand(cmdConfigShow)
var cmdConfigBackup = &cobra.Command{ var cmdConfigBackup = &cobra.Command{