Skip to content

Commit

Permalink
pkg/cwhub - refact Downloader (#3382)
Browse files Browse the repository at this point in the history
* pkg/cwhub - refact Downloader
* single method interfaces
* lint
  • Loading branch information
mmetc authored Dec 27, 2024
1 parent fc17c0c commit 78f4b85
Show file tree
Hide file tree
Showing 31 changed files with 307 additions and 273 deletions.
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clicapi/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (cli *cliCapi) newStatusCmd() *cobra.Command {
Args: cobra.MinimumNArgs(0),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, _ []string) error {
hub, err := require.Hub(cli.cfg(), nil, nil)
hub, err := require.Hub(cli.cfg(), nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/cliconsole/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (cli *cliConsole) enroll(ctx context.Context, key string, name string, over
}
}

hub, err := require.Hub(cfg, nil, nil)
hub, err := require.Hub(cfg, nil)
if err != nil {
return err
}
Expand Down
20 changes: 11 additions & 9 deletions cmd/crowdsec-cli/clihub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (cli *cliHub) newListCmd() *cobra.Command {
Args: cobra.NoArgs,
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
hub, err := require.Hub(cli.cfg(), log.StandardLogger())
if err != nil {
return err
}
Expand All @@ -110,15 +110,15 @@ func (cli *cliHub) newListCmd() *cobra.Command {

func (cli *cliHub) update(ctx context.Context, withContent bool) error {
local := cli.cfg().Hub
remote := require.RemoteHub(ctx, cli.cfg())

// don't use require.Hub because if there is no index file, it would fail
hub, err := cwhub.NewHub(local, remote, log.StandardLogger())
hub, err := cwhub.NewHub(local, log.StandardLogger())
if err != nil {
return err
}

if err := hub.Update(ctx, withContent); err != nil {
indexProvider := require.HubDownloader(ctx, cli.cfg())

if err := hub.Update(ctx, indexProvider, withContent); err != nil {
return fmt.Errorf("failed to update hub: %w", err)
}

Expand Down Expand Up @@ -166,16 +166,18 @@ cscli hub update --with-content`,
func (cli *cliHub) upgrade(ctx context.Context, yes bool, dryRun bool, force bool) error {
cfg := cli.cfg()

hub, err := require.Hub(cfg, require.RemoteHub(ctx, cfg), log.StandardLogger())
hub, err := require.Hub(cfg, log.StandardLogger())
if err != nil {
return err
}

plan := hubops.NewActionPlan(hub)

contentProvider := require.HubDownloader(ctx, cfg)

for _, itemType := range cwhub.ItemTypes {
for _, item := range hub.GetInstalledByType(itemType, true) {
plan.AddCommand(hubops.NewDownloadCommand(item, force))
plan.AddCommand(hubops.NewDownloadCommand(item, contentProvider, force))
}
}

Expand All @@ -196,9 +198,9 @@ func (cli *cliHub) upgrade(ctx context.Context, yes bool, dryRun bool, force boo

func (cli *cliHub) newUpgradeCmd() *cobra.Command {
var (
yes bool
yes bool
dryRun bool
force bool
force bool
)

cmd := &cobra.Command{
Expand Down
53 changes: 28 additions & 25 deletions cmd/crowdsec-cli/cliitem/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ func (cli cliItem) NewCommand() *cobra.Command {
func (cli cliItem) install(ctx context.Context, args []string, yes bool, dryRun bool, downloadOnly bool, force bool, ignoreError bool) error {
cfg := cli.cfg()

hub, err := require.Hub(cfg, require.RemoteHub(ctx, cfg), log.StandardLogger())
hub, err := require.Hub(cfg, log.StandardLogger())
if err != nil {
return err
}

plan := hubops.NewActionPlan(hub)

contentProvider := require.HubDownloader(ctx, cfg)

for _, name := range args {
item := hub.GetItem(cli.name, name)
if item == nil {
Expand All @@ -91,7 +93,7 @@ func (cli cliItem) install(ctx context.Context, args []string, yes bool, dryRun
continue
}

if err = plan.AddCommand(hubops.NewDownloadCommand(item, force)); err != nil {
if err = plan.AddCommand(hubops.NewDownloadCommand(item, contentProvider, force)); err != nil {
return err
}

Expand Down Expand Up @@ -121,7 +123,7 @@ func (cli cliItem) install(ctx context.Context, args []string, yes bool, dryRun

func (cli cliItem) newInstallCmd() *cobra.Command {
var (
yes bool
yes bool
dryRun bool
downloadOnly bool
force bool
Expand Down Expand Up @@ -180,6 +182,7 @@ func (cli cliItem) removePlan(hub *cwhub.Hub, args []string, purge bool, force b
if err := plan.AddCommand(hubops.NewDisableCommand(item, force)); err != nil {
return nil, err
}

if purge {
if err := plan.AddCommand(hubops.NewPurgeCommand(item, force)); err != nil {
return nil, err
Expand Down Expand Up @@ -211,24 +214,22 @@ func (cli cliItem) removePlan(hub *cwhub.Hub, args []string, purge bool, force b

if err := plan.AddCommand(hubops.NewDisableCommand(item, force)); err != nil {
return nil, err

}

if purge {
if err := plan.AddCommand(hubops.NewPurgeCommand(item, force)); err != nil {
return nil, err

}
}
}

return plan, nil
}


func (cli cliItem) remove(ctx context.Context, args []string, yes bool, dryRun bool, purge bool, force bool, all bool) error {
cfg := cli.cfg()

hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
hub, err := require.Hub(cli.cfg(), log.StandardLogger())
if err != nil {
return err
}
Expand All @@ -253,7 +254,7 @@ func (cli cliItem) remove(ctx context.Context, args []string, yes bool, dryRun b

func (cli cliItem) newRemoveCmd() *cobra.Command {
var (
yes bool
yes bool
dryRun bool
purge bool
force bool
Expand Down Expand Up @@ -290,12 +291,12 @@ func (cli cliItem) newRemoveCmd() *cobra.Command {
return cmd
}

func (cli cliItem) upgradePlan(hub *cwhub.Hub, args []string, force bool, all bool) (*hubops.ActionPlan, error) {
func (cli cliItem) upgradePlan(hub *cwhub.Hub, contentProvider cwhub.ContentProvider, args []string, force bool, all bool) (*hubops.ActionPlan, error) {
plan := hubops.NewActionPlan(hub)

if all {
for _, item := range hub.GetInstalledByType(cli.name, true) {
if err := plan.AddCommand(hubops.NewDownloadCommand(item, force)); err != nil {
if err := plan.AddCommand(hubops.NewDownloadCommand(item, contentProvider, force)); err != nil {
return nil, err
}
}
Expand All @@ -313,7 +314,7 @@ func (cli cliItem) upgradePlan(hub *cwhub.Hub, args []string, force bool, all bo
return nil, fmt.Errorf("can't find '%s' in %s", itemName, cli.name)
}

if err := plan.AddCommand(hubops.NewDownloadCommand(item, force)); err != nil {
if err := plan.AddCommand(hubops.NewDownloadCommand(item, contentProvider, force)); err != nil {
return nil, err
}
}
Expand All @@ -324,12 +325,14 @@ func (cli cliItem) upgradePlan(hub *cwhub.Hub, args []string, force bool, all bo
func (cli cliItem) upgrade(ctx context.Context, args []string, yes bool, dryRun bool, force bool, all bool) error {
cfg := cli.cfg()

hub, err := require.Hub(cfg, require.RemoteHub(ctx, cfg), log.StandardLogger())
hub, err := require.Hub(cfg, log.StandardLogger())
if err != nil {
return err
}

plan, err := cli.upgradePlan(hub, args, force, all)
contentProvider := require.HubDownloader(ctx, cfg)

plan, err := cli.upgradePlan(hub, contentProvider, args, force, all)
if err != nil {
return err
}
Expand All @@ -349,10 +352,10 @@ func (cli cliItem) upgrade(ctx context.Context, args []string, yes bool, dryRun

func (cli cliItem) newUpgradeCmd() *cobra.Command {
var (
yes bool
yes bool
dryRun bool
all bool
force bool
all bool
force bool
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -390,13 +393,13 @@ func (cli cliItem) inspect(ctx context.Context, args []string, url string, diff
cfg.Cscli.PrometheusUrl = url
}

remote := (*cwhub.RemoteHubCfg)(nil)
var contentProvider cwhub.ContentProvider

if diff {
remote = require.RemoteHub(ctx, cfg)
contentProvider = require.HubDownloader(ctx, cfg)
}

hub, err := require.Hub(cfg, remote, log.StandardLogger())
hub, err := require.Hub(cfg, log.StandardLogger())
if err != nil {
return err
}
Expand All @@ -408,7 +411,7 @@ func (cli cliItem) inspect(ctx context.Context, args []string, url string, diff
}

if diff {
fmt.Println(cli.whyTainted(ctx, hub, item, rev))
fmt.Println(cli.whyTainted(ctx, hub, contentProvider, item, rev))

continue
}
Expand Down Expand Up @@ -462,7 +465,7 @@ func (cli cliItem) newInspectCmd() *cobra.Command {
func (cli cliItem) list(args []string, all bool) error {
cfg := cli.cfg()

hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
hub, err := require.Hub(cli.cfg(), log.StandardLogger())
if err != nil {
return err
}
Expand Down Expand Up @@ -498,7 +501,7 @@ func (cli cliItem) newListCmd() *cobra.Command {
}

// return the diff between the installed version and the latest version
func (cli cliItem) itemDiff(ctx context.Context, item *cwhub.Item, reverse bool) (string, error) {
func (cli cliItem) itemDiff(ctx context.Context, item *cwhub.Item, contentProvider cwhub.ContentProvider, reverse bool) (string, error) {
if !item.State.Installed {
return "", fmt.Errorf("'%s' is not installed", item.FQName())
}
Expand All @@ -509,7 +512,7 @@ func (cli cliItem) itemDiff(ctx context.Context, item *cwhub.Item, reverse bool)
}
defer os.Remove(dest.Name())

_, remoteURL, err := item.FetchContentTo(ctx, dest.Name())
_, remoteURL, err := item.FetchContentTo(ctx, contentProvider, dest.Name())
if err != nil {
return "", err
}
Expand Down Expand Up @@ -540,7 +543,7 @@ func (cli cliItem) itemDiff(ctx context.Context, item *cwhub.Item, reverse bool)
return fmt.Sprintf("%s", diff), nil
}

func (cli cliItem) whyTainted(ctx context.Context, hub *cwhub.Hub, item *cwhub.Item, reverse bool) string {
func (cli cliItem) whyTainted(ctx context.Context, hub *cwhub.Hub, contentProvider cwhub.ContentProvider, item *cwhub.Item, reverse bool) string {
if !item.State.Installed {
return fmt.Sprintf("# %s is not installed", item.FQName())
}
Expand All @@ -565,7 +568,7 @@ func (cli cliItem) whyTainted(ctx context.Context, hub *cwhub.Hub, item *cwhub.I
ret = append(ret, err.Error())
}

diff, err := cli.itemDiff(ctx, sub, reverse)
diff, err := cli.itemDiff(ctx, sub, contentProvider, reverse)
if err != nil {
ret = append(ret, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/cliitem/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func suggestNearestMessage(hub *cwhub.Hub, itemType string, itemName string) str
}

func compAllItems(itemType string, args []string, toComplete string, cfg configGetter) ([]string, cobra.ShellCompDirective) {
hub, err := require.Hub(cfg(), nil, nil)
hub, err := require.Hub(cfg(), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}
Expand All @@ -56,7 +56,7 @@ func compAllItems(itemType string, args []string, toComplete string, cfg configG
}

func compInstalledItems(itemType string, args []string, toComplete string, cfg configGetter) ([]string, cobra.ShellCompDirective) {
hub, err := require.Hub(cfg(), nil, nil)
hub, err := require.Hub(cfg(), nil)
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/clilapi/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
`,
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
hub, err := require.Hub(cli.cfg(), nil, nil)
hub, err := require.Hub(cli.cfg(), nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (cli *cliLapi) newContextStatusCmd() *cobra.Command {
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
cfg := cli.cfg()
hub, err := require.Hub(cfg, nil, nil)
hub, err := require.Hub(cfg, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,7 +153,7 @@ cscli lapi context detect crowdsecurity/sshd-logs
return fmt.Errorf("failed to init expr helpers: %w", err)
}

hub, err := require.Hub(cfg, nil, nil)
hub, err := require.Hub(cfg, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clilapi/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (cli *cliLapi) newStatusCmd() *cobra.Command {
Args: cobra.MinimumNArgs(0),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, _ []string) error {
hub, err := require.Hub(cli.cfg(), nil, nil)
hub, err := require.Hub(cli.cfg(), nil)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/crowdsec-cli/clisetup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (cli *cliSetup) newDetectCmd() *cobra.Command {

func (cli *cliSetup) newInstallHubCmd() *cobra.Command {
var (
yes bool
yes bool
dryRun bool
)

Expand Down Expand Up @@ -289,14 +289,16 @@ func (cli *cliSetup) install(ctx context.Context, yes bool, dryRun bool, fromFil

cfg := cli.cfg()

hub, err := require.Hub(cfg, require.RemoteHub(ctx, cfg), log.StandardLogger())
hub, err := require.Hub(cfg, log.StandardLogger())
if err != nil {
return err
}

verbose := (cfg.Cscli.Output == "raw")

return setup.InstallHubItems(ctx, hub, input, yes, dryRun, verbose)
contentProvider := require.HubDownloader(ctx, cfg)

return setup.InstallHubItems(ctx, hub, contentProvider, input, yes, dryRun, verbose)
}

func (cli *cliSetup) validate(fromFile string) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clisimulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (cli *cliSimulation) newEnableCmd() *cobra.Command {
Example: `cscli simulation enable`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
hub, err := require.Hub(cli.cfg(), nil, nil)
hub, err := require.Hub(cli.cfg(), nil)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clisupport/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,9 @@ func (cli *cliSupport) dump(ctx context.Context, outFile string) error {
skipAgent = true
}

hub, err := require.Hub(cfg, nil, nil)
hub, err := require.Hub(cfg, nil)
if err != nil {
log.Warn("Could not init hub, running on LAPI ? Hub related information will not be collected")
log.Warn("Could not init hub, running on LAPI? Hub related information will not be collected")
// XXX: lapi status check requires scenarios, will return an error
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/config_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func (cli *cliConfig) backupHub(dirPath string) error {
hub, err := require.Hub(cli.cfg(), nil, nil)
hub, err := require.Hub(cli.cfg(), nil)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 78f4b85

Please sign in to comment.