Skip to content

Commit

Permalink
improve table formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed May 25, 2024
1 parent 3101fae commit 765d3b8
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 93 deletions.
7 changes: 4 additions & 3 deletions providers/abuseipdb/abuseipdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strings"
"time"

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

"github.com/fatih/color"
"github.com/hashicorp/go-retryablehttp"
"github.com/jedib0t/go-pretty/v6/table"
Expand All @@ -25,7 +27,6 @@ const (
ProviderName = "abuseipdb"
APIURL = "https://api.abuseipdb.com"
HostIPPath = "/api/v2/check"
MaxColumnWidth = 120
IndentPipeHyphens = " |-----"
portLastModifiedFormat = "2006-01-02T15:04:05+07:00"
ResultTTL = 12 * time.Hour
Expand Down Expand Up @@ -127,7 +128,7 @@ func (c *Client) CreateTable(data []byte) (*table.Writer, error) {
})

tw.AppendRow(table.Row{"Last Reported", result.Data.LastReportedAt.UTC().Format(providers.TimeFormat)})
tw.AppendRow(table.Row{"Abuse Confidence Score", providers.DashIfEmpty(result.Data.AbuseConfidenceScore)})
tw.AppendRow(table.Row{"Confidence", providers.DashIfEmpty(result.Data.AbuseConfidenceScore)})
tw.AppendRow(table.Row{"Public", result.Data.IsPublic})
tw.AppendRow(table.Row{"Domain", providers.DashIfEmpty(result.Data.Domain)})
tw.AppendRow(table.Row{"Hostnames", providers.DashIfEmpty(strings.Join(result.Data.Hostnames, ", "))})
Expand All @@ -148,7 +149,7 @@ func (c *Client) CreateTable(data []byte) (*table.Writer, error) {
}

tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: true, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: true, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth, ColorsHeader: text.Colors{text.BgCyan}},
})
tw.SetAutoIndex(false)
// tw.SetStyle(table.StyleColoredDark)
Expand Down
6 changes: 3 additions & 3 deletions providers/annotated/annotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
const (
ProviderName = "annotated"
CacheTTL = 5 * time.Minute
MaxColumnWidth = 120
ipFileSuffixesToIgnore = "sh,conf"
)

