github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/pull_buildpack.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/buildpacks/pack/internal/style"
    10  	"github.com/buildpacks/pack/pkg/buildpack"
    11  	"github.com/buildpacks/pack/pkg/dist"
    12  	"github.com/buildpacks/pack/pkg/image"
    13  )
    14  
    15  // PullBuildpackOptions are options available for PullBuildpack
    16  type PullBuildpackOptions struct {
    17  	// URI of the buildpack to retrieve.
    18  	URI string
    19  	// RegistryName to search for buildpacks from.
    20  	RegistryName string
    21  	// RelativeBaseDir to resolve relative assests from.
    22  	RelativeBaseDir string
    23  }
    24  
    25  // PullBuildpack pulls given buildpack to be stored locally
    26  func (c *Client) PullBuildpack(ctx context.Context, opts PullBuildpackOptions) error {
    27  	locatorType, err := buildpack.GetLocatorType(opts.URI, "", []dist.ModuleInfo{})
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	switch locatorType {
    33  	case buildpack.PackageLocator:
    34  		imageName := buildpack.ParsePackageLocator(opts.URI)
    35  		c.logger.Debugf("Pulling buildpack from image: %s", imageName)
    36  
    37  		_, err = c.imageFetcher.Fetch(ctx, imageName, image.FetchOptions{Daemon: true, PullPolicy: image.PullAlways})
    38  		if err != nil {
    39  			return errors.Wrapf(err, "fetching image %s", style.Symbol(opts.URI))
    40  		}
    41  	case buildpack.RegistryLocator:
    42  		c.logger.Debugf("Pulling buildpack from registry: %s", style.Symbol(opts.URI))
    43  		registryCache, err := getRegistry(c.logger, opts.RegistryName)
    44  
    45  		if err != nil {
    46  			return errors.Wrapf(err, "invalid registry '%s'", opts.RegistryName)
    47  		}
    48  
    49  		registryBp, err := registryCache.LocateBuildpack(opts.URI)
    50  		if err != nil {
    51  			return errors.Wrapf(err, "locating in registry %s", style.Symbol(opts.URI))
    52  		}
    53  
    54  		_, err = c.imageFetcher.Fetch(ctx, registryBp.Address, image.FetchOptions{Daemon: true, PullPolicy: image.PullAlways})
    55  		if err != nil {
    56  			return errors.Wrapf(err, "fetching image %s", style.Symbol(opts.URI))
    57  		}
    58  	case buildpack.InvalidLocator:
    59  		return fmt.Errorf("invalid buildpack URI %s", style.Symbol(opts.URI))
    60  	default:
    61  		return fmt.Errorf("unsupported buildpack URI type: %s", style.Symbol(locatorType.String()))
    62  	}
    63  
    64  	return nil
    65  }