Skip to content

Commit

Permalink
Merge pull request #17 from Lingrino/search
Browse files Browse the repository at this point in the history
Search functions
  • Loading branch information
lingrino authored May 31, 2018
2 parents f4d01a6 + c49bbf5 commit bac6483
Show file tree
Hide file tree
Showing 21 changed files with 368 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exported functions are still subject to change.
- [x] Path Copy
- [x] Path Move
- [x] Path Update
- [ ] Path Search
- [x] Path Search
- [ ] Path Diff
- [x] Folder List
- [x] Folder Read
Expand All @@ -29,7 +29,7 @@ exported functions are still subject to change.
- [ ] Folder Destroy (v2 mounts only)
- [x] Folder Copy
- [x] Folder Move
- [ ] Folder Search
- [x] Folder Search
- [ ] Folder Diff
- [ ] Folder Map
- [ ] Policy Enforce
Expand Down
1 change: 0 additions & 1 deletion vaku/folder_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/folder_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/folder_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/folder_move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/folder_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
113 changes: 113 additions & 0 deletions vaku/folder_search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package vaku

import (
"sort"
"strings"

"github.com/pkg/errors"
)

// folderSearchWorkerInput takes input/output channels for input to the job
type folderSearchWorkerInput struct {
inputsC <-chan *PathInput
searchS string
resultsC chan<- *folderSearchWorkerOutput
}

// folderSearchWorkerOutput is the data returned by a FolderSearch worker
type folderSearchWorkerOutput struct {
path string
match bool
err error
}

// FolderSearch takes in a PathInput and a search string. It then calls FolderList() on that path
// and concurrently runs PathSearch() on every returned path in the list. It returns a list of paths
// at which the search string was found. Note that running this function against a path that is not
// a folder will produce an error, to search a single path you should use PathSearch() instead.
func (c *Client) FolderSearch(i *PathInput, s string) ([]string, error) {
var err error
var output []string

// Init the path to get mount info
c.InitPathInput(i)

// Get all of the paths to search
list, err := c.FolderList(&PathInput{
Path: i.Path,
TrimPathPrefix: true,
})
if err != nil {
return output, errors.Wrapf(err, "failed to list folder at %s", i.Path)
}

// Concurrency channels for workers
inputsC := make(chan *PathInput, len(list))
resultsC := make(chan *folderSearchWorkerOutput, len(list))

// Spawn workers equal to MaxConcurrency
for w := 1; w <= MaxConcurrency; w++ {
go c.folderSearchWorker(&folderSearchWorkerInput{
inputsC: inputsC,
searchS: s,
resultsC: resultsC,
})
}

// Add all paths to search to the path channel
for _, p := range list {
inputsC <- &PathInput{
Path: c.PathJoin(i.Path, p),
mountPath: i.mountPath,
mountlessPath: i.mountlessPath,
mountVersion: i.mountVersion,
}
}
close(inputsC)

// Empty the results channel into output
for j := 0; j < len(list); j++ {
o := <-resultsC
if o.err != nil {
err = errors.Wrapf(o.err, "Failed to search path %s", o.path)
}
if o.match {
if i.TrimPathPrefix {
outputPath := c.PathJoin(strings.TrimPrefix(o.path, i.Path))
output = append(output, outputPath)
} else {
output = append(output, o.path)
}
}
}

sort.Strings(output)

return output, err
}

// folderSearchWorker does the work of searching a single path
func (c *Client) folderSearchWorker(i *folderSearchWorkerInput) {
for {
input, more := <-i.inputsC
if more {
match, err := c.PathSearch(input, i.searchS)
output := &folderSearchWorkerOutput{
err: err,
path: input.Path,
match: false,
}
if err != nil {
output.err = errors.Wrapf(err, "Failed to search path %s", input.Path)
i.resultsC <- output
continue
}
if match {
output.match = true
}
i.resultsC <- output
} else {
return
}
}
}
109 changes: 109 additions & 0 deletions vaku/folder_search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package vaku_test

import (
"testing"

"github.com/Lingrino/vaku/vaku"
"github.com/stretchr/testify/assert"
)

type TestFolderSearchData struct {
inputPath *vaku.PathInput
inputSearch string
output []string
outputErr bool
}

func TestFolderSearch(t *testing.T) {
t.Parallel()
c := clientInitForTests(t)

tests := map[int]TestFolderSearchData{
1: {
inputPath: vaku.NewPathInput("secretv1/test"),
inputSearch: "ba",
output: []string{
"fizz",
"foo",
"value",
},
outputErr: false,
},
2: {
inputPath: vaku.NewPathInput("secretv2/test"),
inputSearch: "ba",
output: []string{
"fizz",
"foo",
"value",
},
outputErr: false,
},
3: {
inputPath: vaku.NewPathInput("secretv1/test/inner"),
inputSearch: "rs1mCRB5",
output: []string{
"again/inner/UCrt6sZT",
},
outputErr: false,
},
4: {
inputPath: vaku.NewPathInput("secretv2/test/inner"),
inputSearch: "rs1mCRB5",
output: []string{
"again/inner/UCrt6sZT",
},
outputErr: false,
},
5: {
inputPath: &vaku.PathInput{
Path: "secretv1/test",
TrimPathPrefix: false,
},
inputSearch: "VbJ",
output: []string{
"secretv1/test/HToOeKKD",
},
outputErr: false,
},
6: {
inputPath: &vaku.PathInput{
Path: "secretv2/test",
TrimPathPrefix: false,
},
inputSearch: "VbJ",
output: []string{
"secretv2/test/HToOeKKD",
},
outputErr: false,
},
7: {
inputPath: vaku.NewPathInput("secretv1/doesnotexist"),
inputSearch: "foo",
output: nil,
outputErr: true,
},
8: {
inputPath: vaku.NewPathInput("secretv2/doesnotexist"),
inputSearch: "foo",
output: nil,
outputErr: true,
},
9: {
inputPath: vaku.NewPathInput("secretdoesnotexist/test"),
inputSearch: "foo",
output: nil,
outputErr: true,
},
}

for _, d := range tests {
o, e := c.FolderSearch(d.inputPath, d.inputSearch)
assert.Equal(t, d.output, o)
if d.outputErr {
assert.Error(t, e)
} else {
assert.NoError(t, e)
}
}
}
1 change: 0 additions & 1 deletion vaku/folder_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion vaku/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"
"github.com/pkg/errors"

vapi "github.com/hashicorp/vault/api"
"github.com/pkg/errors"
)

var seededOnce = false
Expand Down
1 change: 0 additions & 1 deletion vaku/mount_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/path_copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/path_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/path_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/path_move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
1 change: 0 additions & 1 deletion vaku/path_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/Lingrino/vaku/vaku"

"github.com/stretchr/testify/assert"
)

Expand Down
Loading

0 comments on commit bac6483

Please sign in to comment.