Skip to content

Commit

Permalink
cscli: restyle table titles; autocomplete "cscli metrics show" (#3391)
Browse files Browse the repository at this point in the history
* cscli: restyle table titles; autocomplete "cscli metrics show"
* lint
  • Loading branch information
mmetc authored Jan 2, 2025
1 parent 90f7c56 commit 5c7b957
Show file tree
Hide file tree
Showing 38 changed files with 185 additions and 130 deletions.
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clialert/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func alertDecisionsTable(out io.Writer, wantColor string, alert *models.Alert) {
}

if foundActive {
fmt.Printf(" - Active Decisions :\n")
t.Writer.SetTitle("Active Decisions")
t.Render() // Send output
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clibouncer/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (cli *cliBouncers) inspectHuman(out io.Writer, bouncer *ent.Bouncer) {
t.AppendRow(table.Row{"Feature Flags", ff})
}

io.WriteString(out, t.Render()+"\n")
fmt.Fprint(out, t.Render())
}

func (cli *cliBouncers) inspect(bouncer *ent.Bouncer) error {
Expand Down
3 changes: 1 addition & 2 deletions cmd/crowdsec-cli/clibouncer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (cli *cliBouncers) listHuman(out io.Writer, bouncers ent.Bouncers) {
t.AppendRow(table.Row{b.Name, b.IPAddress, revoked, lastPull, b.Type, b.Version, b.AuthType})
}

io.WriteString(out, t.Render()+"\n")
fmt.Fprintln(out, t.Render())
}

func (cli *cliBouncers) listCSV(out io.Writer, bouncers ent.Bouncers) error {
Expand Down Expand Up @@ -71,7 +71,6 @@ func (cli *cliBouncers) listCSV(out io.Writer, bouncers ent.Bouncers) error {
func (cli *cliBouncers) List(ctx context.Context, out io.Writer, db *database.Client) error {
// XXX: must use the provided db object, the one in the struct might be nil
// (calling List directly skips the PersistentPreRunE)

bouncers, err := db.ListBouncers(ctx)
if err != nil {
return fmt.Errorf("unable to list bouncers: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/clihub/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func ListItems(out io.Writer, wantColor string, itemTypes []string, items map[st
return fmt.Errorf("failed to parse: %w", err)
}

out.Write(x)
fmt.Fprint(out, string(x))
case "raw":
csvwriter := csv.NewWriter(out)

Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clihub/utils_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ func listHubItemTable(out io.Writer, wantColor string, title string, items []*cw
t.AppendRow(table.Row{item.Name, status, item.State.LocalVersion, item.State.LocalPath})
}

io.WriteString(out, title+"\n")
io.WriteString(out, t.Render()+"\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/cliitem/inspect.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cliitem

import (
"fmt"
"encoding/json"
"fmt"
"os"
"path/filepath"

Expand Down
13 changes: 6 additions & 7 deletions cmd/crowdsec-cli/cliitem/metrics_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
)


func appsecMetricsTable(out io.Writer, wantColor string, itemName string, metrics map[string]int) {
t := cstable.NewLight(out, wantColor).Writer
t.AppendHeader(table.Row{"Inband Hits", "Outband Hits"})
Expand All @@ -20,8 +19,8 @@ func appsecMetricsTable(out io.Writer, wantColor string, itemName string, metric
strconv.Itoa(metrics["outband_hits"]),
})

io.WriteString(out, fmt.Sprintf("\n - (AppSec Rule) %s:\n", itemName))
io.WriteString(out, t.Render()+"\n")
t.SetTitle("(AppSec) " + itemName)
fmt.Fprintln(out, t.Render())
}

func scenarioMetricsTable(out io.Writer, wantColor string, itemName string, metrics map[string]int) {
Expand All @@ -40,8 +39,8 @@ func scenarioMetricsTable(out io.Writer, wantColor string, itemName string, metr
strconv.Itoa(metrics["underflow"]),
})

io.WriteString(out, fmt.Sprintf("\n - (Scenario) %s:\n", itemName))
io.WriteString(out, t.Render()+"\n")
t.SetTitle("(Scenario) " + itemName)
fmt.Fprintln(out, t.Render())
}

func parserMetricsTable(out io.Writer, wantColor string, itemName string, metrics map[string]map[string]int) {
Expand All @@ -65,7 +64,7 @@ func parserMetricsTable(out io.Writer, wantColor string, itemName string, metric
}

if showTable {
io.WriteString(out, fmt.Sprintf("\n - (Parser) %s:\n", itemName))
io.WriteString(out, t.Render()+"\n")
t.SetTitle("(Parser) " + itemName)
fmt.Fprintln(out, t.Render())
}
}
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/climachine/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (cli *cliMachines) inspectHubHuman(out io.Writer, machine *ent.Machine) {
t.AppendHeader(table.Row{"Name", "Status", "Version"})
t.SetTitle(itemType)
t.AppendRows(rows)
io.WriteString(out, t.Render()+"\n")
fmt.Fprintln(out, t.Render())
}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func (cli *cliMachines) inspectHuman(out io.Writer, machine *ent.Machine) {
t.AppendRow(table.Row{"Collections", coll.Name})
}

io.WriteString(out, t.Render()+"\n")
fmt.Fprintln(out, t.Render())
}

func (cli *cliMachines) inspect(machine *ent.Machine) error {
Expand Down
3 changes: 1 addition & 2 deletions cmd/crowdsec-cli/climachine/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (cli *cliMachines) listHuman(out io.Writer, machines ent.Machines) {
t.AppendRow(table.Row{m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, clientinfo.GetOSNameAndVersion(m), m.AuthType, hb})
}

io.WriteString(out, t.Render()+"\n")
fmt.Fprintln(out, t.Render())
}

func (cli *cliMachines) listCSV(out io.Writer, machines ent.Machines) error {
Expand Down Expand Up @@ -90,7 +90,6 @@ func (cli *cliMachines) listCSV(out io.Writer, machines ent.Machines) error {
func (cli *cliMachines) List(ctx context.Context, out io.Writer, db *database.Client) error {
// XXX: must use the provided db object, the one in the struct might be nil
// (calling List directly skips the PersistentPreRunE)

machines, err := db.ListMachines(ctx)
if err != nil {
return fmt.Errorf("unable to list machines: %w", err)
Expand Down
3 changes: 1 addition & 2 deletions cmd/crowdsec-cli/climetrics/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package climetrics
import (
"encoding/json"
"fmt"
"io"

"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -64,7 +63,7 @@ func (cli *cliMetrics) list() error {
t.AppendRow(table.Row{metric.Type, metric.Title, metric.Description})
}

io.WriteString(out, t.Render()+"\n")
fmt.Fprintln(out, t.Render())
case "json":
x, err := json.MarshalIndent(allMetrics, "", " ")
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions cmd/crowdsec-cli/climetrics/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"context"
"errors"
"fmt"
"slices"
"strings"

"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

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

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
)

Expand Down Expand Up @@ -99,6 +103,17 @@ cscli metrics list; cscli metrics list -o json
cscli metrics show acquisition parsers scenarios stash -o json`,
// Positional args are optional
DisableAutoGenTag: true,
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ms := NewMetricStore()
ret := []string{}
for _, section := range maptools.SortedKeys(ms) {
if !slices.Contains(args, section) && strings.Contains(section, toComplete) {
ret = append(ret, section)
}
}

return ret, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
args = expandAlias(args)
return cli.show(cmd.Context(), args, url, noUnit)
Expand Down
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/climetrics/statacquis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package climetrics

import (
"fmt"
"io"

"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -37,8 +38,7 @@ func (s statAcquis) Table(out io.Writer, wantColor string, noUnit bool, showEmpt
log.Warningf("while collecting acquis stats: %s", err)
} else if numRows > 0 || showEmpty {
title, _ := s.Description()
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}
}
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/climetrics/statalert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package climetrics

import (
"fmt"
"io"
"strconv"

Expand Down Expand Up @@ -38,8 +39,7 @@ func (s statAlert) Table(out io.Writer, wantColor string, noUnit bool, showEmpty

if numRows > 0 || showEmpty {
title, _ := s.Description()
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}
}
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/climetrics/statappsecengine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package climetrics

import (
"fmt"
"io"

"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -34,8 +35,7 @@ func (s statAppsecEngine) Table(out io.Writer, wantColor string, noUnit bool, sh
log.Warningf("while collecting appsec stats: %s", err)
} else if numRows > 0 || showEmpty {
title, _ := s.Description()
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}
}
5 changes: 2 additions & 3 deletions cmd/crowdsec-cli/climetrics/statappsecrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ func (s statAppsecRule) Table(out io.Writer, wantColor string, noUnit bool, show
if numRows, err := metricsToTable(t, appsecEngineRulesStats, keys, noUnit); err != nil {
log.Warningf("while collecting appsec rules stats: %s", err)
} else if numRows > 0 || showEmpty {
io.WriteString(out, fmt.Sprintf("Appsec '%s' Rules Metrics:\n", appsecEngine))
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
t.SetTitle(fmt.Sprintf("Appsec '%s' Rules Metrics", appsecEngine))
fmt.Fprintln(out, t.Render())
}
}
}
13 changes: 7 additions & 6 deletions cmd/crowdsec-cli/climetrics/statbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,20 @@ func (*statBouncer) extractRawMetrics(metrics []*ent.Metric) ([]bouncerMetricIte

if item.Name == nil {
logWarningOnce(warningsLogged, "missing 'name' field in metrics reported by "+bouncerName)

// no continue - keep checking the rest
valid = false
}

if item.Unit == nil {
logWarningOnce(warningsLogged, "missing 'unit' field in metrics reported by "+bouncerName)

valid = false
}

if item.Value == nil {
logWarningOnce(warningsLogged, "missing 'value' field in metrics reported by "+bouncerName)

valid = false
}

Expand Down Expand Up @@ -439,11 +442,8 @@ func (s *statBouncer) bouncerTable(out io.Writer, bouncerName string, wantColor
title = fmt.Sprintf("%s since %s", title, s.oldestTS[bouncerName].String())
}

// don't use SetTitle() because it draws the title inside table box
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
// empty line between tables
io.WriteString(out, "\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}

// Table displays a table of metrics for each bouncer
Expand All @@ -452,10 +452,11 @@ func (s *statBouncer) Table(out io.Writer, wantColor string, noUnit bool, showEm

for _, bouncerName := range maptools.SortedKeys(s.aggOverOrigin) {
s.bouncerTable(out, bouncerName, wantColor, noUnit)

found = true
}

if !found && showEmpty {
io.WriteString(out, "No bouncer metrics found.\n\n")
fmt.Fprintln(out, "No bouncer metrics found.")
}
}
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/climetrics/statbucket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package climetrics

import (
"fmt"
"io"

"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -35,8 +36,7 @@ func (s statBucket) Table(out io.Writer, wantColor string, noUnit bool, showEmpt
log.Warningf("while collecting scenario stats: %s", err)
} else if numRows > 0 || showEmpty {
title, _ := s.Description()
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}
}
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/climetrics/statdecision.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package climetrics

import (
"fmt"
"io"
"strconv"

Expand Down Expand Up @@ -53,8 +54,7 @@ func (s statDecision) Table(out io.Writer, wantColor string, noUnit bool, showEm

if numRows > 0 || showEmpty {
title, _ := s.Description()
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}
}
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/climetrics/statlapi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package climetrics

import (
"fmt"
"io"
"strconv"

Expand Down Expand Up @@ -49,8 +50,7 @@ func (s statLapi) Table(out io.Writer, wantColor string, noUnit bool, showEmpty

if numRows > 0 || showEmpty {
title, _ := s.Description()
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}
}
6 changes: 3 additions & 3 deletions cmd/crowdsec-cli/climetrics/statlapibouncer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package climetrics

import (
"fmt"
"io"

"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -35,8 +36,7 @@ func (s statLapiBouncer) Table(out io.Writer, wantColor string, noUnit bool, sho

if numRows > 0 || showEmpty {
title, _ := s.Description()
io.WriteString(out, title+":\n")
io.WriteString(out, t.Render()+"\n")
io.WriteString(out, "\n")
t.SetTitle(title)
fmt.Fprintln(out, t.Render())
}
}
Loading

0 comments on commit 5c7b957

Please sign in to comment.