Skip to content

Commit

Permalink
Support multiple domain check
Browse files Browse the repository at this point in the history
  • Loading branch information
italolelis committed Jul 12, 2018
1 parent 1285d79 commit cb691c2
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 21 deletions.
14 changes: 13 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@
[[constraint]]
name = "github.com/apex/log"
version = "1.0.0"

[[constraint]]
branch = "master"
name = "golang.org/x/sync"
53 changes: 37 additions & 16 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cmd

import (
"context"
"fmt"
"time"

alog "github.com/apex/log"
"github.com/italolelis/reachable/pkg/log"
"github.com/italolelis/reachable/pkg/reachable"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

// NewCheckCmd creates a new check command
Expand All @@ -16,26 +19,44 @@ func NewCheckCmd(ctx context.Context, timeout time.Duration) *cobra.Command {
Short: "Checks if a domain is reachable",
Aliases: []string{"v"},
Run: func(cmd *cobra.Command, args []string) {
var wg errgroup.Group
// wg, ctx := errgroup.WithContext(ctx)
logger := log.WithContext(ctx)

result, err := reachable.IsReachable(ctx, args[0], timeout)
if err != nil {
logger.Debug(err.Error())
logger.Error("Not Reachable!")
return
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())
}

lg.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))

lg.Info("Reachable!")

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

logger.Debugf("Domain %s", result.Domain)
logger.Debugf("IP %s", result.IP)
logger.Debugf("Status Code %d", result.StatusCode)
logger.Debugf("DNS Lookup %d ms", int(result.Response.DNSLookup/time.Millisecond))
logger.Debugf("TCP Connection %d ms", int(result.Response.TCPConnection/time.Millisecond))
logger.Debugf("TLS Handshake %d ms", int(result.Response.TLSHandshake/time.Millisecond))
logger.Debugf("Server Processing %d ms", int(result.Response.ServerProcessing/time.Millisecond))
logger.Debugf("Content Transfer %d ms", int(result.Response.ContentTransfer(time.Now())/time.Millisecond))
logger.Debugf("Total Time %d ms", int(result.Response.Total(time.Now())/time.Millisecond))

logger.Info("Reachable!")
wg.Wait()
},
}
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func NewRootCmd() *cobra.Command {
Short: "Reachable is a CLI tool to check if a domain is up",
Example: `
reachable check google.com
reachable check google.com facebook.com twitter.com
reachable check google.com -v
`,
Expand Down
13 changes: 9 additions & 4 deletions pkg/reachable/reachable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io"
"io/ioutil"
"net/http"
"strings"
"net/url"
"time"

"github.com/gojektech/heimdall"
Expand All @@ -25,11 +25,16 @@ type Reachable struct {
func IsReachable(ctx context.Context, domain string, timeout time.Duration) (*Reachable, error) {
var result httpstat.Result

if !strings.Contains(domain, "http") {
domain = "http://" + domain
u, err := url.Parse(domain)
if err != nil {
return nil, err
}

if u.Scheme == "" {
u.Scheme = "http"
}

req, err := http.NewRequest(http.MethodGet, domain, nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/reachable/reachable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func TestReachable(t *testing.T) {
timeout: 10 * time.Second,
function: testLookupDomain,
},
{
scenario: "test lookup domain and port but no scheme",
domain: "google.com:443",
timeout: 10 * time.Second,
function: testInvalidDomain,
},
{
scenario: "test invalid domain",
domain: "google...wrong.com",
Expand Down

0 comments on commit cb691c2

Please sign in to comment.