Skip to content

Commit

Permalink
cscli: context.Background() -> cmd.Context()
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Sep 24, 2024
1 parent 412de74 commit 132d18c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ linters-settings:

maintidx:
# raise this after refactoring
under: 16
under: 15

misspell:
locale: US
Expand Down
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/clialert/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (cli *cliAlerts) NewCommand() *cobra.Command {
return cmd
}

func (cli *cliAlerts) list(alertListFilter apiclient.AlertsListOpts, limit *int, contained *bool, printMachine bool) error {
func (cli *cliAlerts) list(ctx context.Context, alertListFilter apiclient.AlertsListOpts, limit *int, contained *bool, printMachine bool) error {
var err error

*alertListFilter.ScopeEquals, err = SanitizeScope(*alertListFilter.ScopeEquals, *alertListFilter.IPEquals, *alertListFilter.RangeEquals)
Expand Down Expand Up @@ -311,7 +311,7 @@ func (cli *cliAlerts) list(alertListFilter apiclient.AlertsListOpts, limit *int,
alertListFilter.Contains = new(bool)
}

alerts, _, err := cli.client.Alerts.List(context.Background(), alertListFilter)
alerts, _, err := cli.client.Alerts.List(ctx, alertListFilter)
if err != nil {
return fmt.Errorf("unable to list alerts: %w", err)
}
Expand Down Expand Up @@ -354,7 +354,7 @@ cscli alerts list --type ban`,
Long: `List alerts with optional filters`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.list(alertListFilter, limit, contained, printMachine)
return cli.list(cmd.Context(), alertListFilter, limit, contained, printMachine)
},
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/crowdsec-cli/cliconsole/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (cli *cliConsole) NewCommand() *cobra.Command {
return cmd
}

func (cli *cliConsole) enroll(key string, name string, overwrite bool, tags []string, opts []string) error {
func (cli *cliConsole) enroll(ctx context.Context, key string, name string, overwrite bool, tags []string, opts []string) error {

Check warning on line 69 in cmd/crowdsec-cli/cliconsole/console.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/cliconsole/console.go#L69

Added line #L69 was not covered by tests
cfg := cli.cfg()
password := strfmt.Password(cfg.API.Server.OnlineClient.Credentials.Password)

Expand Down Expand Up @@ -127,7 +127,7 @@ func (cli *cliConsole) enroll(key string, name string, overwrite bool, tags []st
VersionPrefix: "v3",
})

resp, err := c.Auth.EnrollWatcher(context.Background(), key, name, tags, overwrite)
resp, err := c.Auth.EnrollWatcher(ctx, key, name, tags, overwrite)

Check warning on line 130 in cmd/crowdsec-cli/cliconsole/console.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/cliconsole/console.go#L130

Added line #L130 was not covered by tests
if err != nil {
return fmt.Errorf("could not enroll instance: %w", err)
}
Expand Down Expand Up @@ -173,8 +173,8 @@ After running this command your will need to validate the enrollment in the weba
valid options are : %s,all (see 'cscli console status' for details)`, strings.Join(csconfig.CONSOLE_CONFIGS, ",")),
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, args []string) error {
return cli.enroll(args[0], name, overwrite, tags, opts)
RunE: func(cmd *cobra.Command, args []string) error {
return cli.enroll(cmd.Context(), args[0], name, overwrite, tags, opts)

Check warning on line 177 in cmd/crowdsec-cli/cliconsole/console.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/cliconsole/console.go#L177

Added line #L177 was not covered by tests
},
}

Expand Down
22 changes: 11 additions & 11 deletions cmd/crowdsec-cli/clidecision/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (cli *cliDecisions) NewCommand() *cobra.Command {
return cmd
}

func (cli *cliDecisions) list(filter apiclient.AlertsListOpts, NoSimu *bool, contained *bool, printMachine bool) error {
func (cli *cliDecisions) list(ctx context.Context, filter apiclient.AlertsListOpts, NoSimu *bool, contained *bool, printMachine bool) error {
var err error

*filter.ScopeEquals, err = clialert.SanitizeScope(*filter.ScopeEquals, *filter.IPEquals, *filter.RangeEquals)
Expand Down Expand Up @@ -249,7 +249,7 @@ func (cli *cliDecisions) list(filter apiclient.AlertsListOpts, NoSimu *bool, con
filter.Contains = new(bool)
}

alerts, _, err := cli.client.Alerts.List(context.Background(), filter)
alerts, _, err := cli.client.Alerts.List(ctx, filter)
if err != nil {
return fmt.Errorf("unable to retrieve decisions: %w", err)
}
Expand Down Expand Up @@ -293,7 +293,7 @@ cscli decisions list --origin lists --scenario list_name
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.list(filter, NoSimu, contained, printMachine)
return cli.list(cmd.Context(), filter, NoSimu, contained, printMachine)
},
}

Expand All @@ -317,7 +317,7 @@ cscli decisions list --origin lists --scenario list_name
return cmd
}

