github.com/jdolitsky/cnab-go@v0.7.1-beta1/imagestore/construction/construction.go (about)

     1  package construction
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/deislabs/cnab-go/imagestore"
     8  	"github.com/deislabs/cnab-go/imagestore/ocilayout"
     9  	"github.com/deislabs/cnab-go/imagestore/remote"
    10  )
    11  
    12  // NewConstructor creates an image store constructor which will, if necessary, create archive contents.
    13  func NewConstructor(remoteRepos bool) (imagestore.Constructor, error) {
    14  	// infer the concrete type of the image store from the input parameters
    15  	if remoteRepos {
    16  		return remote.Create, nil
    17  	}
    18  	return ocilayout.Create, nil
    19  }
    20  
    21  // NewLocatingConstructor creates an image store constructor which will, if necessary, find existing archive contents.
    22  func NewLocatingConstructor() imagestore.Constructor {
    23  	return func(options ...imagestore.Option) (imagestore.Store, error) {
    24  		parms := imagestore.Create(options...)
    25  		if thin(parms.ArchiveDir) {
    26  			return remote.Create()
    27  		}
    28  		return ocilayout.LocateOciLayout(parms.ArchiveDir)
    29  	}
    30  }
    31  
    32  func thin(archiveDir string) bool {
    33  	// If there is no archive directory, the bundle is thin
    34  	if archiveDir == "" {
    35  		return true
    36  	}
    37  
    38  	// If there is an archive directory, the bundle is thin if and only if the archive directory has no artifacts/
    39  	// subdirectory
    40  	layoutDir := filepath.Join(archiveDir, "artifacts")
    41  	_, err := os.Stat(layoutDir)
    42  	return os.IsNotExist(err)
    43  }