Skip to content

Commit

Permalink
chore(grep): pass grepResults by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Dabrowski committed Jun 28, 2019
1 parent 1761766 commit 0f607aa
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions cli/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)),
Expand Down
2 changes: 1 addition & 1 deletion cli/grep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion s3/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 0f607aa

Please sign in to comment.