forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_buildpack.go
65 lines (54 loc) · 1.95 KB
/
pull_buildpack.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
56
57
58
59
60
61
62
63
64
65
package pack
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/style"
)
// PullBuildpackOptions are options available for PullBuildpack
type PullBuildpackOptions struct {
// URI of the buildpack to retrieve.
URI string
// RegistryName to search for buildpacks from.
RegistryName string
// RelativeBaseDir to resolve relative assests from.
RelativeBaseDir string
}
// PullBuildpack pulls given buildpack to be stored locally
func (c *Client) PullBuildpack(ctx context.Context, opts PullBuildpackOptions) error {
locatorType, err := buildpack.GetLocatorType(opts.URI, "", []dist.BuildpackInfo{})
if err != nil {
return err
}
switch locatorType {
case buildpack.PackageLocator:
imageName := buildpack.ParsePackageLocator(opts.URI)
c.logger.Debugf("Pulling buildpack from image: %s", imageName)
_, err = c.imageFetcher.Fetch(ctx, imageName, true, config.PullAlways)
if err != nil {
return errors.Wrapf(err, "fetching image %s", style.Symbol(opts.URI))
}
case buildpack.RegistryLocator:
c.logger.Debugf("Pulling buildpack from registry: %s", style.Symbol(opts.URI))
registryCache, err := c.getRegistry(c.logger, opts.RegistryName)
if err != nil {
return errors.Wrapf(err, "invalid registry '%s'", opts.RegistryName)
}
registryBp, err := registryCache.LocateBuildpack(opts.URI)
if err != nil {
return errors.Wrapf(err, "locating in registry %s", style.Symbol(opts.URI))
}
_, err = c.imageFetcher.Fetch(ctx, registryBp.Address, true, config.PullAlways)
if err != nil {
return errors.Wrapf(err, "fetching image %s", style.Symbol(opts.URI))
}
case buildpack.InvalidLocator:
return fmt.Errorf("invalid buildpack URI %s", style.Symbol(opts.URI))
default:
return fmt.Errorf("unsupported buildpack URI type: %s", style.Symbol(locatorType.String()))
}
return nil
}