make sure a plugin command name does not already exist

Signed-off-by: SataQiu <qiushida@beyondcent.com>
pull/5195/head
SataQiu 7 years ago
parent 4c226b67f9
commit c2c16fceb4

@ -44,6 +44,8 @@ var (
settings helm_env.EnvSettings settings helm_env.EnvSettings
) )
var RootCmd *cobra.Command
var globalUsage = `The Kubernetes package manager var globalUsage = `The Kubernetes package manager
To begin working with Helm, run the 'helm init' command: To begin working with Helm, run the 'helm init' command:
@ -157,6 +159,8 @@ func newRootCmd(args []string) *cobra.Command {
// Find and add plugins // Find and add plugins
loadPlugins(cmd, out) loadPlugins(cmd, out)
RootCmd = cmd
return cmd return cmd
} }

@ -23,22 +23,12 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"syscall" "syscall"
"text/template"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/helm/pkg/plugin" "k8s.io/helm/pkg/plugin"
) )
var pluginLoadWarningTemp = template.Must(template.New("plugin").Parse(`
Helm Client Plugin Load Warning(s):
{{range $_, $warning := .Warnings}}
- {{ $warning }}
{{end}}
In order to eliminate this warning(s), please try to uninstall or reinstall the plugin(s) in question.
------------------------------------------------------------------------------------------------------
`))
type pluginError struct { type pluginError struct {
error error
code int code int
@ -72,8 +62,6 @@ func loadPlugins(baseCmd *cobra.Command, out io.Writer) {
} }
// Now we create commands for all of these. // Now we create commands for all of these.
warnings := []string{}
loop:
for _, plug := range found { for _, plug := range found {
plug := plug plug := plug
md := plug.Metadata md := plug.Metadata
@ -129,22 +117,9 @@ loop:
} }
} }
// Make sure a command with this name does not already exist. // TODO: Make sure a command with this name does not already exist.
for _, cmd := range baseCmd.Commands() {
if cmd.Name() == md.Name {
warning := fmt.Sprintf("A command with the name of [%s] already exists, so the plugin at [%s] will not be loaded.", md.Name, plug.Dir)
warnings = append(warnings, warning)
continue loop
}
}
baseCmd.AddCommand(c) baseCmd.AddCommand(c)
} }
ctx := map[string]interface{}{
"Warnings": warnings,
}
pluginLoadWarningTemp.Execute(out, ctx)
} }
// manuallyProcessArgs processes an arg array, removing special args. // manuallyProcessArgs processes an arg array, removing special args.

@ -18,6 +18,7 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"k8s.io/helm/pkg/helm/helmpath" "k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/plugin" "k8s.io/helm/pkg/plugin"
@ -83,10 +84,20 @@ func (pcmd *pluginInstallCmd) run() error {
return err return err
} }
// Make sure a command with this name does not already exist.
pluginCmdName := p.Metadata.Name
for _, cmd := range RootCmd.Commands() {
if cmd.Name() == pluginCmdName {
os.Remove(p.Dir)
err = fmt.Errorf("Plugin <%s> not installed as plugin with that name is already installed.", pluginCmdName)
return err
}
}
if err := runHook(p, plugin.Install); err != nil { if err := runHook(p, plugin.Install); err != nil {
return err return err
} }
fmt.Fprintf(pcmd.out, "Installed plugin: %s\n", p.Metadata.Name) fmt.Fprintf(pcmd.out, "Installed plugin: %s\n", pluginCmdName)
return nil return nil
} }

Loading…
Cancel
Save