From 0f607aa42ce3938b4ee78b49ae013fe394a020ac Mon Sep 17 00:00:00 2001 From: Daniel Dabrowski Date: Fri, 28 Jun 2019 14:57:57 +0200 Subject: [PATCH] chore(grep): pass grepResults by reference --- cli/grep.go | 11 +++++------ cli/grep_test.go | 2 +- s3/objects.go | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cli/grep.go b/cli/grep.go index 467df6f..3330658 100644 --- a/cli/grep.go +++ b/cli/grep.go @@ -24,10 +24,9 @@ func Grep(session *config.AWSSession, bucketName string, prefix string, query st objects := make(chan thisS3.StoredObject) listObjectsErrors := make(chan error) listObjectsDone := make(chan bool) - grepResults := make(chan grepResult) + grepResults := make(chan *grepResult) objectProcessed := make(chan bool) - objectsCount := 0 objectsProcessed := 0 allObjectsListed := false @@ -44,8 +43,8 @@ func Grep(session *config.AWSSession, bucketName string, prefix string, query st return case <-listObjectsDone: allObjectsListed = true - case result := <-grepResults: - fmt.Printf("s3://%s/%s %d:%s\n", bucketName, result.Key, result.LineNum, result.Excerpt) + case grepResult := <-grepResults: + fmt.Printf("s3://%s/%s %d:%s\n", bucketName, grepResult.Key, grepResult.LineNum, grepResult.Excerpt) case <-objectProcessed: objectsProcessed++ default: @@ -62,14 +61,14 @@ func Grep(session *config.AWSSession, bucketName string, prefix string, query st // Grep within the content of a single S3 object func grepInObjectContent(session *config.AWSSession, bucketName string, object thisS3.StoredObject, - query string, ignoreCase bool, results chan<- grepResult, processed chan<- bool) { + query string, ignoreCase bool, results chan<- *grepResult, processed chan<- bool) { content, numBytes, err := object.GetContent(session, bucketName) if err != nil { fmt.Printf("%s:%s\n", err, object.GetKey()) } else if numBytes > 0 { for i, line := range bytes.Split(content, []byte("\n")) { if caseAwareContains(line, []byte(query), ignoreCase) { - results <- grepResult{ + results <- &grepResult{ Key: object.GetKey(), LineNum: i + 1, Excerpt: getContentExcerpt(line, []byte(query)), diff --git a/cli/grep_test.go b/cli/grep_test.go index 14376ad..1322a74 100644 --- a/cli/grep_test.go +++ b/cli/grep_test.go @@ -109,7 +109,7 @@ func TestCaseAwareContains(t *testing.T) { } func TestGrepInObjectContent(t *testing.T) { - results := make(chan grepResult) + results := make(chan *grepResult) done := make(chan bool) testSession, err := config.NewAWSSession("testing") diff --git a/s3/objects.go b/s3/objects.go index c47e109..8145ffa 100644 --- a/s3/objects.go +++ b/s3/objects.go @@ -51,7 +51,7 @@ func NewObject(key string) StoredObject { // ListObjects lists all objects in the specified bucket func ListObjects(svc s3iface.S3API, bucketName string, prefix string, - objects chan<-StoredObject, listErrors chan<-error, done chan<-bool) { + objects chan<- StoredObject, listErrors chan<- error, done chan<- bool) { prefixExpression, err := regexp.Compile(fmt.Sprintf("^%s", strings.Trim(strings.TrimSpace(prefix), "/"))) if err != nil { listErrors <- errors.New("The provided prefix is not a valid regular expression")