Skip to content

Commit

Permalink
cscli: cliconfig - remove global variables and gratuitous pointer (#3414
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mmetc authored Jan 20, 2025
1 parent 7d12b80 commit bd7e1b5
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cliconfig

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cliconfig

import (
"fmt"
Expand Down Expand Up @@ -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_<uppercase_feature_name> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cliconfig

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cliconfig

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package main
package cliconfig

import (
"fmt"

"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())
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clisupport/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/require/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions pkg/csconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand All @@ -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,
}
Expand Down

0 comments on commit bd7e1b5

Please sign in to comment.