get.porter.sh/porter@v1.3.0/pkg/porter/resolver.go (about)

     1  package porter
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"get.porter.sh/porter/pkg/cache"
     8  	cnabtooci "get.porter.sh/porter/pkg/cnab/cnab-to-oci"
     9  	"get.porter.sh/porter/pkg/tracing"
    10  )
    11  
    12  type BundleResolver struct {
    13  	Cache    cache.BundleCache
    14  	Registry cnabtooci.RegistryProvider
    15  }
    16  
    17  // Resolves a bundle from the cache, or pulls it and caches it
    18  // Returns the location of the bundle or an error
    19  func (r *BundleResolver) Resolve(ctx context.Context, opts BundlePullOptions) (cache.CachedBundle, error) {
    20  	log := tracing.LoggerFromContext(ctx)
    21  
    22  	if !opts.Force {
    23  		cachedBundle, ok, err := r.Cache.FindBundle(opts.GetReference())
    24  		if err != nil {
    25  			return cache.CachedBundle{}, log.Error(fmt.Errorf("unable to load bundle %s from cache: %w", opts.Reference, err))
    26  		}
    27  		// If we found the bundle, return the path to the bundle.json
    28  		if ok {
    29  			return cachedBundle, nil
    30  		}
    31  	}
    32  
    33  	regOpts := cnabtooci.RegistryOptions{InsecureRegistry: opts.InsecureRegistry}
    34  	bundleRef, err := r.Registry.PullBundle(ctx, opts.GetReference(), regOpts)
    35  	if err != nil {
    36  		return cache.CachedBundle{}, err
    37  	}
    38  
    39  	cb, err := r.Cache.StoreBundle(bundleRef)
    40  	if err != nil {
    41  		return cache.CachedBundle{}, log.Errorf("error storing the bundle %s in the Porter bundle cache: %w", bundleRef, err)
    42  	}
    43  	return cb, nil
    44  }