github.com/zoumo/helm@v2.5.0+incompatible/pkg/plugin/installer/installer.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"errors"
    20  	"fmt"
    21  	"os"
    22  	"path"
    23  	"path/filepath"
    24  
    25  	"k8s.io/helm/pkg/helm/helmpath"
    26  )
    27  
    28  // ErrMissingMetadata indicates that plugin.yaml is missing.
    29  var ErrMissingMetadata = errors.New("plugin metadata (plugin.yaml) missing")
    30  
    31  // Debug enables verbose output.
    32  var Debug bool
    33  
    34  // Installer provides an interface for installing helm client plugins.
    35  type Installer interface {
    36  	// Install adds a plugin to $HELM_HOME.
    37  	Install() error
    38  	// Path is the directory of the installed plugin.
    39  	Path() string
    40  	// Update updates a plugin to $HELM_HOME.
    41  	Update() error
    42  }
    43  
    44  // Install installs a plugin to $HELM_HOME.
    45  func Install(i Installer) error {
    46  	if _, pathErr := os.Stat(path.Dir(i.Path())); os.IsNotExist(pathErr) {
    47  		return errors.New(`plugin home "$HELM_HOME/plugins" does not exist`)
    48  	}
    49  
    50  	if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) {
    51  		return errors.New("plugin already exists")
    52  	}
    53  
    54  	return i.Install()
    55  }
    56  
    57  // Update updates a plugin in $HELM_HOME.
    58  func Update(i Installer) error {
    59  	if _, pathErr := os.Stat(i.Path()); os.IsNotExist(pathErr) {
    60  		return errors.New("plugin does not exist")
    61  	}
    62  
    63  	return i.Update()
    64  }
    65  
    66  // NewForSource determines the correct Installer for the given source.
    67  func NewForSource(source, version string, home helmpath.Home) (Installer, error) {
    68  	// Check if source is a local directory
    69  	if isLocalReference(source) {
    70  		return NewLocalInstaller(source, home)
    71  	}
    72  	return NewVCSInstaller(source, version, home)
    73  }
    74  
    75  // FindSource determines the correct Installer for the given source.
    76  func FindSource(location string, home helmpath.Home) (Installer, error) {
    77  	installer, err := existingVCSRepo(location, home)
    78  	if err != nil && err.Error() == "Cannot detect VCS" {
    79  		return installer, errors.New("cannot get information about plugin source")
    80  	}
    81  	return installer, err
    82  }
    83  
    84  // isLocalReference checks if the source exists on the filesystem.
    85  func isLocalReference(source string) bool {
    86  	_, err := os.Stat(source)
    87  	return err == nil
    88  }
    89  
    90  // isPlugin checks if the directory contains a plugin.yaml file.
    91  func isPlugin(dirname string) bool {
    92  	_, err := os.Stat(filepath.Join(dirname, "plugin.yaml"))
    93  	return err == nil
    94  }
    95  
    96  func debug(format string, args ...interface{}) {
    97  	if Debug {
    98  		format = fmt.Sprintf("[debug] %s\n", format)
    99  		fmt.Printf(format, args...)
   100  	}
   101  }