github.com/liuming-dev/helm@v3.0.0-beta.3+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/filepath"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  // ErrMissingMetadata indicates that plugin.yaml is missing.
    28  var ErrMissingMetadata = errors.New("plugin metadata (plugin.yaml) missing")
    29  
    30  // Debug enables verbose output.
    31  var Debug bool
    32  
    33  // Installer provides an interface for installing helm client plugins.
    34  type Installer interface {
    35  	// Install adds a plugin.
    36  	Install() error
    37  	// Path is the directory of the installed plugin.
    38  	Path() string
    39  	// Update updates a plugin.
    40  	Update() error
    41  }
    42  
    43  // Install installs a plugin.
    44  func Install(i Installer) error {
    45  	if err := os.MkdirAll(filepath.Dir(i.Path()), 0755); err != nil {
    46  		return err
    47  	}
    48  	if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) {
    49  		return errors.New("plugin already exists")
    50  	}
    51  	return i.Install()
    52  }
    53  
    54  // Update updates a plugin.
    55  func Update(i Installer) error {
    56  	if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) {
    57  		return errors.New("plugin does not exist")
    58  	}
    59  	return i.Update()
    60  }
    61  
    62  // NewForSource determines the correct Installer for the given source.
    63  func NewForSource(source, version string) (Installer, error) {
    64  	// Check if source is a local directory
    65  	if isLocalReference(source) {
    66  		return NewLocalInstaller(source)
    67  	} else if isRemoteHTTPArchive(source) {
    68  		return NewHTTPInstaller(source)
    69  	}
    70  	return NewVCSInstaller(source, version)
    71  }
    72  
    73  // FindSource determines the correct Installer for the given source.
    74  func FindSource(location string) (Installer, error) {
    75  	installer, err := existingVCSRepo(location)
    76  	if err != nil && err.Error() == "Cannot detect VCS" {
    77  		return installer, errors.New("cannot get information about plugin source")
    78  	}
    79  	return installer, err
    80  }
    81  
    82  // isLocalReference checks if the source exists on the filesystem.
    83  func isLocalReference(source string) bool {
    84  	_, err := os.Stat(source)
    85  	return err == nil
    86  }
    87  
    88  // isRemoteHTTPArchive checks if the source is a http/https url and is an archive
    89  func isRemoteHTTPArchive(source string) bool {
    90  	if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") {
    91  		for suffix := range Extractors {
    92  			if strings.HasSuffix(source, suffix) {
    93  				return true
    94  			}
    95  		}
    96  	}
    97  	return false
    98  }
    99  
   100  // isPlugin checks if the directory contains a plugin.yaml file.
   101  func isPlugin(dirname string) bool {
   102  	_, err := os.Stat(filepath.Join(dirname, "plugin.yaml"))
   103  	return err == nil
   104  }
   105  
   106  func debug(format string, args ...interface{}) {
   107  	if Debug {
   108  		format = fmt.Sprintf("[debug] %s\n", format)
   109  		fmt.Printf(format, args...)
   110  	}
   111  }