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

cscli refact: extract table wrapper to own package #3105

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions cmd/crowdsec-cli/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
Expand Down Expand Up @@ -54,7 +55,8 @@
}

func (cli *cliAlerts) alertsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
switch cli.cfg().Cscli.Output {
cfg := cli.cfg()
switch 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 @@ -104,7 +106,7 @@
return nil
}

alertsTable(color.Output, alerts, printMachine)
alertsTable(color.Output, cfg.Cscli.Color, alerts, printMachine)
}

return nil
Expand Down Expand Up @@ -138,15 +140,17 @@
return err
}

alertDecisionsTable(color.Output, alert)
cfg := cli.cfg()

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L144

Added line #L144 was not covered by tests
alertDecisionsTable(color.Output, cfg.Cscli.Color, alert)

if len(alert.Meta) > 0 {
fmt.Printf("\n - Context :\n")
sort.Slice(alert.Meta, func(i, j int) bool {
return alert.Meta[i].Key < alert.Meta[j].Key
})

table := newTable(color.Output)
table := cstable.New(color.Output, cfg.Cscli.Color)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L153

Added line #L153 was not covered by tests
table.SetRowLines(false)
table.SetHeaders("Key", "Value")

Expand All @@ -171,7 +175,7 @@
fmt.Printf("\n - Events :\n")

for _, event := range alert.Events {
alertEventTable(color.Output, event)
alertEventTable(color.Output, cfg.Cscli.Color, event)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts.go#L178

Added line #L178 was not covered by tests
}
}

Expand Down
13 changes: 7 additions & 6 deletions cmd/crowdsec-cli/alerts_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

log "github.com/sirupsen/logrus"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/pkg/models"
)

