Skip to content

Commit

Permalink
cscli hub: avoid global csConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 31, 2024
1 parent 4192af3 commit 70627fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions cmd/crowdsec-cli/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import (
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
)

type cliHub struct{}
type cliHub struct{
cfg func() *csconfig.Config
}

func NewCLIHub() *cliHub {
return &cliHub{}
func NewCLIHub(getconfig func() *csconfig.Config) *cliHub {
return &cliHub{
cfg: getconfig,
}
}

func (cli cliHub) NewCommand() *cobra.Command {
Expand Down Expand Up @@ -50,7 +55,7 @@ func (cli cliHub) list(cmd *cobra.Command, args []string) error {
return err
}

hub, err := require.Hub(csConfig, nil, log.StandardLogger())
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
if err != nil {
return err
}
Expand Down Expand Up @@ -96,8 +101,8 @@ func (cli cliHub) NewListCmd() *cobra.Command {
}

func (cli cliHub) update(cmd *cobra.Command, args []string) error {
local := csConfig.Hub
remote := require.RemoteHub(csConfig)
local := cli.cfg().Hub
remote := require.RemoteHub(cli.cfg())

// don't use require.Hub because if there is no index file, it would fail
hub, err := cwhub.NewHub(local, remote, true, log.StandardLogger())
Expand Down Expand Up @@ -135,7 +140,7 @@ func (cli cliHub) upgrade(cmd *cobra.Command, args []string) error {
return err
}

hub, err := require.Hub(csConfig, require.RemoteHub(csConfig), log.StandardLogger())
hub, err := require.Hub(cli.cfg(), require.RemoteHub(cli.cfg()), log.StandardLogger())
if err != nil {
return err
}
Expand Down Expand Up @@ -186,7 +191,7 @@ Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if
}

func (cli cliHub) types(cmd *cobra.Command, args []string) error {
switch csConfig.Cscli.Output {
switch cli.cfg().Cscli.Output {
case "human":
s, err := yaml.Marshal(cwhub.ItemTypes)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,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(NewConfigCmd())
cmd.AddCommand(NewCLIHub().NewCommand())
cmd.AddCommand(NewCLIHub(getconfig).NewCommand())
cmd.AddCommand(NewMetricsCmd())
cmd.AddCommand(NewCLIDashboard().NewCommand())
cmd.AddCommand(NewCLIDecisions().NewCommand())
Expand Down

0 comments on commit 70627fd

Please sign in to comment.