func (cli *cliDecisions) add(addIP, addRange, addDuration, addValue, addScope, addReason, addType string) error {
func (cli *cliDecisions) add(ctx context.Context, addIP, addRange, addDuration, addValue, addScope, addReason, addType string) error {
alerts := models.AddAlertsRequest{}
origin := types.CscliOrigin
capacity := int32(0)
Expand Down Expand Up @@ -386,7 +386,7 @@ func (cli *cliDecisions) add(addIP, addRange, addDuration, addValue, addScope, a
}
alerts = append(alerts, &alert)

_, _, err = cli.client.Alerts.Add(context.Background(), alerts)
_, _, err = cli.client.Alerts.Add(ctx, alerts)
if err != nil {
return err
}
Expand Down Expand Up @@ -419,7 +419,7 @@ cscli decisions add --scope username --value foobar
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.add(addIP, addRange, addDuration, addValue, addScope, addReason, addType)
return cli.add(cmd.Context(), addIP, addRange, addDuration, addValue, addScope, addReason, addType)
},
}

Expand All @@ -436,7 +436,7 @@ cscli decisions add --scope username --value foobar
return cmd
}

func (cli *cliDecisions) delete(delFilter apiclient.DecisionsDeleteOpts, delDecisionID string, contained *bool) error {
func (cli *cliDecisions) delete(ctx context.Context, delFilter apiclient.DecisionsDeleteOpts, delDecisionID string, contained *bool) error {
var err error

/*take care of shorthand options*/
Expand Down Expand Up @@ -480,7 +480,7 @@ func (cli *cliDecisions) delete(delFilter apiclient.DecisionsDeleteOpts, delDeci
var decisions *models.DeleteDecisionResponse

if delDecisionID == "" {
decisions, _, err = cli.client.Decisions.Delete(context.Background(), delFilter)
decisions, _, err = cli.client.Decisions.Delete(ctx, delFilter)
if err != nil {
return fmt.Errorf("unable to delete decisions: %w", err)
}
Expand All @@ -489,7 +489,7 @@ func (cli *cliDecisions) delete(delFilter apiclient.DecisionsDeleteOpts, delDeci
return fmt.Errorf("id '%s' is not an integer: %w", delDecisionID, err)
}

decisions, _, err = cli.client.Decisions.DeleteOne(context.Background(), delDecisionID)
decisions, _, err = cli.client.Decisions.DeleteOne(ctx, delDecisionID)

Check warning on line 492 in cmd/crowdsec-cli/clidecision/decisions.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clidecision/decisions.go#L492

Added line #L492 was not covered by tests
if err != nil {
return fmt.Errorf("unable to delete decision: %w", err)
}
Expand Down Expand Up @@ -543,8 +543,8 @@ cscli decisions delete --origin lists --scenario list_name

return nil
},
RunE: func(_ *cobra.Command, _ []string) error {
return cli.delete(delFilter, delDecisionID, contained)
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.delete(cmd.Context(), delFilter, delDecisionID, contained)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clilapi/lapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func queryLAPIStatus(ctx context.Context, hub *cwhub.Hub, credURL string, login
Scenarios: itemsForAPI,
}

_, _, err = client.Auth.AuthenticateWatcher(context.Background(), t)
_, _, err = client.Auth.AuthenticateWatcher(ctx, t)
if err != nil {
return false, err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/crowdsec-cli/clinotifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ cscli notifications reinject <alert_id> -a '{"remediation": true,"scenario":"not
`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
PreRunE: func(_ *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error
alert, err = cli.fetchAlertFromArgString(args[0])
alert, err = cli.fetchAlertFromArgString(cmd.Context(), args[0])

Check warning on line 373 in cmd/crowdsec-cli/clinotifications/notifications.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clinotifications/notifications.go#L373

Added line #L373 was not covered by tests
if err != nil {
return err
}
Expand Down Expand Up @@ -447,7 +447,7 @@ cscli notifications reinject <alert_id> -a '{"remediation": true,"scenario":"not
return cmd
}

func (cli *cliNotifications) fetchAlertFromArgString(toParse string) (*models.Alert, error) {
func (cli *cliNotifications) fetchAlertFromArgString(ctx context.Context, toParse string) (*models.Alert, error) {

Check warning on line 450 in cmd/crowdsec-cli/clinotifications/notifications.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clinotifications/notifications.go#L450

Added line #L450 was not covered by tests
cfg := cli.cfg()

id, err := strconv.Atoi(toParse)
Expand All @@ -470,7 +470,7 @@ func (cli *cliNotifications) fetchAlertFromArgString(toParse string) (*models.Al
return nil, fmt.Errorf("error creating the client for the API: %w", err)
}

alert, _, err := client.Alerts.GetByID(context.Background(), id)
alert, _, err := client.Alerts.GetByID(ctx, id)

Check warning on line 473 in cmd/crowdsec-cli/clinotifications/notifications.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clinotifications/notifications.go#L473

Added line #L473 was not covered by tests
if err != nil {
return nil, fmt.Errorf("can't find alert with id %d: %w", id, err)
}
Expand Down

0 comments on commit 132d18c

Please sign in to comment.