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

Added a table output #1

Merged
merged 1 commit into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 27 additions & 17 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"github.com/apcera/termtables"
alog "github.com/apex/log"
"github.com/italolelis/reachable/pkg/log"
"github.com/italolelis/reachable/pkg/reachable"
Expand All @@ -20,43 +21,52 @@ func NewCheckCmd(ctx context.Context, timeout time.Duration) *cobra.Command {
Aliases: []string{"v"},
Run: func(cmd *cobra.Command, args []string) {
var wg errgroup.Group
var results [][]interface{}
// wg, ctx := errgroup.WithContext(ctx)
logger := log.WithContext(ctx)

for _, domain := range args {
domain := domain
wg.Go(func() error {
lg := logger.WithField("domain", domain)
result, err := reachable.IsReachable(ctx, domain, timeout)
if err != nil {
if lg.Logger.Level == alog.DebugLevel {
lg.Error(err.Error())
if logger.Level == alog.DebugLevel {
logger.Error(err.Error())
}

lg.Error("Unreachable!")
logger.WithField("domain", domain).Error("Unreachable!")
return err
}

lg.Debugf("Domain %s", result.Domain)
lg.Debugf("Port %s", result.Port)
lg.Debugf("Status Code %d", result.StatusCode)
lg.Debugf("DNS Lookup %d ms", int(result.Response.DNSLookup/time.Millisecond))
lg.Debugf("TCP Connection %d ms", int(result.Response.TCPConnection/time.Millisecond))
lg.Debugf("TLS Handshake %d ms", int(result.Response.TLSHandshake/time.Millisecond))
lg.Debugf("Server Processing %d ms", int(result.Response.ServerProcessing/time.Millisecond))
lg.Debugf("Content Transfer %d ms", int(result.Response.ContentTransfer(time.Now())/time.Millisecond))
lg.Debugf("Total Time %d ms", int(result.Response.Total(time.Now())/time.Millisecond))
results = append(results, []interface{}{
domain,
result.StatusCode,
int(result.Response.DNSLookup / time.Millisecond),
int(result.Response.TCPConnection / time.Millisecond),
int(result.Response.TLSHandshake / time.Millisecond),
int(result.Response.ServerProcessing / time.Millisecond),
int(result.Response.ContentTransfer(time.Now()) / time.Hour),
int(result.Response.Total(time.Now()) / time.Millisecond),
})

lg.Info("Reachable!")
logger.WithField("domain", domain).Info("Reachable!")

if lg.Logger.Level == alog.DebugLevel {
fmt.Println("")
}
return nil
})
}

wg.Wait()

if logger.Level == alog.DebugLevel {
table := termtables.CreateTable()
table.AddHeaders("Domain", "Status Code", "DNS Lookup", "TCP Connection", "TLS Handshake", "Server Processing", "Content Transfer", "Total Time")

for _, r := range results {
table.AddRow(r...)
}

fmt.Printf(table.Render())
}
},
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/italolelis/reachable

require (
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055
github.com/apex/log v1.0.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gojektech/heimdall v5.0.0+incompatible
github.com/gojektech/valkyrie v0.0.0-20180524055739-b19510f6c63c // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v0.0.3
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/InVisionApp/tabular v0.3.0 h1:4DGJoBZRTcgd/O+YgfG7/9bXAQy01tSJxrxWEuHVgnM=
github.com/InVisionApp/tabular v0.3.0/go.mod h1:/G6t7qe0ZULisB+FjMsB0Qu0mtJ2CZldq92nXWjfHGI=
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055 h1:IkPAzP+QjchKXXFX6LCcpDKa89b/e/0gPCUbQGWtUUY=
github.com/apcera/termtables v0.0.0-20170405184538-bcbc5dc54055/go.mod h1:8mHYHlOef9UC51cK1/WRvE/iQVM8O8QlYFa8eh8r5I8=
github.com/apex/log v1.0.0 h1:5UWeZC54mWVtOGSCjtuvDPgY/o0QxmjQgvYZ27pLVGQ=
github.com/apex/log v1.0.0/go.mod h1:yA770aXIDQrhVOIGurT/pVdfCpSq1GQV/auzMN5fzvY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -8,6 +12,8 @@ github.com/gojektech/valkyrie v0.0.0-20180524055739-b19510f6c63c h1:muIXCSMlpYUO
github.com/gojektech/valkyrie v0.0.0-20180524055739-b19510f6c63c/go.mod h1:tDYRk1s5Pms6XJjj5m2PxAzmQvaDU8GqDf1u6x7yxKw=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down