feat: Add colors and tables

This commit is contained in:
Karan Sharma 2020-12-13 08:47:07 +05:30
parent bcc405db1f
commit d9a70daaf8
5 changed files with 64 additions and 29 deletions

View file

@ -3,7 +3,6 @@ package main
import (
"os"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
@ -14,23 +13,6 @@ var (
verboseEnabled = false
)
// initLogger initializes logger
func initLogger(verbose bool) *logrus.Logger {
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
DisableLevelTruncation: true,
})
// Set logger level
if verbose {
logger.SetLevel(logrus.DebugLevel)
logger.Debug("verbose logging enabled")
} else {
logger.SetLevel(logrus.InfoLevel)
}
return logger
}
func main() {
var (
logger = initLogger(verboseEnabled)
@ -101,6 +83,11 @@ func main() {
Usage: "Use IPv4 only",
Destination: &hub.QueryFlags.UseIPv4,
},
&cli.BoolFlag{
Name: "time",
Usage: "Display how long it took for the response to arrive",
Destination: &hub.QueryFlags.DisplayTimeTaken,
},
&cli.BoolFlag{
Name: "verbose",
Usage: "Enable verbose logging",

View file

@ -18,16 +18,17 @@ type Hub struct {
// QueryFlags is used store the value of CLI flags.
type QueryFlags struct {
QNames *cli.StringSlice
QTypes *cli.StringSlice
QClasses *cli.StringSlice
Nameservers *cli.StringSlice
IsDOH bool
IsDOT bool
IsUDP bool
UseTCP bool
UseIPv4 bool
UseIPv6 bool
QNames *cli.StringSlice
QTypes *cli.StringSlice
QClasses *cli.StringSlice
Nameservers *cli.StringSlice
IsDOH bool
IsDOT bool
IsUDP bool
UseTCP bool
UseIPv4 bool
UseIPv6 bool
DisplayTimeTaken bool
}
// NewHub initializes an instance of Hub which holds app wide configuration.
@ -45,3 +46,20 @@ func NewHub(logger *logrus.Logger, buildVersion string) *Hub {
}
return hub
}
// initLogger initializes logger
func initLogger(verbose bool) *logrus.Logger {
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
DisableLevelTruncation: true,
})
// Set logger level
if verbose {
logger.SetLevel(logrus.DebugLevel)
logger.Debug("verbose logging enabled")
} else {
logger.SetLevel(logrus.InfoLevel)
}
return logger
}

View file

@ -2,11 +2,13 @@ package main
import (
"fmt"
"os"
"strconv"
"github.com/fatih/color"
"github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/olekukonko/tablewriter"
)
// Output takes a list of `dns.Answers` and based
@ -15,6 +17,14 @@ func (hub *Hub) Output(responses []resolvers.Response) {
// Create SprintXxx functions to mix strings with other non-colorized strings:
green := color.New(color.FgGreen).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
table := tablewriter.NewWriter(os.Stdout)
header := []string{"Name", "Type", "Class", "TTL", "Address"}
if hub.QueryFlags.DisplayTimeTaken {
header = append(header, "Time Taken")
}
table.SetHeader(header)
for _, r := range responses {
var res string
for _, a := range r.Message.Answer {
@ -27,7 +37,13 @@ func (hub *Hub) Output(responses []resolvers.Response) {
qclass := dns.Class(h.Class).String()
ttl := strconv.FormatInt(int64(h.Ttl), 10) + "s"
qtype := blue(dns.Type(h.Rrtype).String())
fmt.Printf("%s \t %s \t %s \t %s \t %s\n", qtype, name, qclass, ttl, res)
output := []string{name, qtype, qclass, ttl, res}
// Print how long it took
if hub.QueryFlags.DisplayTimeTaken {
output = append(output, fmt.Sprintf("%dms", r.RTT.Milliseconds()))
}
table.Append(output)
}
}
table.Render()
}