Skip to content

Commit

Permalink
Merge pull request #11985 from johngmyers/complete-misc
Browse files Browse the repository at this point in the history
Cobra cleanups
  • Loading branch information
k8s-ci-robot committed Jul 13, 2021
2 parents f5c67ee + 010aa2c commit 7122e4d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 68 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ gcs-publish-ci: bazel-version-ci
gen-cli-docs: ${KOPS} # Regenerate CLI docs
KOPS_STATE_STORE= \
KOPS_FEATURE_FLAGS= \
${KOPS} genhelpdocs --out docs/cli
${KOPS} gen-cli-docs --out docs/cli

.PHONY: push-amd64
push-amd64: crossbuild-nodeup-amd64
Expand Down Expand Up @@ -439,7 +439,7 @@ verify-misspelling:
.PHONY: verify-gendocs
verify-gendocs: ${KOPS}
@TMP_DOCS="$$(mktemp -d)"; \
'${KOPS}' genhelpdocs --out "$$TMP_DOCS"; \
'${KOPS}' gen-cli-docs --out "$$TMP_DOCS"; \
\
if ! diff -r "$$TMP_DOCS" '${KOPS_ROOT}/docs/cli'; then \
echo "FAIL: make verify-gendocs failed, as the generated markdown docs are out of date." 1>&2; \
Expand Down
3 changes: 1 addition & 2 deletions cmd/kops/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,12 @@ func NewCmdCreateCluster(f *util.Factory, out io.Writer) *cobra.Command {
encryptEtcdStorage := false

cmd := &cobra.Command{
Use: "cluster [CLUSTER]",
Short: createClusterShort,
Long: createClusterLong,
Example: createClusterExample,
Args: rootCommand.clusterNameArgsNoKubeconfig(&options.ClusterName),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
},
Use: "cluster [CLUSTER]",
Short: createClusterShort,
Long: createClusterLong,
Example: createClusterExample,
Args: rootCommand.clusterNameArgsNoKubeconfig(&options.ClusterName),
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
var err error

Expand Down
46 changes: 23 additions & 23 deletions cmd/kops/gen_help_docs.go → cmd/kops/gen_cli_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ limitations under the License.
package main

import (
"io"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"k8s.io/kops/cmd/kops/util"
)

const fileHeader = `
<!--- This file is automatically generated by make gen-cli-docs; changes should be made in the go CLI command code (under cmd/kops) -->
`

type GenHelpDocsCmd struct {
cobraCommand *cobra.Command
OutDir string
}

var genHelpDocsCmd = GenHelpDocsCmd{
cobraCommand: &cobra.Command{
Use: "genhelpdocs",
Short: "Generate CLI help docs",
Hidden: true,
},
type GenHelpDocsOptions struct {
OutDir string
}

func init() {
cmd := genHelpDocsCmd.cobraCommand
rootCommand.cobraCommand.AddCommand(cmd)

cmd.Run = func(cmd *cobra.Command, args []string) {
err := genHelpDocsCmd.Run()
if err != nil {
exitWithError(err)
}
func NewCmdGenCLIDocs(f *util.Factory, out io.Writer) *cobra.Command {
options := &GenHelpDocsOptions{}

cmd := &cobra.Command{
Use: "gen-cli-docs",
Short: "Generate CLI help docs",
Hidden: true,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
return RunGenCLIDocs(options)
},
}

cmd.Flags().StringVar(&genHelpDocsCmd.OutDir, "out", "", "path to write out to.")
cmd.Flags().StringVar(&options.OutDir, "out", "", "path to write out to.")
cmd.MarkFlagDirname("out")

return cmd
}

func (c *GenHelpDocsCmd) Run() error {
func RunGenCLIDocs(options *GenHelpDocsOptions) error {
rootCommand.cobraCommand.DisableAutoGenTag = true

// unset KOPS_STATE_STORE from default value
Expand All @@ -64,5 +64,5 @@ func (c *GenHelpDocsCmd) Run() error {
linkHandler := func(link string) string { return link }
filePrepender := func(filname string) string { return fileHeader }

return doc.GenMarkdownTreeCustom(rootCommand.cobraCommand, c.OutDir, filePrepender, linkHandler)
return doc.GenMarkdownTreeCustom(rootCommand.cobraCommand, options.OutDir, filePrepender, linkHandler)
}
41 changes: 18 additions & 23 deletions cmd/kops/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/util/pkg/text"
"k8s.io/kops/util/pkg/vfs"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
Expand All @@ -54,42 +53,38 @@ var (
replaceShort = i18n.T(`Replace cluster resources.`)
)

// replaceOptions is the options for the command
type replaceOptions struct {
// Filenames is a list of files containing resources
// ReplaceOptions is the options for the command
type ReplaceOptions struct {
// Filenames is a list of files containing resources to replace.
Filenames []string
// create any resources not found - we limit to instance groups only for now
force bool
// Force causes any missing rescources to be created.
Force bool
}

// NewCmdReplace returns a new replace command
func NewCmdReplace(f *util.Factory, out io.Writer) *cobra.Command {
options := &replaceOptions{}
options := &ReplaceOptions{}

cmd := &cobra.Command{
Use: "replace -f FILENAME",
Short: replaceShort,
Long: replaceLong,
Example: replaceExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
if len(options.Filenames) == 0 {
cmd.Help()
return
}

cmdutil.CheckErr(RunReplace(ctx, f, cmd, out, options))
Use: "replace {-f FILENAME}...",
Short: replaceShort,
Long: replaceLong,
Example: replaceExample,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
return RunReplace(context.TODO(), f, out, options)
},
}
cmd.Flags().StringSliceVarP(&options.Filenames, "filename", "f", options.Filenames, "A list of one or more files separated by a comma.")
cmd.Flags().BoolVarP(&options.force, "force", "", false, "Force any changes, which will also create any non-existing resource")
cmd.MarkFlagRequired("filename")
cmd.Flags().BoolVarP(&options.Force, "force", "", false, "Force any changes, which will also create any non-existing resource")

return cmd
}

// RunReplace processes the replace command
func RunReplace(ctx context.Context, f *util.Factory, cmd *cobra.Command, out io.Writer, c *replaceOptions) error {
func RunReplace(ctx context.Context, f *util.Factory, out io.Writer, c *ReplaceOptions) error {
clientset, err := f.Clientset()
if err != nil {
return err
Expand Down Expand Up @@ -140,7 +135,7 @@ func RunReplace(ctx context.Context, f *util.Factory, cmd *cobra.Command, out io
}
}
if cluster == nil {
if !c.force {
if !c.Force {
return fmt.Errorf("cluster %v does not exist (try adding --force flag)", clusterName)
}
_, err = clientset.CreateCluster(ctx, v)
Expand Down Expand Up @@ -172,7 +167,7 @@ func RunReplace(ctx context.Context, f *util.Factory, cmd *cobra.Command, out io
ig, err := clientset.InstanceGroupsFor(cluster).Get(ctx, igName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
if !c.force {
if !c.Force {
return fmt.Errorf("instanceGroup: %v does not exist (try adding --force flag)", igName)
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion cmd/kops/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,17 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command {
cmd.AddCommand(NewCmdDistrust(f, out))
cmd.AddCommand(NewCmdEdit(f, out))
cmd.AddCommand(NewCmdExport(f, out))
cmd.AddCommand(NewCmdGenCLIDocs(f, out))
cmd.AddCommand(NewCmdGet(f, out))
cmd.AddCommand(commands.NewCmdHelpers(f, out))
cmd.AddCommand(NewCmdPromote(f, out))
cmd.AddCommand(NewCmdUpdate(f, out))
cmd.AddCommand(NewCmdReplace(f, out))
cmd.AddCommand(NewCmdRollingUpdate(f, out))
cmd.AddCommand(NewCmdSet(f, out))
cmd.AddCommand(NewCmdToolbox(f, out))
cmd.AddCommand(NewCmdTrust(f, out))
cmd.AddCommand(NewCmdUnset(f, out))
cmd.AddCommand(NewCmdUpdate(f, out))
cmd.AddCommand(NewCmdUpgrade(f, out))
cmd.AddCommand(NewCmdValidate(f, out))
cmd.AddCommand(NewCmdVersion(f, out))
Expand Down
14 changes: 6 additions & 8 deletions cmd/kops/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ func NewCmdVersion(f *util.Factory, out io.Writer) *cobra.Command {
options := &commands.VersionOptions{}

cmd := &cobra.Command{
Use: "version",
Short: versionShort,
Long: versionLong,
Example: versionExample,
Args: cobra.NoArgs,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
},
Use: "version",
Short: versionShort,
Long: versionLong,
Example: versionExample,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
return commands.RunVersion(f, out, options)
},
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/kops_replace.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7122e4d

Please sign in to comment.