From 02886140a7000512e9d8ef2141b2dab022895af7 Mon Sep 17 00:00:00 2001 From: "Thibault \"bui\" Koechlin" Date: Thu, 28 Oct 2021 09:32:59 +0200 Subject: [PATCH] add a --key to cscli config show (#1029) * add a --key to cscli config show --- cmd/crowdsec-cli/config.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/cmd/crowdsec-cli/config.go b/cmd/crowdsec-cli/config.go index 6112ed9cc..a053349b5 100644 --- a/cmd/crowdsec-cli/config.go +++ b/cmd/crowdsec-cli/config.go @@ -7,6 +7,8 @@ import ( "os" "path/filepath" + "github.com/antonmedv/expr" + "github.com/pkg/errors" "github.com/crowdsecurity/crowdsec/pkg/csconfig" @@ -279,6 +281,10 @@ func NewConfigCmd() *cobra.Command { Args: cobra.ExactArgs(0), DisableAutoGenTag: true, } + var key string + type Env struct { + Config *csconfig.Config + } var cmdConfigShow = &cobra.Command{ Use: "show", Short: "Displays current config", @@ -286,6 +292,36 @@ func NewConfigCmd() *cobra.Command { Args: cobra.ExactArgs(0), DisableAutoGenTag: true, 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 { case "human": 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) var cmdConfigBackup = &cobra.Command{