forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindEntry.go
39 lines (34 loc) · 1.01 KB
/
findEntry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package plugin
import (
"context"
"fmt"
"strings"
)
// FindEntry returns the child of start found by following the segments, or an error if it cannot be found.
func FindEntry(ctx context.Context, start Entry, segments []string) (Entry, error) {
visitedSegments := make([]string, 0, cap(segments))
for _, segment := range segments {
switch curParent := start.(type) {
case Parent:
// Get the entries via. List()
entries, err := List(ctx, curParent)
if err != nil {
return nil, err
}
// Search for the specific entry
entry, ok := entries.Load(segment)
if !ok {
reason := fmt.Sprintf("The %v entry does not exist", segment)
if len(visitedSegments) != 0 {
reason += fmt.Sprintf(" in the %v parent", strings.Join(visitedSegments, "/"))
}
return nil, fmt.Errorf(reason)
}
start = entry
visitedSegments = append(visitedSegments, segment)
default:
return nil, fmt.Errorf("The entry %v is not a parent", strings.Join(visitedSegments, "/"))
}
}
return start, nil
}