Skip to content

Commit

Permalink
cscli refact: package 'cliconsole' (#3149)
Browse files Browse the repository at this point in the history
* cscli refact: package 'cliconsole'

* dry

* lint

* lint
  • Loading branch information
mmetc authored Aug 20, 2024
1 parent e7b54c6 commit 08fdfc4
Show file tree
Hide file tree
Showing 19 changed files with 90 additions and 106 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ linters-settings:
disable:
- reflectvaluecompare
- fieldalignment
- printf

maintidx:
# raise this after refactoring
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error {
fmt.Println(string(apiConfigDump))
}

log.Warning(ReloadMessage())
log.Warning(reloadMessage)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cliconsole

import (
"context"
Expand Down Expand Up @@ -28,13 +28,17 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
)

type configGetter func() *csconfig.Config

type cliConsole struct {
cfg configGetter
cfg func() *csconfig.Config
reloadMessage string
}

func NewCLIConsole(cfg configGetter) *cliConsole {
func New(cfg configGetter, reloadMessage string) *cliConsole {
return &cliConsole{
cfg: cfg,
cfg: cfg,
reloadMessage: reloadMessage,
}
}

Expand Down Expand Up @@ -221,7 +225,7 @@ Enable given information push to the central API. Allows to empower the console`
log.Infof("%v have been enabled", args)
}

log.Infof(ReloadMessage())
log.Info(cli.reloadMessage)

return nil
},
Expand Down Expand Up @@ -255,7 +259,7 @@ Disable given information push to the central API.`,
log.Infof("%v have been disabled", args)
}

log.Infof(ReloadMessage())
log.Info(cli.reloadMessage)

return nil
},
Expand Down Expand Up @@ -348,13 +352,8 @@ func (cli *cliConsole) setConsoleOpts(args []string, wanted bool) error {
switch arg {
case csconfig.CONSOLE_MANAGEMENT:
/*for each flag check if it's already set before setting it*/
if consoleCfg.ConsoleManagement != nil {
if *consoleCfg.ConsoleManagement == wanted {
log.Debugf("%s already set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
} else {
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
*consoleCfg.ConsoleManagement = wanted
}
if consoleCfg.ConsoleManagement != nil && *consoleCfg.ConsoleManagement == wanted {
log.Debugf("%s already set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
} else {
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
consoleCfg.ConsoleManagement = ptr.Of(wanted)
Expand Down Expand Up @@ -386,52 +385,32 @@ func (cli *cliConsole) setConsoleOpts(args []string, wanted bool) error {
}
case csconfig.SEND_CUSTOM_SCENARIOS:
/*for each flag check if it's already set before setting it*/
if consoleCfg.ShareCustomScenarios != nil {
if *consoleCfg.ShareCustomScenarios == wanted {
log.Debugf("%s already set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
} else {
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
*consoleCfg.ShareCustomScenarios = wanted
}
if consoleCfg.ShareCustomScenarios != nil && *consoleCfg.ShareCustomScenarios == wanted {
log.Debugf("%s already set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
} else {
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
consoleCfg.ShareCustomScenarios = ptr.Of(wanted)
}
case csconfig.SEND_TAINTED_SCENARIOS:
/*for each flag check if it's already set before setting it*/
if consoleCfg.ShareTaintedScenarios != nil {
if *consoleCfg.ShareTaintedScenarios == wanted {
log.Debugf("%s already set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
} else {
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
*consoleCfg.ShareTaintedScenarios = wanted
}
if consoleCfg.ShareTaintedScenarios != nil && *consoleCfg.ShareTaintedScenarios == wanted {
log.Debugf("%s already set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
} else {
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
consoleCfg.ShareTaintedScenarios = ptr.Of(wanted)
}
case csconfig.SEND_MANUAL_SCENARIOS:
/*for each flag check if it's already set before setting it*/
if consoleCfg.ShareManualDecisions != nil {
if *consoleCfg.ShareManualDecisions == wanted {
log.Debugf("%s already set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
} else {
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
*consoleCfg.ShareManualDecisions = wanted
}
if consoleCfg.ShareManualDecisions != nil && *consoleCfg.ShareManualDecisions == wanted {
log.Debugf("%s already set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
} else {
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
consoleCfg.ShareManualDecisions = ptr.Of(wanted)
}
case csconfig.SEND_CONTEXT:
/*for each flag check if it's already set before setting it*/
if consoleCfg.ShareContext != nil {
if *consoleCfg.ShareContext == wanted {
log.Debugf("%s already set to %t", csconfig.SEND_CONTEXT, wanted)
} else {
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
*consoleCfg.ShareContext = wanted
}
if consoleCfg.ShareContext != nil && *consoleCfg.ShareContext == wanted {
log.Debugf("%s already set to %t", csconfig.SEND_CONTEXT, wanted)
} else {
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
consoleCfg.ShareContext = ptr.Of(wanted)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cliconsole

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/climetrics/statbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (*statBouncer) Description() (string, string) {

func logWarningOnce(warningsLogged map[string]bool, msg string) {
if _, ok := warningsLogged[msg]; !ok {
log.Warningf(msg)
log.Warning(msg)

warningsLogged[msg] = true
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/crowdsec-cli/itemcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cli cliItem) install(ctx context.Context, args []string, downloadOnly bool
return errors.New(msg)
}

log.Errorf(msg)
log.Error(msg)

continue
}
Expand All @@ -92,7 +92,7 @@ func (cli cliItem) install(ctx context.Context, args []string, downloadOnly bool
}
}

log.Infof(ReloadMessage())
log.Info(reloadMessage)

return nil
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error
log.Infof("Removed %d %s", removed, cli.name)

if removed > 0 {
log.Infof(ReloadMessage())
log.Info(reloadMessage)
}

return nil
Expand Down Expand Up @@ -217,7 +217,7 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error
log.Infof("Removed %d %s", removed, cli.name)

if removed > 0 {
log.Infof(ReloadMessage())
log.Info(reloadMessage)
}

return nil
Expand Down Expand Up @@ -283,7 +283,7 @@ func (cli cliItem) upgrade(ctx context.Context, args []string, force bool, all b
log.Infof("Updated %d %s", updated, cli.name)

if updated > 0 {
log.Infof(ReloadMessage())
log.Info(reloadMessage)
}

return nil
Expand Down Expand Up @@ -314,7 +314,7 @@ func (cli cliItem) upgrade(ctx context.Context, args []string, force bool, all b
}

if updated > 0 {
log.Infof(ReloadMessage())
log.Info(reloadMessage)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/lapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string) e
fmt.Printf("%s\n", string(apiConfigDump))
}

log.Warning(ReloadMessage())
log.Warning(reloadMessage)

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/crowdsecurity/go-cs-lib/trace"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cliconsole"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/climetrics"

"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/fflag"
)
Expand Down Expand Up @@ -262,7 +262,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
cmd.AddCommand(NewCLICapi(cli.cfg).NewCommand())
cmd.AddCommand(NewCLILapi(cli.cfg).NewCommand())
cmd.AddCommand(NewCompletionCmd())
cmd.AddCommand(NewCLIConsole(cli.cfg).NewCommand())
cmd.AddCommand(cliconsole.New(cli.cfg, reloadMessage).NewCommand())
cmd.AddCommand(NewCLIExplain(cli.cfg).NewCommand())
cmd.AddCommand(NewCLIHubTest(cli.cfg).NewCommand())
cmd.AddCommand(NewCLINotifications(cli.cfg).NewCommand())
Expand Down
23 changes: 0 additions & 23 deletions cmd/crowdsec-cli/messages.go

This file was deleted.

6 changes: 6 additions & 0 deletions cmd/crowdsec-cli/reload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !windows && !freebsd && !linux

package main

// generic message since we don't know the platform
const reloadMessage = "Please reload the crowdsec process for the new configuration to be effective."
4 changes: 4 additions & 0 deletions cmd/crowdsec-cli/reload_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// actually sudo is not that popular on freebsd, but this will do
const reloadMessage = "Run 'sudo service crowdsec reload' for the new configuration to be effective."
4 changes: 4 additions & 0 deletions cmd/crowdsec-cli/reload_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// assume systemd, although gentoo and others may differ
const reloadMessage = "Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
3 changes: 3 additions & 0 deletions cmd/crowdsec-cli/reload_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

const reloadMessage = "Please restart the crowdsec service for the new configuration to be effective."
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cscli simulation disable crowdsecurity/ssh-bf`,
},
PersistentPostRun: func(cmd *cobra.Command, _ []string) {
if cmd.Name() != "status" {
log.Infof(ReloadMessage())
log.Info(reloadMessage)
}
},
}
Expand Down
Loading

0 comments on commit 08fdfc4

Please sign in to comment.