-
Notifications
You must be signed in to change notification settings - Fork 17
/
package_manager_configuration_manager.go
55 lines (43 loc) · 1.48 KB
/
package_manager_configuration_manager.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package npminstall
import (
"fmt"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/paketo-buildpacks/packit/v2/servicebindings"
)
//go:generate faux --interface BindingResolver --output fakes/binding_resolver.go
type BindingResolver interface {
Resolve(typ, provider, platformDir string) ([]servicebindings.Binding, error)
}
type PackageManagerConfigurationManager struct {
bindingResolver BindingResolver
logs scribe.Emitter
defaultPath string
}
func NewPackageManagerConfigurationManager(bindingResolver BindingResolver, logs scribe.Emitter, defaultPath string) PackageManagerConfigurationManager {
return PackageManagerConfigurationManager{
bindingResolver: bindingResolver,
logs: logs,
defaultPath: defaultPath,
}
}
func (p PackageManagerConfigurationManager) DeterminePath(typ, platformDir, entry string) (string, error) {
if p.defaultPath != "" {
return p.defaultPath, nil
}
bindings, err := p.bindingResolver.Resolve(typ, "", platformDir)
if err != nil {
return "", err
}
if len(bindings) > 1 {
return "", fmt.Errorf("failed: binding resolver found more than one binding of type '%s'", typ)
}
if len(bindings) == 1 {
p.logs.Process("Loading service binding of type '%s'", typ)
if _, ok := bindings[0].Entries[entry]; !ok {
return "", fmt.Errorf("failed: binding of type '%s' does not contain required entry '%s'", typ, entry)
}
return filepath.Join(bindings[0].Path, entry), nil
}
return "", nil
}