From 901c7986816c7a4ff80485385b41ceaf2567929f Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Tue, 30 Oct 2018 13:20:26 -0400 Subject: [PATCH] prevent sorting of PATH dirs This patch allows the `kubectl plugin list` command to display discovered plugin paths in the same order as they appear in a user's PATH. Prior to this patch, discovered plugin paths were sorted before being displayed. Additionally, any errors encountered while reading from any directory in a user's PATH will now be printed to stderr at the end of the command's output. --- pkg/kubectl/cmd/plugin/BUILD | 1 - pkg/kubectl/cmd/plugin/plugin.go | 23 ++++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/kubectl/cmd/plugin/BUILD b/pkg/kubectl/cmd/plugin/BUILD index 88174ca29aa..fb6091de395 100644 --- a/pkg/kubectl/cmd/plugin/BUILD +++ b/pkg/kubectl/cmd/plugin/BUILD @@ -9,7 +9,6 @@ go_library( "//pkg/kubectl/cmd/util:go_default_library", "//pkg/kubectl/util/i18n:go_default_library", "//pkg/kubectl/util/templates:go_default_library", - "//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library", "//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library", "//vendor/github.com/spf13/cobra:go_default_library", ], diff --git a/pkg/kubectl/cmd/plugin/plugin.go b/pkg/kubectl/cmd/plugin/plugin.go index 71b11d697e4..528ede0e70c 100644 --- a/pkg/kubectl/cmd/plugin/plugin.go +++ b/pkg/kubectl/cmd/plugin/plugin.go @@ -17,6 +17,7 @@ limitations under the License. package plugin import ( + "bytes" "fmt" "io/ioutil" "os" @@ -26,7 +27,6 @@ import ( "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/util/sets" "k8s.io/cli-runtime/pkg/genericclioptions" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/util/i18n" @@ -108,11 +108,12 @@ func (o *PluginListOptions) Run() error { pluginsFound := false isFirstFile := true + pluginErrors := []error{} pluginWarnings := 0 - paths := sets.NewString(filepath.SplitList(os.Getenv(path))...) - for _, dir := range paths.List() { + for _, dir := range filepath.SplitList(os.Getenv(path)) { files, err := ioutil.ReadDir(dir) if err != nil { + pluginErrors = append(pluginErrors, fmt.Errorf("error: unable to read directory %q in your PATH: %v", dir, err)) continue } @@ -146,15 +147,23 @@ func (o *PluginListOptions) Run() error { } if !pluginsFound { - return fmt.Errorf("error: unable to find any kubectl plugins in your PATH") + pluginErrors = append(pluginErrors, fmt.Errorf("error: unable to find any kubectl plugins in your PATH")) } if pluginWarnings > 0 { - fmt.Fprintln(o.ErrOut) if pluginWarnings == 1 { - return fmt.Errorf("one plugin warning was found") + pluginErrors = append(pluginErrors, fmt.Errorf("error: one plugin warning was found")) + } else { + pluginErrors = append(pluginErrors, fmt.Errorf("error: %v plugin warnings were found", pluginWarnings)) } - return fmt.Errorf("%v plugin warnings were found", pluginWarnings) + } + if len(pluginErrors) > 0 { + fmt.Fprintln(o.ErrOut) + errs := bytes.NewBuffer(nil) + for _, e := range pluginErrors { + fmt.Fprintln(errs, e) + } + return fmt.Errorf("%s", errs.String()) } return nil