diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index d9bc47c7729..c4a3963bd91 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -285,7 +285,11 @@ _kubectl_create() flags_completion=() flags+=("--filename=") + flags_with_completion+=("--filename") + flags_completion+=("_filedir '@(json|yaml|yml)'") two_word_flags+=("-f") + flags_with_completion+=("-f") + flags_completion+=("_filedir '@(json|yaml|yml)'") flags+=("--help") flags+=("-h") @@ -304,7 +308,11 @@ _kubectl_update() flags_completion=() flags+=("--filename=") + flags_with_completion+=("--filename") + flags_completion+=("_filedir '@(json|yaml|yml)'") two_word_flags+=("-f") + flags_with_completion+=("-f") + flags_completion+=("_filedir '@(json|yaml|yml)'") flags+=("--help") flags+=("-h") flags+=("--patch=") @@ -325,7 +333,11 @@ _kubectl_delete() flags+=("--all") flags+=("--filename=") + flags_with_completion+=("--filename") + flags_completion+=("_filedir '@(json|yaml|yml)'") two_word_flags+=("-f") + flags_with_completion+=("-f") + flags_completion+=("_filedir '@(json|yaml|yml)'") flags+=("--help") flags+=("-h") flags+=("--selector=") @@ -526,7 +538,11 @@ _kubectl_stop() flags+=("--all") flags+=("--filename=") + flags_with_completion+=("--filename") + flags_completion+=("_filedir '@(json|yaml|yml)'") two_word_flags+=("-f") + flags_with_completion+=("-f") + flags_completion+=("_filedir '@(json|yaml|yml)'") flags+=("--help") flags+=("-h") flags+=("--selector=") diff --git a/pkg/kubectl/bash_comp_utils.go b/pkg/kubectl/bash_comp_utils.go new file mode 100644 index 00000000000..55e5b86e839 --- /dev/null +++ b/pkg/kubectl/bash_comp_utils.go @@ -0,0 +1,40 @@ +/* +Copyright 2015 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// A set of common functions needed by cmd/kubectl and pkg/kubectl packages. +package kubectl + +import ( + "github.com/GoogleCloudPlatform/kubernetes/pkg/util" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func AddJsonFilenameFlag(cmd *cobra.Command, value *util.StringList, usage string) { + annotations := []string{"json", "yaml", "yml"} + annotation := make(map[string][]string) + annotation[cobra.BashCompFilenameExt] = annotations + + flag := &pflag.Flag{ + Name: "filename", + Shorthand: "f", + Usage: usage, + Value: value, + DefValue: value.String(), + Annotations: annotation, + } + cmd.Flags().AddFlag(flag) +} diff --git a/pkg/kubectl/cmd/create.go b/pkg/kubectl/cmd/create.go index b0e731d1870..a0ff1d679d3 100644 --- a/pkg/kubectl/cmd/create.go +++ b/pkg/kubectl/cmd/create.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" @@ -50,7 +51,10 @@ func NewCmdCreate(f *cmdutil.Factory, out io.Writer) *cobra.Command { cmdutil.CheckErr(RunCreate(f, out, filenames)) }, } - cmd.Flags().VarP(&filenames, "filename", "f", "Filename, directory, or URL to file to use to create the resource") + + usage := "Filename, directory, or URL to file to use to create the resource" + kubectl.AddJsonFilenameFlag(cmd, &filenames, usage) + return cmd } diff --git a/pkg/kubectl/cmd/delete.go b/pkg/kubectl/cmd/delete.go index f660521909b..f5a2ddf3601 100644 --- a/pkg/kubectl/cmd/delete.go +++ b/pkg/kubectl/cmd/delete.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/cobra" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" + "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" @@ -67,7 +68,8 @@ func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command { cmdutil.CheckErr(err) }, } - cmd.Flags().VarP(&filenames, "filename", "f", "Filename, directory, or URL to a file containing the resource to delete") + usage := "Filename, directory, or URL to a file containing the resource to delete" + kubectl.AddJsonFilenameFlag(cmd, &filenames, usage) cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on") cmd.Flags().Bool("all", false, "[-all] to select all the specified resources") return cmd diff --git a/pkg/kubectl/cmd/stop.go b/pkg/kubectl/cmd/stop.go index 40dcc460e28..f361b9ef8f2 100644 --- a/pkg/kubectl/cmd/stop.go +++ b/pkg/kubectl/cmd/stop.go @@ -20,6 +20,7 @@ import ( "fmt" "io" + "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" @@ -79,7 +80,8 @@ func NewCmdStop(f *cmdutil.Factory, out io.Writer) *cobra.Command { }) }, } - cmd.Flags().VarP(&flags.Filenames, "filename", "f", "Filename, directory, or URL to file of resource(s) to be stopped") + usage := "Filename, directory, or URL to file of resource(s) to be stopped" + kubectl.AddJsonFilenameFlag(cmd, &flags.Filenames, usage) cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on") cmd.Flags().Bool("all", false, "[-all] to select all the specified resources") return cmd diff --git a/pkg/kubectl/cmd/update.go b/pkg/kubectl/cmd/update.go index fd08b2d7ad0..08672ebaf37 100644 --- a/pkg/kubectl/cmd/update.go +++ b/pkg/kubectl/cmd/update.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" @@ -53,7 +54,8 @@ func NewCmdUpdate(f *cmdutil.Factory, out io.Writer) *cobra.Command { cmdutil.CheckErr(err) }, } - cmd.Flags().VarP(&filenames, "filename", "f", "Filename, directory, or URL to file to use to update the resource.") + usage := "Filename, directory, or URL to file to use to update the resource." + kubectl.AddJsonFilenameFlag(cmd, &filenames, usage) cmd.Flags().String("patch", "", "A JSON document to override the existing resource. The resource is downloaded, patched with the JSON, then updated.") return cmd }