From 781ca91a5975c72ed1719d3e29a5cddfc12872d2 Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Fri, 20 Feb 2015 16:27:56 -0500 Subject: [PATCH 1/4] Use the 'example' section of cobra commands This makes examples their own section in the docs, if they are defined. --- cmd/gendocs/gen_kubectl_docs.go | 14 ++++++++++---- cmd/genman/gen_kubectl_man.go | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/gendocs/gen_kubectl_docs.go b/cmd/gendocs/gen_kubectl_docs.go index 3d628e6024c..e8d2ec72183 100644 --- a/cmd/gendocs/gen_kubectl_docs.go +++ b/cmd/gendocs/gen_kubectl_docs.go @@ -31,9 +31,6 @@ import ( func printOptions(out *bytes.Buffer, command *cobra.Command, name string) { flags := command.NonInheritedFlags() flags.SetOutput(out) - if command.Runnable() { - fmt.Fprintf(out, "%s\n\n", command.UseLine()) - } if flags.HasFlags() { fmt.Fprintf(out, "### Options\n\n```\n") flags.PrintDefaults() @@ -68,7 +65,16 @@ func genMarkdown(command *cobra.Command, parent, docsDir string) { fmt.Fprintf(out, "## %s\n\n", name) fmt.Fprintf(out, "%s\n\n", short) fmt.Fprintf(out, "### Synopsis\n\n") - fmt.Fprintf(out, "%s\n\n", long) + fmt.Fprintf(out, "```\n%s\n```\n\n", long) + + if command.Runnable() { + fmt.Fprintf(out, "%s\n\n", command.UseLine()) + } + + if len(command.Example) > 0 { + fmt.Fprintf(out, "### Examples\n\n") + fmt.Fprintf(out, "```\n%s\n```\n\n", command.Example) + } printOptions(out, command, name) diff --git a/cmd/genman/gen_kubectl_man.go b/cmd/genman/gen_kubectl_man.go index 68878aba117..ec8f9cdbd61 100644 --- a/cmd/genman/gen_kubectl_man.go +++ b/cmd/genman/gen_kubectl_man.go @@ -133,6 +133,11 @@ func genMarkdown(command *cobra.Command, parent, docsDir string) { preamble(out, name, short, long) printOptions(out, command) + if len(command.Example) > 0 { + fmt.Fprintf(out, "# EXAMPLE\n") + fmt.Fprintf(out, "```\n%s\n```\n", command.Example) + } + if len(command.Commands()) > 0 || len(parent) > 0 { fmt.Fprintf(out, "# SEE ALSO\n") if len(parent) > 0 { From d508395828e41d16722dc47f2f8fef264119dc1a Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Fri, 20 Feb 2015 16:28:43 -0500 Subject: [PATCH 2/4] Change all cobra commands to use the example section This will output the examples in their own section, rather than as part of the 'long' synposis. --- pkg/kubectl/cmd/config/create_context.go | 3 +- pkg/kubectl/cmd/config/view.go | 11 +++--- pkg/kubectl/cmd/create.go | 28 ++++++++------- pkg/kubectl/cmd/delete.go | 45 ++++++++++++------------ pkg/kubectl/cmd/exec.go | 22 ++++++------ pkg/kubectl/cmd/expose.go | 25 +++++++------ pkg/kubectl/cmd/get.go | 42 +++++++++++----------- pkg/kubectl/cmd/label.go | 39 ++++++++++---------- pkg/kubectl/cmd/log.go | 23 ++++++------ pkg/kubectl/cmd/portforward.go | 35 +++++++++--------- pkg/kubectl/cmd/resize.go | 26 +++++++------- pkg/kubectl/cmd/rollingupdate.go | 35 +++++++++--------- pkg/kubectl/cmd/run.go | 38 ++++++++++---------- pkg/kubectl/cmd/stop.go | 36 ++++++++++--------- pkg/kubectl/cmd/update.go | 34 +++++++++--------- 15 files changed, 231 insertions(+), 211 deletions(-) diff --git a/pkg/kubectl/cmd/config/create_context.go b/pkg/kubectl/cmd/config/create_context.go index 2da9b859c91..aac3d588a26 100644 --- a/pkg/kubectl/cmd/config/create_context.go +++ b/pkg/kubectl/cmd/config/create_context.go @@ -46,8 +46,7 @@ func NewCmdConfigSetContext(out io.Writer, pathOptions *pathOptions) *cobra.Comm Specifying a name that already exists will merge new fields on top of existing values for those fields. e.g. kubectl config set-context gce --user=cluster-admin - only sets the user field on the gce context entry without touching other values. - `, + only sets the user field on the gce context entry without touching other values.`, Run: func(cmd *cobra.Command, args []string) { if !options.complete(cmd) { diff --git a/pkg/kubectl/cmd/config/view.go b/pkg/kubectl/cmd/config/view.go index 95f13c6dcc6..434cc2c9b3d 100644 --- a/pkg/kubectl/cmd/config/view.go +++ b/pkg/kubectl/cmd/config/view.go @@ -41,13 +41,12 @@ func NewCmdConfigView(out io.Writer, pathOptions *pathOptions) *cobra.Command { cmd := &cobra.Command{ Use: "view", Short: "displays merged .kubeconfig settings or a specified .kubeconfig file.", - Long: `displays merged .kubeconfig settings or a specified .kubeconfig file. -Examples: - // Show merged .kubeconfig settings. - $ kubectl config view + Long: "displays merged .kubeconfig settings or a specified .kubeconfig file.", + Example: `// Show merged .kubeconfig settings. +$ kubectl config view - // Show only local ./.kubeconfig settings - $ kubectl config view --local`, +// Show only local ./.kubeconfig settings +$ kubectl config view --local`, Run: func(cmd *cobra.Command, args []string) { options.complete() diff --git a/pkg/kubectl/cmd/create.go b/pkg/kubectl/cmd/create.go index 67098970c45..187e6b0dc26 100644 --- a/pkg/kubectl/cmd/create.go +++ b/pkg/kubectl/cmd/create.go @@ -26,24 +26,26 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/util" ) +const ( + create_long = `Create a resource by filename or stdin. + +JSON and YAML formats are accepted.` + create_example = `// Create a pod using the data in pod.json. +$ kubectl create -f pod.json + +// Create a pod based on the JSON passed into stdin. +$ cat pod.json | kubectl create -f -` +) + func (f *Factory) NewCmdCreate(out io.Writer) *cobra.Command { flags := &struct { Filenames util.StringList }{} cmd := &cobra.Command{ - Use: "create -f filename", - Short: "Create a resource by filename or stdin", - Long: `Create a resource by filename or stdin. - -JSON and YAML formats are accepted. - -Examples: - - // Create a pod using the data in pod.json. - $ kubectl create -f pod.json - - // Create a pod based on the JSON passed into stdin. - $ cat pod.json | kubectl create -f -`, + Use: "create -f filename", + Short: "Create a resource by filename or stdin", + Long: create_long, + Example: create_example, Run: func(cmd *cobra.Command, args []string) { schema, err := f.Validator(cmd) checkErr(err) diff --git a/pkg/kubectl/cmd/delete.go b/pkg/kubectl/cmd/delete.go index bd3777f454a..8797ab24648 100644 --- a/pkg/kubectl/cmd/delete.go +++ b/pkg/kubectl/cmd/delete.go @@ -28,14 +28,8 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/util" ) -func (f *Factory) NewCmdDelete(out io.Writer) *cobra.Command { - flags := &struct { - Filenames util.StringList - }{} - cmd := &cobra.Command{ - Use: "delete ([-f filename] | ( [( | -l