github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec-cli/utils_table.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io" 6 "strconv" 7 8 "github.com/aquasecurity/table" 9 10 "github.com/crowdsecurity/crowdsec/pkg/cwhub" 11 "github.com/crowdsecurity/crowdsec/pkg/emoji" 12 ) 13 14 func listHubItemTable(out io.Writer, title string, items []*cwhub.Item) { 15 t := newLightTable(out) 16 t.SetHeaders("Name", fmt.Sprintf("%v Status", emoji.Package), "Version", "Local Path") 17 t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft) 18 t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft) 19 20 for _, item := range items { 21 status := fmt.Sprintf("%v %s", item.State.Emoji(), item.State.Text()) 22 t.AddRow(item.Name, status, item.State.LocalVersion, item.State.LocalPath) 23 } 24 25 renderTableTitle(out, title) 26 t.Render() 27 } 28 29 func appsecMetricsTable(out io.Writer, itemName string, metrics map[string]int) { 30 t := newTable(out) 31 t.SetHeaders("Inband Hits", "Outband Hits") 32 33 t.AddRow( 34 strconv.Itoa(metrics["inband_hits"]), 35 strconv.Itoa(metrics["outband_hits"]), 36 ) 37 38 renderTableTitle(out, fmt.Sprintf("\n - (AppSec Rule) %s:", itemName)) 39 t.Render() 40 } 41 42 func scenarioMetricsTable(out io.Writer, itemName string, metrics map[string]int) { 43 if metrics["instantiation"] == 0 { 44 return 45 } 46 47 t := newTable(out) 48 t.SetHeaders("Current Count", "Overflows", "Instantiated", "Poured", "Expired") 49 50 t.AddRow( 51 strconv.Itoa(metrics["curr_count"]), 52 strconv.Itoa(metrics["overflow"]), 53 strconv.Itoa(metrics["instantiation"]), 54 strconv.Itoa(metrics["pour"]), 55 strconv.Itoa(metrics["underflow"]), 56 ) 57 58 renderTableTitle(out, fmt.Sprintf("\n - (Scenario) %s:", itemName)) 59 t.Render() 60 } 61 62 func parserMetricsTable(out io.Writer, itemName string, metrics map[string]map[string]int) { 63 t := newTable(out) 64 t.SetHeaders("Parsers", "Hits", "Parsed", "Unparsed") 65 66 // don't show table if no hits 67 showTable := false 68 69 for source, stats := range metrics { 70 if stats["hits"] > 0 { 71 t.AddRow( 72 source, 73 strconv.Itoa(stats["hits"]), 74 strconv.Itoa(stats["parsed"]), 75 strconv.Itoa(stats["unparsed"]), 76 ) 77 78 showTable = true 79 } 80 } 81 82 if showTable { 83 renderTableTitle(out, fmt.Sprintf("\n - (Parser) %s:", itemName)) 84 t.Render() 85 } 86 }