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

Add GolangCI-Lint #28

Merged
merged 10 commits into from
Aug 19, 2019
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
8 changes: 8 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 11 additions & 10 deletions bucket_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"errors"
"fmt"
"github.com/miekg/dns"
"net"
"strings"

"github.com/miekg/dns"
)

type Resolver interface {
Expand Down Expand Up @@ -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))

Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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,
resultChannel: result,
quit: quit,
}

return consumer, nil
return consumer
}

// Consume reads messages from 'input', and outputs results to 'result'.
Expand Down
5 changes: 1 addition & 4 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
31 changes: 8 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -40,11 +33,12 @@ func main() {
os.Exit(0)
}

names = opts["<name>"].([]string)
preAndSuffixesFile = opts["--suffixlist"].(string)
wordListFile = opts["--wordlist"].(string)
threads, _ = opts.Int("--threads")
names := opts["<name>"].([]string)
preAndSuffixesFile := opts["--suffixlist"].(string)
wordListFile := opts["--wordlist"].(string)
threads, _ := opts.Int("--threads")

var nameserver string
if opts["--nameserver"] == nil {
nameserver = ""
} else {
Expand All @@ -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)
Expand Down
13 changes: 5 additions & 8 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,32 @@ 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.
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
Expand Down
10 changes: 3 additions & 7 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down