diff --git a/cmd/crowdsec-cli/config_backup.go b/cmd/crowdsec-cli/cliconfig/backup.go similarity index 95% rename from cmd/crowdsec-cli/config_backup.go rename to cmd/crowdsec-cli/cliconfig/backup.go index 0a58a8c1ab3..5cd34fcf07f 100644 --- a/cmd/crowdsec-cli/config_backup.go +++ b/cmd/crowdsec-cli/cliconfig/backup.go @@ -1,4 +1,4 @@ -package main +package cliconfig import ( "fmt" diff --git a/cmd/crowdsec-cli/config.go b/cmd/crowdsec-cli/cliconfig/config.go similarity index 58% rename from cmd/crowdsec-cli/config.go rename to cmd/crowdsec-cli/cliconfig/config.go index 4cf8916ad4b..22095ac7d5b 100644 --- a/cmd/crowdsec-cli/config.go +++ b/cmd/crowdsec-cli/cliconfig/config.go @@ -1,20 +1,26 @@ -package main +package cliconfig import ( "github.com/spf13/cobra" + + "github.com/crowdsecurity/crowdsec/pkg/csconfig" ) +type configGetter func() *csconfig.Config + +type mergedConfigGetter func() string + type cliConfig struct { cfg configGetter } -func NewCLIConfig(cfg configGetter) *cliConfig { +func New(cfg configGetter) *cliConfig { return &cliConfig{ cfg: cfg, } } -func (cli *cliConfig) NewCommand() *cobra.Command { +func (cli *cliConfig) NewCommand(mergedConfigGetter mergedConfigGetter) *cobra.Command { cmd := &cobra.Command{ Use: "config [command]", Short: "Allows to view current config", @@ -23,7 +29,7 @@ func (cli *cliConfig) NewCommand() *cobra.Command { } cmd.AddCommand(cli.newShowCmd()) - cmd.AddCommand(cli.newShowYAMLCmd()) + cmd.AddCommand(cli.newShowYAMLCmd(mergedConfigGetter)) cmd.AddCommand(cli.newBackupCmd()) cmd.AddCommand(cli.newRestoreCmd()) cmd.AddCommand(cli.newFeatureFlagsCmd()) diff --git a/cmd/crowdsec-cli/config_feature_flags.go b/cmd/crowdsec-cli/cliconfig/feature_flags.go similarity index 96% rename from cmd/crowdsec-cli/config_feature_flags.go rename to cmd/crowdsec-cli/cliconfig/feature_flags.go index 760e2194bb3..c03db10ccce 100644 --- a/cmd/crowdsec-cli/config_feature_flags.go +++ b/cmd/crowdsec-cli/cliconfig/feature_flags.go @@ -1,4 +1,4 @@ -package main +package cliconfig import ( "fmt" @@ -86,7 +86,7 @@ func (cli *cliConfig) featureFlags(showRetired bool) error { fmt.Println("To enable a feature you can: ") fmt.Println(" - set the environment variable CROWDSEC_FEATURE_ to true") - featurePath, err := filepath.Abs(csconfig.GetFeatureFilePath(ConfigFilePath)) + featurePath, err := filepath.Abs(csconfig.GetFeatureFilePath(cli.cfg().FilePath)) if err != nil { // we already read the file, shouldn't happen return err diff --git a/cmd/crowdsec-cli/config_restore.go b/cmd/crowdsec-cli/cliconfig/restore.go similarity index 95% rename from cmd/crowdsec-cli/config_restore.go rename to cmd/crowdsec-cli/cliconfig/restore.go index 75373475ed9..d368b27ea30 100644 --- a/cmd/crowdsec-cli/config_restore.go +++ b/cmd/crowdsec-cli/cliconfig/restore.go @@ -1,4 +1,4 @@ -package main +package cliconfig import ( "fmt" diff --git a/cmd/crowdsec-cli/config_show.go b/cmd/crowdsec-cli/cliconfig/show.go similarity index 99% rename from cmd/crowdsec-cli/config_show.go rename to cmd/crowdsec-cli/cliconfig/show.go index 3d17d264574..90c0ab71069 100644 --- a/cmd/crowdsec-cli/config_show.go +++ b/cmd/crowdsec-cli/cliconfig/show.go @@ -1,4 +1,4 @@ -package main +package cliconfig import ( "encoding/json" diff --git a/cmd/crowdsec-cli/config_showyaml.go b/cmd/crowdsec-cli/cliconfig/showyaml.go similarity index 62% rename from cmd/crowdsec-cli/config_showyaml.go rename to cmd/crowdsec-cli/cliconfig/showyaml.go index 10549648d09..2e46a0171ab 100644 --- a/cmd/crowdsec-cli/config_showyaml.go +++ b/cmd/crowdsec-cli/cliconfig/showyaml.go @@ -1,4 +1,4 @@ -package main +package cliconfig import ( "fmt" @@ -6,19 +6,19 @@ import ( "github.com/spf13/cobra" ) -func (cli *cliConfig) showYAML() error { +func (cli *cliConfig) showYAML(mergedConfig string) error { fmt.Println(mergedConfig) return nil } -func (cli *cliConfig) newShowYAMLCmd() *cobra.Command { +func (cli *cliConfig) newShowYAMLCmd(mergedConfigGetter mergedConfigGetter) *cobra.Command { cmd := &cobra.Command{ Use: "show-yaml", Short: "Displays merged config.yaml + config.yaml.local", Args: cobra.NoArgs, DisableAutoGenTag: true, RunE: func(_ *cobra.Command, _ []string) error { - return cli.showYAML() + return cli.showYAML(mergedConfigGetter()) }, } diff --git a/cmd/crowdsec-cli/clisupport/support.go b/cmd/crowdsec-cli/clisupport/support.go index 5f6032a17bd..eb3e03df253 100644 --- a/cmd/crowdsec-cli/clisupport/support.go +++ b/cmd/crowdsec-cli/clisupport/support.go @@ -290,7 +290,7 @@ func (cli *cliSupport) dumpConfigYAML(zw *zip.Writer) error { cfg := cli.cfg() - config, err := os.ReadFile(*cfg.FilePath) + config, err := os.ReadFile(cfg.FilePath) if err != nil { return fmt.Errorf("could not read config file: %w", err) } diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 936211be7ff..a17bafb96d8 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -17,6 +17,7 @@ import ( "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clialert" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clibouncer" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clicapi" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliconfig" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliconsole" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/clidecision" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliexplain" @@ -256,7 +257,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall cmd.AddCommand(NewCLIDoc().NewCommand(cmd)) cmd.AddCommand(NewCLIVersion().NewCommand()) - cmd.AddCommand(NewCLIConfig(cli.cfg).NewCommand()) + cmd.AddCommand(cliconfig.New(cli.cfg).NewCommand(func() string { return mergedConfig })) cmd.AddCommand(clihub.New(cli.cfg).NewCommand()) cmd.AddCommand(climetrics.New(cli.cfg).NewCommand()) cmd.AddCommand(NewCLIDashboard(cli.cfg).NewCommand()) diff --git a/cmd/crowdsec-cli/require/require.go b/cmd/crowdsec-cli/require/require.go index dd98cd092cb..beffa29f3eb 100644 --- a/cmd/crowdsec-cli/require/require.go +++ b/cmd/crowdsec-cli/require/require.go @@ -27,7 +27,7 @@ func LAPI(c *csconfig.Config) error { func CAPI(c *csconfig.Config) error { if c.API.Server.OnlineClient == nil { - return fmt.Errorf("no configuration for Central API (CAPI) in '%s'", *c.FilePath) + return fmt.Errorf("no configuration for Central API (CAPI) in '%s'", c.FilePath) } return nil diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index 3bbdf607187..b0784e5e6f3 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -30,7 +30,7 @@ var globalConfig = Config{} // Config contains top-level defaults -> overridden by configuration file -> overridden by CLI flags type Config struct { // just a path to ourselves :p - FilePath *string `yaml:"-"` + FilePath string `yaml:"-"` Self []byte `yaml:"-"` Common *CommonCfg `yaml:"common,omitempty"` Prometheus *PrometheusCfg `yaml:"prometheus,omitempty"` @@ -45,9 +45,10 @@ type Config struct { Hub *LocalHubCfg `yaml:"-"` } -func NewConfig(configFile string, disableAgent bool, disableAPI bool, inCli bool) (*Config, string, error) { +// NewConfig +func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*Config, string, error) { patcher := yamlpatch.NewPatcher(configFile, ".local") - patcher.SetQuiet(inCli) + patcher.SetQuiet(quiet) fcontent, err := patcher.MergedPatchContent() if err != nil { @@ -56,7 +57,7 @@ func NewConfig(configFile string, disableAgent bool, disableAPI bool, inCli bool configData := csstring.StrictExpand(string(fcontent), os.LookupEnv) cfg := Config{ - FilePath: &configFile, + FilePath: configFile, DisableAgent: disableAgent, DisableAPI: disableAPI, }