Expand Down Expand Up @@ -253,7 +252,8 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {
var rows []table.Row

for prefix, annotations := range result {
tw.AppendRow(table.Row{"Prefix", dashIfEmpty(prefix.String())})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Prefix", providers.Column1MinWidth), dashIfEmpty(prefix.String())})

for _, anno := range annotations {
tw.AppendRow(table.Row{"Date", anno.Date})
Expand All @@ -279,7 +279,7 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)

Expand Down
10 changes: 5 additions & 5 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (
)

const (
ProviderName = "aws"
DocTTL = 24 * time.Hour
MaxColumnWidth = 120
ProviderName = "aws"
DocTTL = 24 * time.Hour
)

type Config struct {
Expand Down Expand Up @@ -314,7 +313,8 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

var rows []table.Row

tw.AppendRow(table.Row{"Prefix", dashIfEmpty(result.Prefix.IPPrefix.String())})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Prefix", providers.Column1MinWidth), dashIfEmpty(result.Prefix.IPPrefix.String())})
tw.AppendRow(table.Row{"Service", dashIfEmpty(result.Prefix.Service)})
tw.AppendRow(table.Row{"Region", dashIfEmpty(result.Prefix.Region)})

Expand All @@ -332,7 +332,7 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("AWS | Host: %s", c.Host.String())
Expand Down
8 changes: 2 additions & 6 deletions providers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

const (
MaxColumnWidth = 120
)

func (c *ProviderClient) loadProviderDataFromSource() error {
azureClient := azure.New()
azureClient.Client = c.HTTPClient
Expand Down Expand Up @@ -293,10 +289,10 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {
tw.AppendRow(table.Row{"Platform", dashIfEmpty(result.Properties.Platform)})
tw.AppendRow(table.Row{"Cloud", dashIfEmpty(result.Cloud)})
tw.AppendRow(table.Row{"System Service", dashIfEmpty(result.Properties.SystemService)})
tw.AppendRow(table.Row{"Network Features", dashIfEmpty(strings.Join(result.Properties.NetworkFeatures, ","))})
tw.AppendRow(table.Row{"Net Features", dashIfEmpty(strings.Join(result.Properties.NetworkFeatures, ","))})
tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("AZURE | Host: %s", c.Host.String())
Expand Down
6 changes: 1 addition & 5 deletions providers/bingbot/bingbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ func (c *ProviderClient) loadProviderData() error {
return nil
}

const (
MaxColumnWidth = 120
)

func (c *ProviderClient) Initialise() error {
if c.Cache == nil {
return errors.New("cache not set")
Expand Down Expand Up @@ -294,7 +290,7 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("Bingbot | Host: %s", c.Host.String())
Expand Down
9 changes: 3 additions & 6 deletions providers/criminalip/criminalip.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ func fetchData(client session.Session) (*HostSearchResult, error) {
return result, nil
}

const (
MaxColumnWidth = 120
)

func tidyBanner(banner string) string {
// remove empty lines using regex match
var lines []string
Expand Down Expand Up @@ -358,7 +354,8 @@ func (c *Client) CreateTable(data []byte) (*table.Writer, error) {
tw.AppendRow(table.Row{"Domains", strings.Join(getDomains(result.Domain), ", ")})
}

tw.AppendRow(table.Row{"Score Inbound", result.Score.Inbound})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Score Inbound", providers.Column1MinWidth), result.Score.Inbound})
tw.AppendRow(table.Row{"Score Outbound", result.Score.Outbound})

portDataForTable, err := c.GenPortDataForTable(result.Port.Data)
Expand Down Expand Up @@ -417,7 +414,7 @@ func (c *Client) CreateTable(data []byte) (*table.Writer, error) {

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: true, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: true, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("CRIMINAL IP | Host: %s", c.Host.String())
Expand Down
9 changes: 3 additions & 6 deletions providers/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ func (c *ProviderClient) loadProviderData() error {
return nil
}

const (
MaxColumnWidth = 120
)

func (c *ProviderClient) Initialise() error {
if c.Cache == nil {
return errors.New("cache not set")
Expand Down Expand Up @@ -270,7 +266,8 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

var rows []table.Row

tw.AppendRow(table.Row{"Prefix", dashIfEmpty(result.Record.NetworkText)})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Prefix", providers.Column1MinWidth), dashIfEmpty(result.Record.NetworkText)})
tw.AppendRow(table.Row{"Country Code", dashIfEmpty(result.Record.CountryCode)})
tw.AppendRow(table.Row{"City Name", dashIfEmpty(result.Record.CityName)})
tw.AppendRow(table.Row{"City Code", dashIfEmpty(result.Record.CityCode)})
Expand All @@ -286,7 +283,7 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("DIGITAL OCEAN | Host: %s", c.Host.String())
Expand Down
9 changes: 3 additions & 6 deletions providers/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ func (c *ProviderClient) loadProviderData() error {
return nil
}

const (
MaxColumnWidth = 120
)

func (c *ProviderClient) Initialise() error {
if c.Cache == nil {
return errors.New("cache not set")
Expand Down Expand Up @@ -292,7 +288,8 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

var rows []table.Row

tw.AppendRow(table.Row{"Prefix", dashIfEmpty(result.Prefix.String())})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Prefix", providers.Column1MinWidth), dashIfEmpty(result.Prefix.String())})
tw.AppendRow(table.Row{"Scope", dashIfEmpty(result.Scope)})
tw.AppendRow(table.Row{"Service", dashIfEmpty(result.Service)})

Expand All @@ -306,7 +303,7 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("GCP | Host: %s", c.Host.String())
Expand Down
9 changes: 3 additions & 6 deletions providers/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ func (c *ProviderClient) loadProviderData() error {
return nil
}

const (
MaxColumnWidth = 120
)

func (c *ProviderClient) Initialise() error {
if c.Cache == nil {
return errors.New("cache not set")
Expand Down Expand Up @@ -286,15 +282,16 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

var rows []table.Row

tw.AppendRow(table.Row{"Prefix", dashIfEmpty(result.Prefix.String())})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Prefix", providers.Column1MinWidth), dashIfEmpty(result.Prefix.String())})

if !result.CreationTime.IsZero() {
tw.AppendRow(table.Row{"Creation Time", dashIfEmpty(result.CreationTime.UTC().Format(providers.TimeFormat))})
}

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("GOOGLE | Host: %s", c.Host.String())
Expand Down
9 changes: 3 additions & 6 deletions providers/googlebot/googlebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ func (c *ProviderClient) loadProviderData() error {
return nil
}

const (
MaxColumnWidth = 120
)

func (c *ProviderClient) Initialise() error {
if c.Cache == nil {
return errors.New("cache not set")
Expand Down Expand Up @@ -286,15 +282,16 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

var rows []table.Row

tw.AppendRow(table.Row{"Prefix", dashIfEmpty(result.Prefix.String())})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Prefix", providers.Column1MinWidth), dashIfEmpty(result.Prefix.String())})

if !result.CreationTime.IsZero() {
tw.AppendRow(table.Row{"Creation Time", dashIfEmpty(result.CreationTime.String())})
}

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("Googlebot | Host: %s", c.Host.String())
Expand Down
9 changes: 3 additions & 6 deletions providers/icloudpr/icloudpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ func (c *ProviderClient) loadProviderData() error {
return nil
}

const (
MaxColumnWidth = 120
)

func (c *ProviderClient) Initialise() error {
if c.Cache == nil {
return errors.New("cache not set")
Expand Down Expand Up @@ -273,7 +269,8 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

var rows []table.Row

tw.AppendRow(table.Row{"Prefix", dashIfEmpty(result.Prefix.String())})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Prefix", providers.Column1MinWidth), dashIfEmpty(result.Prefix.String())})
tw.AppendRow(table.Row{"Alpha2Code", dashIfEmpty(result.Alpha2Code)})
tw.AppendRow(table.Row{"Region", dashIfEmpty(result.Region)})
tw.AppendRow(table.Row{"City", dashIfEmpty(result.City)})
Expand All @@ -285,7 +282,7 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {

tw.AppendRows(rows)
tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: false, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
})
tw.SetAutoIndex(false)
tw.SetTitle("ICLOUD PRIVATE RELAY | Host: %s", c.Host.String())
Expand Down
10 changes: 3 additions & 7 deletions providers/ipapi/ipapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

const (
ProviderName = "ipapi"
MaxColumnWidth = 120
IndentPipeHyphens = " |-----"
portLastModifiedFormat = "2006-01-02T15:04:05+07:00"
ResultTTL = 1 * time.Hour
Expand Down Expand Up @@ -124,8 +123,8 @@ func (c *Client) CreateTable(data []byte) (*table.Writer, error) {
}

tw := table.NewWriter()

tw.AppendRow(table.Row{"Organisation", providers.DashIfEmpty(findHostData.Org)})
// pad column to ensure title row fills the table
tw.AppendRow(table.Row{providers.PadRight("Organisation", providers.Column1MinWidth), providers.DashIfEmpty(findHostData.Org)})
tw.AppendRow(table.Row{"Hostname", providers.DashIfEmpty(findHostData.Hostname)})
tw.AppendRow(table.Row{"Country", providers.DashIfEmpty(findHostData.CountryName)})
tw.AppendRow(table.Row{"Region", providers.DashIfEmpty(findHostData.Region)})
Expand All @@ -134,13 +133,10 @@ func (c *Client) CreateTable(data []byte) (*table.Writer, error) {
tw.AppendRow(table.Row{"ASN", providers.DashIfEmpty(findHostData.Asn)})

tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: false, WidthMax: MaxColumnWidth, WidthMin: 50},
{Number: 2, AutoMerge: true, WidthMax: providers.WideColumnMaxWidth, WidthMin: providers.WideColumnMinWidth},
{Number: 1, AutoMerge: true},
})

tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: true, WidthMax: MaxColumnWidth, WidthMin: 50},
})
tw.SetAutoIndex(false)
// tw.SetStyle(table.StyleColoredDark)
// tw.Style().Options.DrawBorder = true
Expand Down
10 changes: 6 additions & 4 deletions providers/ipurl/ipurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
const (
ProviderName = "ipurl"
CacheTTL = 3 * time.Hour
// override default set in providers package constant as column 2 is expected to be wide
column1MinWidth = 13
)

type Config struct {
Expand Down Expand Up @@ -371,7 +373,9 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {
}

tw := table.NewWriter()
tw.AppendRow(table.Row{color.HiWhiteString("Prefixes")})

// pad column to ensure title row fills the table
tw.AppendRow(table.Row{color.HiWhiteString(providers.PadRight("Prefixes", column1MinWidth))})

for prefix, urls := range result {
tw.AppendRow(table.Row{"", color.CyanString(prefix.String())})
Expand All @@ -382,7 +386,7 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {
}

tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 2, AutoMerge: true, WidthMax: MaxColumnWidth, WidthMin: 10},
{Number: 2, AutoMerge: true, WidthMax: providers.WideColumnMaxWidth, WidthMin: 10},
})
tw.SetAutoIndex(false)
tw.SetTitle("IP URL | Host: %s", c.Host.String())
Expand All @@ -394,6 +398,4 @@ func (c *ProviderClient) CreateTable(data []byte) (*table.Writer, error) {
return &tw, nil
}

const MaxColumnWidth = 120

const IndentPipeHyphens = " |-----"
Loading

0 comments on commit 765d3b8

Please sign in to comment.