Repository navigation
Expand file tree
/
Copy pathlookup.go
More file actions
65 lines (57 loc) · 1.66 KB
/
Copy pathlookup.go
File metadata and controls
65 lines (57 loc) · 1.66 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package dotenv
import (
"os"
"sort"
)
// LookupFn is a function that looks up a variable by name and returns the Variable and whether it was found
type LookupFn func(string) (Variable, bool)
// CompositeLookup manages multiple LookupFn with explicit priorities
type CompositeLookup struct {
lookups []prioritizedLookup
}
func WithPriority(Lookup LookupFn, priority int) prioritizedLookup {
return prioritizedLookup{
Lookup: Lookup,
Priority: priority,
}
}
// NewCompositeLookup creates a new CompositeLookup with the given prioritized lookup functions
// Higher priority values are tried first
func NewCompositeLookup(lookups ...prioritizedLookup) *CompositeLookup {
// Make a copy and sort by priority (highest first)
sorted := make([]prioritizedLookup, len(lookups))
copy(sorted, lookups)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Priority > sorted[j].Priority
})
return &CompositeLookup{
lookups: sorted,
}
}
// PrioritizedLookup associates a lookup function with a priority value
type prioritizedLookup struct {
Priority int
Lookup LookupFn
}
// Lookup implements the LookupFn signature by trying each lookup function in priority order
func (c *CompositeLookup) Lookup(name string) (Variable, bool) {
for _, pl := range c.lookups {
if v, ok := pl.Lookup(name); ok {
return v, true
}
}
return Variable{}, false
}
// Lookup implements the LookupFn signature by looking up variables in the OS environment
var OSEnv LookupFn = func(name string) (Variable, bool) {
value, ok := os.LookupEnv(name)
if !ok {
return Variable{}, false
}
return Variable{
Name: name,
Value: value,
RawValue: value,
Location: ":os",
}, true
}