Skip to content

Commit

Permalink
cscli config show: avoid globals, use yaml v3
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Feb 27, 2024
1 parent 8e9e091 commit d90f471
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions cmd/crowdsec-cli/config_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/sanity-io/litter"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
)

func showConfigKey(key string) error {
func (cli *cliConfig) showKey(key string) error {
cfg := cli.cfg()

type Env struct {
Config *csconfig.Config
}
Expand All @@ -30,15 +32,15 @@ func showConfigKey(key string) error {
return err
}

output, err := expr.Run(program, Env{Config: csConfig})
output, err := expr.Run(program, Env{Config: cfg})
if err != nil {
return err
}

switch csConfig.Cscli.Output {
switch cfg.Cscli.Output {
case "human", "raw":
// Don't use litter for strings, it adds quotes
// that we didn't have before
// that would break compatibility with previous versions
switch output.(type) {
case string:
fmt.Println(output)
Expand All @@ -51,13 +53,14 @@ func showConfigKey(key string) error {
return fmt.Errorf("failed to marshal configuration: %w", err)
}

fmt.Printf("%s\n", string(data))
fmt.Println(string(data))

Check warning on line 56 in cmd/crowdsec-cli/config_show.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/config_show.go#L56

Added line #L56 was not covered by tests
}

return nil
}

var configShowTemplate = `Global:
func (cli *cliConfig) template() string {
return `Global:
{{- if .ConfigPaths }}
- Configuration Folder : {{.ConfigPaths.ConfigDir}}
Expand Down Expand Up @@ -181,19 +184,11 @@ Central API:
{{- end }}
{{- end }}
`
}

func (cli *cliConfig) show(key string) error {
func (cli *cliConfig) show() error {
cfg := cli.cfg()

if err := cfg.LoadAPIClient(); err != nil {
log.Errorf("failed to load API client configuration: %s", err)
// don't return, we can still show the configuration
}

if key != "" {
return showConfigKey(key)
}

switch cfg.Cscli.Output {
case "human":
// The tests on .Enable look funny because the option has a true default which has
Expand All @@ -204,7 +199,7 @@ func (cli *cliConfig) show(key string) error {
"ValueBool": func(b *bool) bool { return b != nil && *b },
}

tmp, err := template.New("config").Funcs(funcs).Parse(configShowTemplate)
tmp, err := template.New("config").Funcs(funcs).Parse(cli.template())
if err != nil {
return err
}
Expand All @@ -219,14 +214,14 @@ func (cli *cliConfig) show(key string) error {
return fmt.Errorf("failed to marshal configuration: %w", err)
}

fmt.Printf("%s\n", string(data))
fmt.Println(string(data))
case "raw":
data, err := yaml.Marshal(cfg)
if err != nil {
return fmt.Errorf("failed to marshal configuration: %w", err)
}

fmt.Printf("%s\n", string(data))
fmt.Println(string(data))
}

return nil
Expand All @@ -242,7 +237,16 @@ func (cli *cliConfig) newShowCmd() *cobra.Command {
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.show(key)
if err := cli.cfg().LoadAPIClient(); err != nil {
log.Errorf("failed to load API client configuration: %s", err)
// don't return, we can still show the configuration
}

Check warning on line 243 in cmd/crowdsec-cli/config_show.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/config_show.go#L241-L243

Added lines #L241 - L243 were not covered by tests

if key != "" {
return cli.showKey(key)
}

return cli.show()
},
}

Expand Down

0 comments on commit d90f471

Please sign in to comment.