Skip to content

Commit

Permalink
refact cscli alerts: avoid global
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Feb 9, 2024
1 parent df159b0 commit 0fbf09c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
38 changes: 21 additions & 17 deletions cmd/crowdsec-cli/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func DecisionsFromAlert(alert *models.Alert) string {
return ret
}

func alertsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
switch csConfig.Cscli.Output {
func (cli *cliAlerts) alertsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
switch cli.cfg().Cscli.Output {
case "raw":
csvwriter := csv.NewWriter(os.Stdout)
header := []string{"id", "scope", "value", "reason", "country", "as", "decisions", "created_at"}
Expand Down Expand Up @@ -116,7 +116,7 @@ var alertTemplate = `
`

func displayOneAlert(alert *models.Alert, withDetail bool) error {
func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) error {
tmpl, err := template.New("alert").Parse(alertTemplate)
if err != nil {
return err
Expand Down Expand Up @@ -163,10 +163,13 @@ func displayOneAlert(alert *models.Alert, withDetail bool) error {

type cliAlerts struct{
client *apiclient.ApiClient
cfg configGetter
}

func NewCLIAlerts() *cliAlerts {
return &cliAlerts{}
func NewCLIAlerts(getconfig configGetter) *cliAlerts {
return &cliAlerts{
cfg: getconfig,
}
}

func (cli *cliAlerts) NewCommand() *cobra.Command {
Expand All @@ -176,18 +179,18 @@ func (cli *cliAlerts) NewCommand() *cobra.Command {
Args: cobra.MinimumNArgs(1),
DisableAutoGenTag: true,
Aliases: []string{"alert"},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
if err := csConfig.LoadAPIClient(); err != nil {
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
cfg := cli.cfg()
if err := cfg.LoadAPIClient(); err != nil {
return fmt.Errorf("loading api client: %w", err)
}
apiURL, err := url.Parse(csConfig.API.Client.Credentials.URL)
apiURL, err := url.Parse(cfg.API.Client.Credentials.URL)
if err != nil {
return fmt.Errorf("parsing api url %s: %w", apiURL, err)
}
cli.client, err = apiclient.NewClient(&apiclient.Config{
MachineID: csConfig.API.Client.Credentials.Login,
Password: strfmt.Password(csConfig.API.Client.Credentials.Password),
MachineID: cfg.API.Client.Credentials.Login,
Password: strfmt.Password(cfg.API.Client.Credentials.Password),
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
URL: apiURL,
VersionPrefix: "v1",
Expand Down Expand Up @@ -307,7 +310,7 @@ cscli alerts list --type ban`,
return fmt.Errorf("unable to list alerts: %v", err)
}

err = alertsToTable(alerts, printMachine)
err = cli.alertsToTable(alerts, printMachine)
if err != nil {
return fmt.Errorf("unable to list alerts: %v", err)
}
Expand Down Expand Up @@ -443,6 +446,7 @@ func (cli *cliAlerts) NewInspectCmd() *cobra.Command {
Example: `cscli alerts inspect 123`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := cli.cfg()
if len(args) == 0 {
printHelp(cmd)
return fmt.Errorf("missing alert_id")
Expand All @@ -456,9 +460,9 @@ func (cli *cliAlerts) NewInspectCmd() *cobra.Command {
if err != nil {
return fmt.Errorf("can't find alert with id %s: %s", alertID, err)
}
switch csConfig.Cscli.Output {
switch cfg.Cscli.Output {
case "human":
if err := displayOneAlert(alert, details); err != nil {
if err := cli.displayOneAlert(alert, details); err != nil {
continue
}
case "json":
Expand Down Expand Up @@ -495,11 +499,11 @@ func (cli *cliAlerts) NewFlushCmd() *cobra.Command {
Example: `cscli alerts flush --max-items 1000 --max-age 7d`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
if err := require.LAPI(csConfig); err != nil {
cfg := cli.cfg()
if err := require.LAPI(cfg); err != nil {

Check warning on line 503 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L502-L503

Added lines #L502 - L503 were not covered by tests
return err
}
db, err := database.NewClient(csConfig.DbConfig)
db, err := database.NewClient(cfg.DbConfig)

Check warning on line 506 in cmd/crowdsec-cli/alerts.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L506

Added line #L506 was not covered by tests
if err != nil {
return fmt.Errorf("unable to create new database client: %s", err)
}
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 @@ -236,7 +236,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
cmd.AddCommand(NewCLIMetrics(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIDashboard(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIDecisions(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIAlerts().NewCommand())
cmd.AddCommand(NewCLIAlerts(cli.cfg).NewCommand())
cmd.AddCommand(NewCLISimulation(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIBouncers(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIMachines(cli.cfg).NewCommand())
Expand Down

0 comments on commit 0fbf09c

Please sign in to comment.