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.
This commit is contained in:
juanvallejo 2018-10-30 13:20:26 -04:00
parent 76234a31b0
commit 901c798681
No known key found for this signature in database
GPG Key ID: 7D2C958002D6448D
2 changed files with 16 additions and 8 deletions

View File

@ -9,7 +9,6 @@ go_library(
"//pkg/kubectl/cmd/util:go_default_library", "//pkg/kubectl/cmd/util:go_default_library",
"//pkg/kubectl/util/i18n:go_default_library", "//pkg/kubectl/util/i18n:go_default_library",
"//pkg/kubectl/util/templates: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", "//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library", "//vendor/github.com/spf13/cobra:go_default_library",
], ],

View File

@ -17,6 +17,7 @@ limitations under the License.
package plugin package plugin
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -26,7 +27,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/util/i18n" "k8s.io/kubernetes/pkg/kubectl/util/i18n"
@ -108,11 +108,12 @@ func (o *PluginListOptions) Run() error {
pluginsFound := false pluginsFound := false
isFirstFile := true isFirstFile := true
pluginErrors := []error{}
pluginWarnings := 0 pluginWarnings := 0
paths := sets.NewString(filepath.SplitList(os.Getenv(path))...) for _, dir := range filepath.SplitList(os.Getenv(path)) {
for _, dir := range paths.List() {
files, err := ioutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
pluginErrors = append(pluginErrors, fmt.Errorf("error: unable to read directory %q in your PATH: %v", dir, err))
continue continue
} }
@ -146,15 +147,23 @@ func (o *PluginListOptions) Run() error {
} }
if !pluginsFound { 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 { if pluginWarnings > 0 {
fmt.Fprintln(o.ErrOut)
if pluginWarnings == 1 { 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 return nil