diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dc77706..8313a86 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,5 +24,13 @@ jobs: env: GOPROXY: https://proxy.golang.org + - name: Run linter + uses: docker://golangci/golangci-lint + env: + GOROOT: /usr/local/go + with: + entrypoint: golangci-lint + args: run --enable-all -D lll -D errcheck + - name: Run tests run: go test -mod readonly -race diff --git a/CHANGELOG.md b/CHANGELOG.md index e67c946..8afa935 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Unreleased -- Migrated CI from Travis CI to GitHub Actions +- Migrated CI from Travis CI to GitHub Actions [\#27](https://github.com/koenrh/s3enum/pull/27) +- Added GolangCI-Lint [\#28](https://github.com/koenrh/s3enum/pull/28) ## v0.1.0 diff --git a/bucket_checker.go b/bucket_checker.go index 1032a99..1bedd10 100644 --- a/bucket_checker.go +++ b/bucket_checker.go @@ -3,9 +3,10 @@ package main import ( "errors" "fmt" - "github.com/miekg/dns" "net" "strings" + + "github.com/miekg/dns" ) type Resolver interface { @@ -33,7 +34,7 @@ type S3Resolver struct { const s3host = "s3.amazonaws.com" -// IsBucket determines wheter this prefix is a valid S3 bucket name. +// IsBucket determines whether this prefix is a valid S3 bucket name. func (s *S3Resolver) IsBucket(name string) bool { result, err := s.resolveCNAME(fmt.Sprintf("%s.%s.", name, s3host)) @@ -52,18 +53,18 @@ func getConfig(nameserver string) (*dns.ClientConfig, error) { Servers: []string{addr.String()}, Port: "53", }, nil - } else { - return nil, errors.New("invalid ip addr") } - } else { - config, err := dns.ClientConfigFromFile("/etc/resolv.conf") - if err != nil { - return nil, errors.New("could not read local resolver config") - } + return nil, errors.New("invalid ip addr") + } + + config, err := dns.ClientConfigFromFile("/etc/resolv.conf") - return config, nil + if err != nil { + return nil, errors.New("could not read local resolver config") } + + return config, nil } func (s *S3Resolver) resolveCNAME(name string) (string, error) { diff --git a/consumer.go b/consumer.go index c33b7b9..f953522 100644 --- a/consumer.go +++ b/consumer.go @@ -9,7 +9,7 @@ type Consumer struct { } // NewConsumer initializer -func NewConsumer(resolver Resolver, input chan string, result chan string, quit chan bool) (*Consumer, error) { +func NewConsumer(resolver Resolver, input chan string, result chan string, quit chan bool) *Consumer { consumer := &Consumer{ resolver: resolver, inputChannel: input, @@ -17,7 +17,7 @@ func NewConsumer(resolver Resolver, input chan string, result chan string, quit quit: quit, } - return consumer, nil + return consumer } // Consume reads messages from 'input', and outputs results to 'result'. diff --git a/consumer_test.go b/consumer_test.go index 778c34b..20abee1 100644 --- a/consumer_test.go +++ b/consumer_test.go @@ -24,10 +24,7 @@ func TestConsume(t *testing.T) { done3 := make(chan bool) resolver := NewTestResolver() - consumer, err := NewConsumer(resolver, inputChannel, resultChannel, done2) - if err != nil { - t.Errorf("fail!") - } + consumer := NewConsumer(resolver, inputChannel, resultChannel, done2) go consumer.Consume() diff --git a/main.go b/main.go index 5bc8dde..2f5b59b 100644 --- a/main.go +++ b/main.go @@ -2,16 +2,9 @@ package main import ( "fmt" - "github.com/docopt/docopt-go" "os" -) -var ( - threads int - names []string - wordListFile string - preAndSuffixesFile string - nameserver string + "github.com/docopt/docopt-go" ) const version = "0.0.1" @@ -40,11 +33,12 @@ func main() { os.Exit(0) } - names = opts[""].([]string) - preAndSuffixesFile = opts["--suffixlist"].(string) - wordListFile = opts["--wordlist"].(string) - threads, _ = opts.Int("--threads") + names := opts[""].([]string) + preAndSuffixesFile := opts["--suffixlist"].(string) + wordListFile := opts["--wordlist"].(string) + threads, _ := opts.Int("--threads") + var nameserver string if opts["--nameserver"] == nil { nameserver = "" } else { @@ -63,21 +57,12 @@ func main() { os.Exit(1) } - consumer, err := NewConsumer(resolver, wordChannel, resultChannel, wordDone) - if err != nil { - fmt.Fprintf(os.Stderr, "Could not initialize Consumer: %v\n", err) - os.Exit(1) - } - + consumer := NewConsumer(resolver, wordChannel, resultChannel, wordDone) for i := 0; i < threads; i++ { go consumer.Consume() } - printer, err := NewPrinter(resultChannel, resultDone) - if err != nil { - fmt.Fprintf(os.Stderr, "Could not initialize Printer: %v\n", err) - os.Exit(1) - } + printer := NewPrinter(resultChannel, resultDone, os.Stdout) go printer.PrintBuckets() producer, err := NewProducer(preAndSuffixesFile, wordChannel, resultDone) diff --git a/printer.go b/printer.go index a35056d..496370b 100644 --- a/printer.go +++ b/printer.go @@ -3,27 +3,24 @@ package main import ( "fmt" "io" - "os" -) - -var ( - out io.Writer = os.Stdout // substituted during testing ) // Printer struct type Printer struct { channel chan string done chan bool + log io.Writer } // NewPrinter initializer -func NewPrinter(channel chan string, done chan bool) (*Printer, error) { +func NewPrinter(channel chan string, done chan bool, log io.Writer) *Printer { printer := &Printer{ channel: channel, done: done, + log: log, } - return printer, nil + return printer } // PrintBuckets prints the results as they come in. @@ -31,7 +28,7 @@ func (c *Printer) PrintBuckets() { for { bucket, more := <-c.channel if more { - fmt.Fprintf(out, "%s\n", bucket) + fmt.Fprintf(c.log, "%s\n", bucket) } else { c.done <- true return diff --git a/printer_test.go b/printer_test.go index 606ef75..5bec368 100644 --- a/printer_test.go +++ b/printer_test.go @@ -10,15 +10,11 @@ func TestPrintResults(t *testing.T) { channel := make(chan string) done := make(chan bool) - printer, err := NewPrinter(channel, done) - if err != nil { - t.Errorf("failed to initialize the Printer") - } + log := new(bytes.Buffer) + printer := NewPrinter(channel, done, log) go printer.PrintBuckets() - out = new(bytes.Buffer) // replace 'out' in order to capture the output - // produce some test results to the results channel for i := 1; i <= 5; i++ { channel <- fmt.Sprintf("test%v", i) @@ -33,7 +29,7 @@ func TestPrintResults(t *testing.T) { "test4\n" + "test5\n" - got := out.(*bytes.Buffer).String() + got := printer.log.(*bytes.Buffer).String() if got != expected { t.Errorf("expected %q, got %q", expected, got) }