func alertsTable(out io.Writer, alerts *models.GetAlertsResponse, printMachine bool) {
t := newTable(out)
func alertsTable(out io.Writer, wantColor string, alerts *models.GetAlertsResponse, printMachine bool) {
t := cstable.New(out, wantColor)
t.SetRowLines(false)

header := []string{"ID", "value", "reason", "country", "as", "decisions", "created_at"}
Expand Down Expand Up @@ -51,9 +52,9 @@
t.Render()
}

func alertDecisionsTable(out io.Writer, alert *models.Alert) {
func alertDecisionsTable(out io.Writer, wantColor string, alert *models.Alert) {
foundActive := false
t := newTable(out)
t := cstable.New(out, wantColor)
t.SetRowLines(false)
t.SetHeaders("ID", "scope:value", "action", "expiration", "created_at")

Expand Down Expand Up @@ -90,10 +91,10 @@
}
}

func alertEventTable(out io.Writer, event *models.Event) {
func alertEventTable(out io.Writer, wantColor string, event *models.Event) {

Check warning on line 94 in cmd/crowdsec-cli/alerts_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts_table.go#L94

Added line #L94 was not covered by tests
fmt.Fprintf(out, "\n- Date: %s\n", *event.Timestamp)

t := newTable(out)
t := cstable.New(out, wantColor)

Check warning on line 97 in cmd/crowdsec-cli/alerts_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/alerts_table.go#L97

Added line #L97 was not covered by tests
t.SetHeaders("Key", "Value")
sort.Slice(event.Meta, func(i, j int) bool {
return event.Meta[i].Key < event.Meta[j].Key
Expand Down
5 changes: 3 additions & 2 deletions cmd/crowdsec-cli/bouncers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
"github.com/crowdsecurity/crowdsec/pkg/database"
Expand Down Expand Up @@ -90,7 +91,7 @@
}

func (cli *cliBouncers) listHuman(out io.Writer, bouncers ent.Bouncers) {
t := newLightTable(out).Writer
t := cstable.NewLight(out, cli.cfg().Cscli.Color).Writer
t.AppendHeader(table.Row{"Name", "IP Address", "Valid", "Last API pull", "Type", "Version", "Auth Type"})

for _, b := range bouncers {
Expand Down Expand Up @@ -411,7 +412,7 @@
}

func (cli *cliBouncers) inspectHuman(out io.Writer, bouncer *ent.Bouncer) {
t := newTable(out).Writer
t := cstable.NewLight(out, cli.cfg().Cscli.Color).Writer

Check warning on line 415 in cmd/crowdsec-cli/bouncers.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/bouncers.go#L415

Added line #L415 was not covered by tests

t.SetTitle("Bouncer: " + bouncer.Name)

Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (cli *cliConsole) newStatusCmd() *cobra.Command {
consoleCfg := cfg.API.Server.ConsoleConfig
switch cfg.Cscli.Output {
case "human":
cmdConsoleStatusTable(color.Output, *consoleCfg)
cmdConsoleStatusTable(color.Output, cfg.Cscli.Color, *consoleCfg)
case "json":
out := map[string](*bool){
csconfig.SEND_MANUAL_SCENARIOS: consoleCfg.ShareManualDecisions,
Expand Down
10 changes: 6 additions & 4 deletions cmd/crowdsec-cli/console_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package main
import (
"io"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/table"
"github.com/jedib0t/go-pretty/v6/text"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/emoji"
)

func cmdConsoleStatusTable(out io.Writer, consoleCfg csconfig.ConsoleConfig) {
t := newTable(out)
func cmdConsoleStatusTable(out io.Writer, wantColor string, consoleCfg csconfig.ConsoleConfig) {
t := cstable.New(out, wantColor)
t.SetRowLines(false)

t.SetHeaders("Option Name", "Activated", "Description")
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
t.SetHeaderAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)

for _, option := range csconfig.CONSOLE_CONFIGS {
activated := emoji.CrossMark
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
package main
package cstable

// transisional file to keep (minimal) backwards compatibility with the old table
// we can migrate the code to the new dependency later, it can already use the Writer interface

import (
"fmt"
"io"
"os"

isatty "github.com/mattn/go-isatty"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)

func RenderTitle(out io.Writer, title string) {
if out == nil {
panic("renderTableTitle: out is nil")

Check warning on line 19 in cmd/crowdsec-cli/cstable/cstable.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/cstable/cstable.go#L19

Added line #L19 was not covered by tests
}

if title == "" {
return
}

Check warning on line 24 in cmd/crowdsec-cli/cstable/cstable.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/cstable/cstable.go#L23-L24

Added lines #L23 - L24 were not covered by tests

fmt.Fprintln(out, title)
}

func shouldWeColorize(wantColor string) bool {
switch wantColor {
case "yes":
return true
case "no":
return false

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/cstable/cstable.go#L31-L34

Added lines #L31 - L34 were not covered by tests
default:
return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
}
}

type Table struct {
Writer table.Writer
output io.Writer
align []text.Align
alignHeader []text.Align
}

func newTable(out io.Writer) *Table {
func New(out io.Writer, wantColor string) *Table {
if out == nil {
panic("newTable: out is nil")
}

t := table.NewWriter()

// colorize output, use unicode box characters
fancy := shouldWeColorize()
fancy := shouldWeColorize(wantColor)

color := table.ColorOptions{}
colorOptions := table.ColorOptions{}

if fancy {
color.Header = text.Colors{text.Italic}
color.Border = text.Colors{text.FgHiBlack}
color.Separator = text.Colors{text.FgHiBlack}
colorOptions.Header = text.Colors{text.Italic}
colorOptions.Border = text.Colors{text.FgHiBlack}
colorOptions.Separator = text.Colors{text.FgHiBlack}

Check warning on line 62 in cmd/crowdsec-cli/cstable/cstable.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/cstable/cstable.go#L60-L62

Added lines #L60 - L62 were not covered by tests
}

// no upper/lower case transformations
Expand All @@ -46,7 +72,7 @@

style := table.Style{
Box: box,
Color: color,
Color: colorOptions,
Format: format,
HTML: table.DefaultHTMLOptions,
Options: table.OptionsDefault,
Expand All @@ -63,8 +89,8 @@
}
}

func newLightTable(output io.Writer) *Table {
t := newTable(output)
func NewLight(output io.Writer, wantColor string) *Table {
t := New(output, wantColor)
s := t.Writer.Style()
s.Box.Left = ""
s.Box.LeftSeparator = ""
Expand Down
3 changes: 2 additions & 1 deletion cmd/crowdsec-cli/decisions_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"strconv"

"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
)

func (cli *cliDecisions) decisionsTable(out io.Writer, alerts *models.GetAlertsResponse, printMachine bool) {
t := newTable(out)
t := cstable.New(out, cli.cfg().Cscli.Color)
t.SetRowLines(false)

header := []string{"ID", "Source", "Scope:Value", "Reason", "Action", "Country", "AS", "Events", "expiration", "Alert ID"}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (cli *cliHub) list(all bool) error {
}
}

err = listItems(color.Output, cwhub.ItemTypes, items, true, cfg.Cscli.Output)
err = listItems(color.Output, cfg.Cscli.Color, cwhub.ItemTypes, items, true, cfg.Cscli.Output)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/crowdsec-cli/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@

switch cfg.Cscli.Output {
case "human":
hubTestResultTable(color.Output, testResult)
hubTestResultTable(color.Output, cfg.Cscli.Color, testResult)

Check warning on line 374 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L374

Added line #L374 was not covered by tests
case "json":
jsonResult := make(map[string][]string, 0)
jsonResult["success"] = make([]string, 0)
Expand Down Expand Up @@ -480,7 +480,7 @@

switch cfg.Cscli.Output {
case "human":
hubTestListTable(color.Output, hubPtr.Tests)
hubTestListTable(color.Output, cfg.Cscli.Color, hubPtr.Tests)

Check warning on line 483 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L483

Added line #L483 was not covered by tests
case "json":
j, err := json.MarshalIndent(hubPtr.Tests, " ", " ")
if err != nil {
Expand Down Expand Up @@ -578,15 +578,15 @@
switch cfg.Cscli.Output {
case "human":
if showParserCov || showAll {
hubTestParserCoverageTable(color.Output, parserCoverage)
hubTestParserCoverageTable(color.Output, cfg.Cscli.Color, parserCoverage)

Check warning on line 581 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L581

Added line #L581 was not covered by tests
}

if showScenarioCov || showAll {
hubTestScenarioCoverageTable(color.Output, scenarioCoverage)
hubTestScenarioCoverageTable(color.Output, cfg.Cscli.Color, scenarioCoverage)

Check warning on line 585 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L585

Added line #L585 was not covered by tests
}

if showAppsecCov || showAll {
hubTestAppsecRuleCoverageTable(color.Output, appsecRuleCoverage)
hubTestAppsecRuleCoverageTable(color.Output, cfg.Cscli.Color, appsecRuleCoverage)

Check warning on line 589 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L589

Added line #L589 was not covered by tests
}

fmt.Println()
Expand Down
44 changes: 23 additions & 21 deletions cmd/crowdsec-cli/hubtest_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
"fmt"
"io"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/table"
"github.com/jedib0t/go-pretty/v6/text"

"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
"github.com/crowdsecurity/crowdsec/pkg/emoji"
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
)

func hubTestResultTable(out io.Writer, testResult map[string]bool) {
t := newLightTable(out)
func hubTestResultTable(out io.Writer, wantColor string, testResult map[string]bool) {
t := cstable.NewLight(out, wantColor)

Check warning on line 15 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L14-L15

Added lines #L14 - L15 were not covered by tests
t.SetHeaders("Test", "Result")
t.SetHeaderAlignment(table.AlignLeft)
t.SetAlignment(table.AlignLeft)
t.SetHeaderAlignment(text.AlignLeft)
t.SetAlignment(text.AlignLeft)

Check warning on line 18 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L17-L18

Added lines #L17 - L18 were not covered by tests

for testName, success := range testResult {
status := emoji.CheckMarkButton
Expand All @@ -27,11 +29,11 @@
t.Render()
}

func hubTestListTable(out io.Writer, tests []*hubtest.HubTestItem) {
t := newLightTable(out)
func hubTestListTable(out io.Writer, wantColor string, tests []*hubtest.HubTestItem) {
t := cstable.NewLight(out, wantColor)

Check warning on line 33 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L32-L33

Added lines #L32 - L33 were not covered by tests
t.SetHeaders("Name", "Path")
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft)
t.SetAlignment(table.AlignLeft, table.AlignLeft)
t.SetHeaderAlignment(text.AlignLeft, text.AlignLeft)
t.SetAlignment(text.AlignLeft, text.AlignLeft)

Check warning on line 36 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L35-L36

Added lines #L35 - L36 were not covered by tests

for _, test := range tests {
t.AddRow(test.Name, test.Path)
Expand All @@ -40,11 +42,11 @@
t.Render()
}

func hubTestParserCoverageTable(out io.Writer, coverage []hubtest.Coverage) {
t := newLightTable(out)
func hubTestParserCoverageTable(out io.Writer, wantColor string, coverage []hubtest.Coverage) {
t := cstable.NewLight(out, wantColor)

Check warning on line 46 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L45-L46

Added lines #L45 - L46 were not covered by tests
t.SetHeaders("Parser", "Status", "Number of tests")
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
t.SetHeaderAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)
t.SetAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)

Check warning on line 49 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L48-L49

Added lines #L48 - L49 were not covered by tests

parserTested := 0

Expand All @@ -61,11 +63,11 @@
t.Render()
}

func hubTestAppsecRuleCoverageTable(out io.Writer, coverage []hubtest.Coverage) {
t := newLightTable(out)
func hubTestAppsecRuleCoverageTable(out io.Writer, wantColor string, coverage []hubtest.Coverage) {
t := cstable.NewLight(out, wantColor)

Check warning on line 67 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L66-L67

Added lines #L66 - L67 were not covered by tests
t.SetHeaders("Appsec Rule", "Status", "Number of tests")
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
t.SetHeaderAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)
t.SetAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)

Check warning on line 70 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L69-L70

Added lines #L69 - L70 were not covered by tests

parserTested := 0

Expand All @@ -82,11 +84,11 @@
t.Render()
}

func hubTestScenarioCoverageTable(out io.Writer, coverage []hubtest.Coverage) {
t := newLightTable(out)
func hubTestScenarioCoverageTable(out io.Writer, wantColor string, coverage []hubtest.Coverage) {
t := cstable.NewLight(out, wantColor)

Check warning on line 88 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L87-L88

Added lines #L87 - L88 were not covered by tests
t.SetHeaders("Scenario", "Status", "Number of tests")
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
t.SetHeaderAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)
t.SetAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)

Check warning on line 91 in cmd/crowdsec-cli/hubtest_table.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest_table.go#L90-L91

Added lines #L90 - L91 were not covered by tests

parserTested := 0

Expand Down
Loading
Loading