Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact cscli - don't export functions if not required #3224

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cmd/crowdsec-cli/clialert/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
)

func DecisionsFromAlert(alert *models.Alert) string {
func decisionsFromAlert(alert *models.Alert) string {
ret := ""
decMap := make(map[string]int)

Expand Down Expand Up @@ -77,7 +77,7 @@ func (cli *cliAlerts) alertsToTable(alerts *models.GetAlertsResponse, printMachi
*alertItem.Scenario,
alertItem.Source.Cn,
alertItem.Source.GetAsNumberName(),
DecisionsFromAlert(alertItem),
decisionsFromAlert(alertItem),
*alertItem.StartAt,
}
if printMachine {
Expand Down Expand Up @@ -227,10 +227,10 @@ func (cli *cliAlerts) NewCommand() *cobra.Command {
},
}

cmd.AddCommand(cli.NewListCmd())
cmd.AddCommand(cli.NewInspectCmd())
cmd.AddCommand(cli.NewFlushCmd())
cmd.AddCommand(cli.NewDeleteCmd())
cmd.AddCommand(cli.newListCmd())
cmd.AddCommand(cli.newInspectCmd())
cmd.AddCommand(cli.newFlushCmd())
cmd.AddCommand(cli.newDeleteCmd())

return cmd
}
Expand Down Expand Up @@ -323,7 +323,7 @@ func (cli *cliAlerts) list(alertListFilter apiclient.AlertsListOpts, limit *int,
return nil
}

func (cli *cliAlerts) NewListCmd() *cobra.Command {
func (cli *cliAlerts) newListCmd() *cobra.Command {
alertListFilter := apiclient.AlertsListOpts{
ScopeEquals: new(string),
ValueEquals: new(string),
Expand Down Expand Up @@ -439,7 +439,7 @@ func (cli *cliAlerts) delete(alertDeleteFilter apiclient.AlertsDeleteOpts, Activ
return nil
}

func (cli *cliAlerts) NewDeleteCmd() *cobra.Command {
func (cli *cliAlerts) newDeleteCmd() *cobra.Command {
var (
ActiveDecision *bool
AlertDeleteAll bool
Expand Down Expand Up @@ -538,7 +538,7 @@ func (cli *cliAlerts) inspect(details bool, alertIDs ...string) error {
return nil
}

func (cli *cliAlerts) NewInspectCmd() *cobra.Command {
func (cli *cliAlerts) newInspectCmd() *cobra.Command {
var details bool

cmd := &cobra.Command{
Expand All @@ -561,7 +561,7 @@ func (cli *cliAlerts) NewInspectCmd() *cobra.Command {
return cmd
}

func (cli *cliAlerts) NewFlushCmd() *cobra.Command {
func (cli *cliAlerts) newFlushCmd() *cobra.Command {
var (
maxItems int
maxAge string
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clialert/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func alertsTable(out io.Writer, wantColor string, alerts *models.GetAlertsRespon
*alertItem.Scenario,
alertItem.Source.Cn,
alertItem.Source.GetAsNumberName(),
DecisionsFromAlert(alertItem),
decisionsFromAlert(alertItem),
*alertItem.StartAt,
}

Expand Down
26 changes: 13 additions & 13 deletions cmd/crowdsec-cli/clihub/item_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
)

func ShowMetrics(prometheusURL string, hubItem *cwhub.Item, wantColor string) error {
func showMetrics(prometheusURL string, hubItem *cwhub.Item, wantColor string) error {
switch hubItem.Type {
case cwhub.PARSERS:
metrics := GetParserMetric(prometheusURL, hubItem.Name)
metrics := getParserMetric(prometheusURL, hubItem.Name)
parserMetricsTable(color.Output, wantColor, hubItem.Name, metrics)
case cwhub.SCENARIOS:
metrics := GetScenarioMetric(prometheusURL, hubItem.Name)
metrics := getScenarioMetric(prometheusURL, hubItem.Name)
scenarioMetricsTable(color.Output, wantColor, hubItem.Name, metrics)
case cwhub.COLLECTIONS:
for _, sub := range hubItem.SubItems() {
if err := ShowMetrics(prometheusURL, sub, wantColor); err != nil {
if err := showMetrics(prometheusURL, sub, wantColor); err != nil {
return err
}
}
case cwhub.APPSEC_RULES:
metrics := GetAppsecRuleMetric(prometheusURL, hubItem.Name)
metrics := getAppsecRuleMetric(prometheusURL, hubItem.Name)

Check warning on line 34 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L34

Added line #L34 was not covered by tests
appsecMetricsTable(color.Output, wantColor, hubItem.Name, metrics)
default: // no metrics for this item type
}

return nil
}

// GetParserMetric is a complete rip from prom2json
func GetParserMetric(url string, itemName string) map[string]map[string]int {
// getParserMetric is a complete rip from prom2json
func getParserMetric(url string, itemName string) map[string]map[string]int {
stats := make(map[string]map[string]int)

result := GetPrometheusMetric(url)
result := getPrometheusMetric(url)
for idx, fam := range result {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
Expand Down Expand Up @@ -131,7 +131,7 @@
return stats
}

func GetScenarioMetric(url string, itemName string) map[string]int {
func getScenarioMetric(url string, itemName string) map[string]int {
stats := make(map[string]int)

stats["instantiation"] = 0
Expand All @@ -140,7 +140,7 @@
stats["pour"] = 0
stats["underflow"] = 0

result := GetPrometheusMetric(url)
result := getPrometheusMetric(url)
for idx, fam := range result {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
Expand Down Expand Up @@ -195,13 +195,13 @@
return stats
}

func GetAppsecRuleMetric(url string, itemName string) map[string]int {
func getAppsecRuleMetric(url string, itemName string) map[string]int {

Check warning on line 198 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L198

Added line #L198 was not covered by tests
stats := make(map[string]int)

stats["inband_hits"] = 0
stats["outband_hits"] = 0

results := GetPrometheusMetric(url)
results := getPrometheusMetric(url)

Check warning on line 204 in cmd/crowdsec-cli/clihub/item_metrics.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clihub/item_metrics.go#L204

Added line #L204 was not covered by tests
for idx, fam := range results {
if !strings.HasPrefix(fam.Name, "cs_") {
continue
Expand Down Expand Up @@ -260,7 +260,7 @@
return stats
}

func GetPrometheusMetric(url string) []*prom2json.Family {
func getPrometheusMetric(url string) []*prom2json.Family {
mfChan := make(chan *dto.MetricFamily, 1024)

// Start with the DefaultTransport for sane defaults.
Expand Down
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/clihub/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func ListItems(out io.Writer, wantColor string, itemTypes []string, items map[st
return nil
}

func InspectItem(item *cwhub.Item, showMetrics bool, output string, prometheusURL string, wantColor string) error {
func InspectItem(item *cwhub.Item, wantMetrics bool, output string, prometheusURL string, wantColor string) error {
switch output {
case "human", "raw":
enc := yaml.NewEncoder(os.Stdout)
Expand Down Expand Up @@ -174,10 +174,10 @@ func InspectItem(item *cwhub.Item, showMetrics bool, output string, prometheusUR
fmt.Println()
}

if showMetrics {
if wantMetrics {
fmt.Printf("\nCurrent metrics: \n")

if err := ShowMetrics(prometheusURL, item, wantColor); err != nil {
if err := showMetrics(prometheusURL, item, wantColor); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

func (cli *cliHubTest) NewCleanCmd() *cobra.Command {
func (cli *cliHubTest) newCleanCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "clean",
Short: "clean [test_name]",
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (cli *cliHubTest) coverage(showScenarioCov bool, showParserCov bool, showAp
return nil
}

func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
func (cli *cliHubTest) newCoverageCmd() *cobra.Command {
var (
showParserCov bool
showScenarioCov bool
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
)

func (cli *cliHubTest) NewCreateCmd() *cobra.Command {
func (cli *cliHubTest) newCreateCmd() *cobra.Command {
var (
ignoreParsers bool
labels map[string]string
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

func (cli *cliHubTest) NewEvalCmd() *cobra.Command {
func (cli *cliHubTest) newEvalCmd() *cobra.Command {
var evalExpression string

cmd := &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (cli *cliHubTest) explain(testName string, details bool, skipOk bool) error
}


func (cli *cliHubTest) NewExplainCmd() *cobra.Command {
func (cli *cliHubTest) newExplainCmd() *cobra.Command {
var (
details bool
skipOk bool
Expand Down
16 changes: 8 additions & 8 deletions cmd/crowdsec-cli/clihubtest/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func (cli *cliHubTest) NewCommand() *cobra.Command {
cmd.PersistentFlags().StringVar(&cscliPath, "cscli", "cscli", "Path to cscli")
cmd.PersistentFlags().BoolVar(&isAppsecTest, "appsec", false, "Command relates to appsec tests")

cmd.AddCommand(cli.NewCreateCmd())
cmd.AddCommand(cli.NewRunCmd())
cmd.AddCommand(cli.NewCleanCmd())
cmd.AddCommand(cli.NewInfoCmd())
cmd.AddCommand(cli.NewListCmd())
cmd.AddCommand(cli.NewCoverageCmd())
cmd.AddCommand(cli.NewEvalCmd())
cmd.AddCommand(cli.NewExplainCmd())
cmd.AddCommand(cli.newCreateCmd())
cmd.AddCommand(cli.newRunCmd())
cmd.AddCommand(cli.newCleanCmd())
cmd.AddCommand(cli.newInfoCmd())
cmd.AddCommand(cli.newListCmd())
cmd.AddCommand(cli.newCoverageCmd())
cmd.AddCommand(cli.newEvalCmd())
cmd.AddCommand(cli.newExplainCmd())

return cmd
}
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
)

func (cli *cliHubTest) NewInfoCmd() *cobra.Command {
func (cli *cliHubTest) newInfoCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "info",
Short: "info [test_name]",
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

func (cli *cliHubTest) NewListCmd() *cobra.Command {
func (cli *cliHubTest) newListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list",
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihubtest/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (cli *cliHubTest) run(runAll bool, NucleiTargetHost string, AppSecHost stri
return nil
}

func (cli *cliHubTest) NewRunCmd() *cobra.Command {
func (cli *cliHubTest) newRunCmd() *cobra.Command {
var (
noClean bool
runAll bool
Expand Down
16 changes: 8 additions & 8 deletions cmd/crowdsec-cli/clinotifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func (cli *cliNotifications) NewCommand() *cobra.Command {
},
}

cmd.AddCommand(cli.NewListCmd())
cmd.AddCommand(cli.NewInspectCmd())
cmd.AddCommand(cli.NewReinjectCmd())
cmd.AddCommand(cli.NewTestCmd())
cmd.AddCommand(cli.newListCmd())
cmd.AddCommand(cli.newInspectCmd())
cmd.AddCommand(cli.newReinjectCmd())
cmd.AddCommand(cli.newTestCmd())

return cmd
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func (cli *cliNotifications) getProfilesConfigs() (map[string]NotificationsCfg,
return ncfgs, nil
}

func (cli *cliNotifications) NewListCmd() *cobra.Command {
func (cli *cliNotifications) newListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list notifications plugins",
Expand Down Expand Up @@ -201,7 +201,7 @@ func (cli *cliNotifications) NewListCmd() *cobra.Command {
return cmd
}

func (cli *cliNotifications) NewInspectCmd() *cobra.Command {
func (cli *cliNotifications) newInspectCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "inspect",
Short: "Inspect notifications plugin",
Expand Down Expand Up @@ -260,7 +260,7 @@ func (cli *cliNotifications) notificationConfigFilter(cmd *cobra.Command, args [
return ret, cobra.ShellCompDirectiveNoFileComp
}

func (cli cliNotifications) NewTestCmd() *cobra.Command {
func (cli cliNotifications) newTestCmd() *cobra.Command {
var (
pluginBroker csplugin.PluginBroker
pluginTomb tomb.Tomb
Expand Down Expand Up @@ -351,7 +351,7 @@ func (cli cliNotifications) NewTestCmd() *cobra.Command {
return cmd
}

func (cli *cliNotifications) NewReinjectCmd() *cobra.Command {
func (cli *cliNotifications) newReinjectCmd() *cobra.Command {
var (
alertOverride string
alert *models.Alert
Expand Down
16 changes: 8 additions & 8 deletions cmd/crowdsec-cli/clisetup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func (cli *cliSetup) NewCommand() *cobra.Command {
DisableAutoGenTag: true,
}

cmd.AddCommand(cli.NewDetectCmd())
cmd.AddCommand(cli.NewInstallHubCmd())
cmd.AddCommand(cli.NewDataSourcesCmd())
cmd.AddCommand(cli.NewValidateCmd())
cmd.AddCommand(cli.newDetectCmd())
cmd.AddCommand(cli.newInstallHubCmd())
cmd.AddCommand(cli.newDataSourcesCmd())
cmd.AddCommand(cli.newValidateCmd())

return cmd
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func (f *detectFlags) bind(cmd *cobra.Command) {
flags.BoolVar(&f.outYaml, "yaml", false, "output yaml, not json")
}

func (cli *cliSetup) NewDetectCmd() *cobra.Command {
func (cli *cliSetup) newDetectCmd() *cobra.Command {
f := detectFlags{}

cmd := &cobra.Command{
Expand All @@ -92,7 +92,7 @@ func (cli *cliSetup) NewDetectCmd() *cobra.Command {
return cmd
}

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

cmd := &cobra.Command{
Expand All @@ -111,7 +111,7 @@ func (cli *cliSetup) NewInstallHubCmd() *cobra.Command {
return cmd
}

func (cli *cliSetup) NewDataSourcesCmd() *cobra.Command {
func (cli *cliSetup) newDataSourcesCmd() *cobra.Command {
var toDir string

cmd := &cobra.Command{
Expand All @@ -130,7 +130,7 @@ func (cli *cliSetup) NewDataSourcesCmd() *cobra.Command {
return cmd
}

func (cli *cliSetup) NewValidateCmd() *cobra.Command {
func (cli *cliSetup) newValidateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "validate [setup_file]",
Short: "validate a setup file",
Expand Down
Loading