github.com/phroggyy/helm@v3.0.0-beta.3+incompatible/cmd/helm/plugin.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 main
    17  
    18  import (
    19  	"io"
    20  	"os"
    21  	"os/exec"
    22  
    23  	"github.com/pkg/errors"
    24  	"github.com/spf13/cobra"
    25  
    26  	"helm.sh/helm/pkg/plugin"
    27  )
    28  
    29  const pluginHelp = `
    30  Manage client-side Helm plugins.
    31  `
    32  
    33  func newPluginCmd(out io.Writer) *cobra.Command {
    34  	cmd := &cobra.Command{
    35  		Use:   "plugin",
    36  		Short: "add, list, or remove Helm plugins",
    37  		Long:  pluginHelp,
    38  	}
    39  	cmd.AddCommand(
    40  		newPluginInstallCmd(out),
    41  		newPluginListCmd(out),
    42  		newPluginRemoveCmd(out),
    43  		newPluginUpdateCmd(out),
    44  	)
    45  	return cmd
    46  }
    47  
    48  // runHook will execute a plugin hook.
    49  func runHook(p *plugin.Plugin, event string) error {
    50  	hook := p.Metadata.Hooks[event]
    51  	if hook == "" {
    52  		return nil
    53  	}
    54  
    55  	prog := exec.Command("sh", "-c", hook)
    56  	// TODO make this work on windows
    57  	// I think its ... ¯\_(ツ)_/¯
    58  	// prog := exec.Command("cmd", "/C", p.Metadata.Hooks.Install())
    59  
    60  	debug("running %s hook: %s", event, prog)
    61  
    62  	plugin.SetupPluginEnv(settings, p.Metadata.Name, p.Dir)
    63  	prog.Stdout, prog.Stderr = os.Stdout, os.Stderr
    64  	if err := prog.Run(); err != nil {
    65  		if eerr, ok := err.(*exec.ExitError); ok {
    66  			os.Stderr.Write(eerr.Stderr)
    67  			return errors.Errorf("plugin %s hook for %q exited with error", event, p.Metadata.Name)
    68  		}
    69  		return err
    70  	}
    71  	return nil
    72  }