github.com/azure/draft-classic@v0.16.0/pkg/draft/pack/repo/installer/installer.go (about)

     1  package installer
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/Azure/draft/pkg/draft/draftpath"
     8  	"github.com/Azure/draft/pkg/draft/pack/repo"
     9  	"github.com/Azure/draft/pkg/plugin/installer"
    10  )
    11  
    12  type base struct {
    13  	// Source is the reference to a pack repo
    14  	Source string
    15  
    16  	// DraftHome is the $DRAFT_HOME directory
    17  	DraftHome draftpath.Home
    18  }
    19  
    20  func newBase(source string, home draftpath.Home) base {
    21  	return base{source, home}
    22  }
    23  
    24  // isPackRepo checks if the directory contains a packs directory.
    25  func isPackRepo(dirname string) bool {
    26  	fi, err := os.Stat(filepath.Join(dirname, "packs"))
    27  	return err == nil && fi.IsDir()
    28  }
    29  
    30  // isLocalReference checks if the source exists on the filesystem.
    31  func isLocalReference(source string) bool {
    32  	_, err := os.Stat(source)
    33  	return err == nil
    34  }
    35  
    36  // Install installs a pack repo to $DRAFT_HOME
    37  func Install(i installer.Installer) error {
    38  	if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) {
    39  		return repo.ErrExists
    40  	}
    41  
    42  	return i.Install()
    43  }
    44  
    45  // Update updates a pack repo in $DRAFT_HOME.
    46  func Update(i installer.Installer) error {
    47  	if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) {
    48  		return repo.ErrDoesNotExist
    49  	}
    50  
    51  	return i.Update()
    52  }
    53  
    54  // FindSource determines the correct Installer for the given source.
    55  func FindSource(location string, home draftpath.Home) (installer.Installer, error) {
    56  	installer, err := existingVCSRepo(location, home)
    57  	if err != nil && err.Error() == "Cannot detect VCS" {
    58  		return installer, repo.ErrMissingSource
    59  	}
    60  	return installer, err
    61  }
    62  
    63  // New determines and returns the correct Installer for the given source
    64  func New(source, version string, home draftpath.Home) (installer.Installer, error) {
    65  	if isLocalReference(source) {
    66  		return NewLocalInstaller(source, home)
    67  	}
    68  
    69  	return NewVCSInstaller(source, version, home)
    70  }