github.com/rohankumardubey/draft-classic@v0.16.0/pkg/plugin/installer/installer.go (about) 1 package installer 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/Azure/draft/pkg/draft/draftpath" 9 "github.com/Azure/draft/pkg/plugin" 10 ) 11 12 // special thanks to the Kubernets Helm plugin installer pkg 13 14 // Debug enables verbose output. 15 var Debug bool 16 17 // Installer provides an interface for installing client plugins. 18 type Installer interface { 19 // Install adds a plugin to a path 20 Install() error 21 // Path is the directory of the installed plugin. 22 Path() string 23 // Update updates a plugin to $DRAFT_HOME. 24 Update() error 25 } 26 27 // Install installs a plugin to $DRAFT_HOME 28 func Install(i Installer) error { 29 if _, pathErr := os.Stat(filepath.Dir(i.Path())); os.IsNotExist(pathErr) { 30 31 return plugin.ErrHomeMissing 32 } 33 34 if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) { 35 return plugin.ErrExists 36 } 37 38 return i.Install() 39 } 40 41 // Update updates a plugin in $DRAFT_HOME. 42 func Update(i Installer) error { 43 if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) { 44 return plugin.ErrDoesNotExist 45 } 46 47 return i.Update() 48 } 49 50 // FindSource determines the correct Installer for the given source. 51 func FindSource(location string, home draftpath.Home) (Installer, error) { 52 installer, err := existingVCSRepo(location, home) 53 if err != nil && err.Error() == "Cannot detect VCS" { 54 return installer, plugin.ErrMissingSource 55 } 56 return installer, err 57 } 58 59 // New determines and returns the correct Installer for the given source 60 func New(source, version string, home draftpath.Home) (Installer, error) { 61 if isLocalReference(source) { 62 return NewLocalInstaller(source, home) 63 } 64 65 return NewVCSInstaller(source, version, home) 66 } 67 68 func debug(format string, args ...interface{}) { 69 if Debug { 70 format = fmt.Sprintf("[debug] %s\n", format) 71 fmt.Printf(format, args...) 72 } 73 } 74 75 // isLocalReference checks if the source exists on the filesystem. 76 func isLocalReference(source string) bool { 77 _, err := os.Stat(source) 78 return err == nil 79 } 80 81 // isPlugin checks if the directory contains a plugin.yaml file. 82 func isPlugin(dirname string) bool { 83 _, err := os.Stat(filepath.Join(dirname, "plugin.yaml")) 84 return err == nil 85 }