Merge pull request #6791 from eparis/bash-2-kubectl-get-resources

bash_completions: annotate kubectl get with resources
pull/6/head
Jeff Lowdermilk 2015-04-14 15:02:34 -07:00
commit 037407f49e
5 changed files with 36 additions and 1 deletions

2
Godeps/Godeps.json generated
View File

@ -391,7 +391,7 @@
},
{
"ImportPath": "github.com/spf13/cobra",
"Rev": "c0da825198c75814463e1b3018e42e438b771a5b"
"Rev": "9cb5e8502924a8ff1cce18a9348b61995d7b4fde"
},
{
"ImportPath": "github.com/spf13/pflag",

View File

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"github.com/spf13/pflag"
@ -282,6 +283,7 @@ func writeRequiredFlag(cmd *Command, out *bytes.Buffer) {
func writeRequiredNoun(cmd *Command, out *bytes.Buffer) {
fmt.Fprintf(out, " must_have_one_noun=()\n")
sort.Sort(sort.StringSlice(cmd.ValidArgs))
for _, value := range cmd.ValidArgs {
fmt.Fprintf(out, " must_have_one_noun+=(%q)\n", value)
}

View File

@ -242,6 +242,19 @@ _kubectl_get()
must_have_one_flag=()
must_have_one_noun=()
must_have_one_noun+=("endpoints")
must_have_one_noun+=("event")
must_have_one_noun+=("limitrange")
must_have_one_noun+=("namespace")
must_have_one_noun+=("node")
must_have_one_noun+=("persistentvolume")
must_have_one_noun+=("persistentvolumeclaim")
must_have_one_noun+=("pod")
must_have_one_noun+=("replicationcontroller")
must_have_one_noun+=("resourcequota")
must_have_one_noun+=("secret")
must_have_one_noun+=("service")
must_have_one_noun+=("status")
}
_kubectl_describe()

View File

@ -60,6 +60,9 @@ $ kubectl get rc/web service/frontend pods/web-pod-13je7`
// NewCmdGet creates a command object for the generic "get" action, which
// retrieves one or more resources from a server.
func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
p := kubectl.NewHumanReadablePrinter(false)
validArgs := p.HandledResources()
cmd := &cobra.Command{
Use: "get [(-o|--output=)json|yaml|template|...] (RESOURCE [NAME] | RESOURCE/NAME ...)",
Short: "Display one or many resources",
@ -69,6 +72,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
err := RunGet(f, out, cmd, args)
cmdutil.CheckErr(err)
},
ValidArgs: validArgs,
}
cmdutil.AddPrinterFlags(cmd)
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")

View File

@ -227,6 +227,22 @@ func (h *HumanReadablePrinter) validatePrintHandlerFunc(printFunc reflect.Value)
return nil
}
func (h *HumanReadablePrinter) HandledResources() []string {
keys := make([]string, 0)
for k := range h.handlerMap {
// k.String looks like "*api.PodList" and we want just "pod"
api := strings.Split(k.String(), ".")
resource := api[len(api)-1]
if strings.HasSuffix(resource, "List") {
continue
}
resource = strings.ToLower(resource)
keys = append(keys, resource)
}
return keys
}
var podColumns = []string{"POD", "IP", "CONTAINER(S)", "IMAGE(S)", "HOST", "LABELS", "STATUS", "CREATED"}
var replicationControllerColumns = []string{"CONTROLLER", "CONTAINER(S)", "IMAGE(S)", "SELECTOR", "REPLICAS"}
var serviceColumns = []string{"NAME", "LABELS", "SELECTOR", "IP", "PORT(S)"}