github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/plugin/installer/installer.go (about) 1 /* 2 Copyright The Helm Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package installer 17 18 import ( 19 "fmt" 20 "os" 21 "path" 22 "path/filepath" 23 "strings" 24 25 "github.com/pkg/errors" 26 27 "helm.sh/helm/pkg/helmpath" 28 ) 29 30 // ErrMissingMetadata indicates that plugin.yaml is missing. 31 var ErrMissingMetadata = errors.New("plugin metadata (plugin.yaml) missing") 32 33 // Debug enables verbose output. 34 var Debug bool 35 36 // Installer provides an interface for installing helm client plugins. 37 type Installer interface { 38 // Install adds a plugin to $HELM_HOME. 39 Install() error 40 // Path is the directory of the installed plugin. 41 Path() string 42 // Update updates a plugin to $HELM_HOME. 43 Update() error 44 } 45 46 // Install installs a plugin to $HELM_HOME. 47 func Install(i Installer) error { 48 if _, pathErr := os.Stat(path.Dir(i.Path())); os.IsNotExist(pathErr) { 49 return errors.New(`plugin home "$HELM_HOME/plugins" does not exist`) 50 } 51 52 if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) { 53 return errors.New("plugin already exists") 54 } 55 56 return i.Install() 57 } 58 59 // Update updates a plugin in $HELM_HOME. 60 func Update(i Installer) error { 61 if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) { 62 return errors.New("plugin does not exist") 63 } 64 65 return i.Update() 66 } 67 68 // NewForSource determines the correct Installer for the given source. 69 func NewForSource(source, version string, home helmpath.Home) (Installer, error) { 70 // Check if source is a local directory 71 if isLocalReference(source) { 72 return NewLocalInstaller(source, home) 73 } else if isRemoteHTTPArchive(source) { 74 return NewHTTPInstaller(source, home) 75 } 76 return NewVCSInstaller(source, version, home) 77 } 78 79 // FindSource determines the correct Installer for the given source. 80 func FindSource(location string, home helmpath.Home) (Installer, error) { 81 installer, err := existingVCSRepo(location, home) 82 if err != nil && err.Error() == "Cannot detect VCS" { 83 return installer, errors.New("cannot get information about plugin source") 84 } 85 return installer, err 86 } 87 88 // isLocalReference checks if the source exists on the filesystem. 89 func isLocalReference(source string) bool { 90 _, err := os.Stat(source) 91 return err == nil 92 } 93 94 // isRemoteHTTPArchive checks if the source is a http/https url and is an archive 95 func isRemoteHTTPArchive(source string) bool { 96 if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") { 97 for suffix := range Extractors { 98 if strings.HasSuffix(source, suffix) { 99 return true 100 } 101 } 102 } 103 return false 104 } 105 106 // isPlugin checks if the directory contains a plugin.yaml file. 107 func isPlugin(dirname string) bool { 108 _, err := os.Stat(filepath.Join(dirname, "plugin.yaml")) 109 return err == nil 110 } 111 112 func debug(format string, args ...interface{}) { 113 if Debug { 114 format = fmt.Sprintf("[debug] %s\n", format) 115 fmt.Printf(format, args...) 116 } 117 }