From 6f9025397e65ab5d032fbbded9e849c739570179 Mon Sep 17 00:00:00 2001 From: Leonardo Date: Wed, 5 Jun 2019 06:35:00 +0200 Subject: [PATCH] add concurrency on --all and --export Signed-off-by: Leonardo --- cmd/flares/main.go | 22 +++++++++++++++------- internal/cloudflare/cloudflare.go | 4 +--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/flares/main.go b/cmd/flares/main.go index 2295a7f..2856e17 100644 --- a/cmd/flares/main.go +++ b/cmd/flares/main.go @@ -20,6 +20,7 @@ import ( "log" "os" "path" + "sync" "github.com/pkg/errors" "github.com/urfave/cli" @@ -96,21 +97,28 @@ func main() { zones, err := dns.Zones() fatalIfErr(err) + wg := sync.WaitGroup{} for id, domain := range zones { if debugFlag { fmt.Println(id, domain) } - table, err := dns.TableFor(domain) - fatalIfErr(err) + wg.Add(1) + go func() { + table, err := dns.TableFor(domain) + fatalIfErr(err) - if c.Bool("export") { - export(domain, table) - continue - } + if c.Bool("export") { + export(domain, table) + } else { + fmt.Println(string(table)) + } + + wg.Done() + }() - fmt.Println(string(table)) } + wg.Wait() return nil } diff --git a/internal/cloudflare/cloudflare.go b/internal/cloudflare/cloudflare.go index 32e3768..b3e5025 100644 --- a/internal/cloudflare/cloudflare.go +++ b/internal/cloudflare/cloudflare.go @@ -8,7 +8,6 @@ package cloudflare import ( "encoding/json" - "fmt" "io/ioutil" "net/http" "net/url" @@ -57,8 +56,8 @@ func (cf Cloudflare) TableFor(domain string) ([]byte, error) { // ref: https://api.cloudflare.com/#zone-list-zones func (cf Cloudflare) Zones() (map[string]string, error) { var result = map[string]string{} - var count = 1 + var count = 1 for { endpoint := cf.API + "/zones" u, err := url.Parse(endpoint) @@ -165,7 +164,6 @@ func (cf Cloudflare) zoneIDFor(domain string) (string, error) { return "", err } if !response.Success { - fmt.Println("DEBUG:", response.Errors) return "", errors.New(response.Errors[0].Message) } if len(response.Result) == 0 {