From ea689f8ca9cb1efa5fa21d6e193ed52554b9ccf9 Mon Sep 17 00:00:00 2001 From: Billy Shambrook Date: Sat, 5 Nov 2016 00:27:06 +0000 Subject: [PATCH] add completion cli command. --- cmd/kops/completion.go | 89 +++++++++++++++++++++++++++++++++++++ docs/cli/kops.md | 2 +- docs/cli/kops_completion.md | 38 ++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 cmd/kops/completion.go create mode 100644 docs/cli/kops_completion.md diff --git a/cmd/kops/completion.go b/cmd/kops/completion.go new file mode 100644 index 0000000000..ecd331fb64 --- /dev/null +++ b/cmd/kops/completion.go @@ -0,0 +1,89 @@ +/* +Copyright 2016 The Kubernetes Authors. + +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. +*/ + +package main + +import ( + "fmt" + "io" + "os" + + "github.com/spf13/cobra" +) + +const boilerPlate = ` +# Copyright 2016 The Kubernetes Authors. +# +# 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. +` + +type CompletionCmd struct { + cobraCommand *cobra.Command + Shell string +} + +var completionCmd = CompletionCmd{ + cobraCommand: &cobra.Command{ + Use: "completion", + Short: "Outputs kops shell completion for the given shell (bash)", + }, +} + +func init() { + cmd := completionCmd.cobraCommand + rootCommand.cobraCommand.AddCommand(cmd) + + cmd.Run = func(cmd *cobra.Command, args []string) { + err := completionCmd.Run(os.Stdout) + if err != nil { + exitWithError(err) + } + } + + cmd.Flags().StringVar(&completionCmd.Shell, "shell", "", "target shell (bash).") +} + +func (c *CompletionCmd) Run(w io.Writer) error { + if c.Shell == "" { + return fmt.Errorf("--shell is required") + } + + if c.Shell != "bash" { + return fmt.Errorf("only bash shell is supported for kops completion") + } + + _, err := w.Write([]byte(boilerPlate)) + if err != nil { + return err + } + + err = rootCommand.cobraCommand.GenBashCompletion(w) + if err != nil { + return err + } + + return nil +} diff --git a/docs/cli/kops.md b/docs/cli/kops.md index 363cde9e73..971e5150a9 100644 --- a/docs/cli/kops.md +++ b/docs/cli/kops.md @@ -24,12 +24,12 @@ It allows you to create, destroy, upgrade and maintain clusters. ``` ### SEE ALSO +* [kops completion](kops_completion.md) - Outputs kops shell completion for the given shell (bash) * [kops create](kops_create.md) - Create a resource by filename or stdin * [kops delete](kops_delete.md) - delete clusters * [kops describe](kops_describe.md) - describe objects * [kops edit](kops_edit.md) - edit items * [kops export](kops_export.md) - export clusters/kubecfg -* [kops genhelpdocs](kops_genhelpdocs.md) - Generate CLI help docs * [kops get](kops_get.md) - list or get objects * [kops import](kops_import.md) - import clusters * [kops rolling-update](kops_rolling-update.md) - rolling update clusters diff --git a/docs/cli/kops_completion.md b/docs/cli/kops_completion.md new file mode 100644 index 0000000000..42d84941c1 --- /dev/null +++ b/docs/cli/kops_completion.md @@ -0,0 +1,38 @@ +## kops completion + +Outputs kops shell completion for the given shell (bash) + +### Synopsis + + +Outputs kops shell completion for the given shell (bash) + +``` +kops completion +``` + +### Options + +``` + --shell string target shell (bash). +``` + +### Options inherited from parent commands + +``` + --alsologtostderr log to standard error as well as files + --config string config file (default is $HOME/.kops.yaml) + --log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) + --log_dir string If non-empty, write log files in this directory + --logtostderr log to standard error instead of files (default false) + --name string Name of cluster + --state string Location of state storage + --stderrthreshold severity logs at or above this threshold go to stderr (default 2) + -v, --v Level log level for V logs + --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging +``` + +### SEE ALSO +* [kops](kops.md) - kops is kubernetes ops + +###### Auto generated by spf13/cobra on 5-Nov-2016