From 69d0ecd26de3e619209aea51bbbcf730acaed5fb Mon Sep 17 00:00:00 2001 From: Thomas Tendyck Date: Tue, 10 May 2022 12:34:47 +0200 Subject: [PATCH] cli: show instance types on help and usage --- cli/cmd/create.go | 11 ++++++++++- cli/cmd/instancetypes.go | 7 +++++++ cli/cmd/validargs.go | 6 +++++- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 cli/cmd/instancetypes.go diff --git a/cli/cmd/create.go b/cli/cmd/create.go index 47a31bd66..e9c5f276d 100644 --- a/cli/cmd/create.go +++ b/cli/cmd/create.go @@ -37,6 +37,15 @@ func newCreateCmd() *cobra.Command { must(cobra.MarkFlagRequired(cmd.Flags(), "worker-nodes")) cmd.Flags().StringP("instance-type", "t", "", "instance type of cluster nodes") must(cmd.RegisterFlagCompletionFunc("instance-type", instanceTypeCompletion)) + + cmd.SetHelpTemplate(cmd.HelpTemplate() + fmt.Sprintf(` +Azure instance types: +%v + +GCP instance types: +%v +`, formatInstanceTypes(azure.InstanceTypes), formatInstanceTypes(gcp.InstanceTypes))) + return cmd } @@ -117,7 +126,7 @@ func parseCreateFlags(cmd *cobra.Command, provider cloudprovider.Provider) (crea if insType == "" { insType = defaultInstanceType(provider) } - if err := validInstanceTypeForProvider(insType, provider); err != nil { + if err := validInstanceTypeForProvider(cmd, insType, provider); err != nil { return createFlags{}, err } diff --git a/cli/cmd/instancetypes.go b/cli/cmd/instancetypes.go new file mode 100644 index 000000000..a98e79a1a --- /dev/null +++ b/cli/cmd/instancetypes.go @@ -0,0 +1,7 @@ +package cmd + +import "strings" + +func formatInstanceTypes(types []string) string { + return " " + strings.Join(types, "\n ") +} diff --git a/cli/cmd/validargs.go b/cli/cmd/validargs.go index 9ae6c0370..393b64ef9 100644 --- a/cli/cmd/validargs.go +++ b/cli/cmd/validargs.go @@ -32,7 +32,7 @@ func isCloudProvider(arg int) cobra.PositionalArgs { } } -func validInstanceTypeForProvider(insType string, provider cloudprovider.Provider) error { +func validInstanceTypeForProvider(cmd *cobra.Command, insType string, provider cloudprovider.Provider) error { switch provider { case cloudprovider.GCP: for _, instanceType := range gcp.InstanceTypes { @@ -40,6 +40,8 @@ func validInstanceTypeForProvider(insType string, provider cloudprovider.Provide return nil } } + cmd.SetUsageTemplate("GCP instance types:\n" + formatInstanceTypes(gcp.InstanceTypes)) + cmd.SilenceUsage = false return fmt.Errorf("%s isn't a valid GCP instance type", insType) case cloudprovider.Azure: for _, instanceType := range azure.InstanceTypes { @@ -47,6 +49,8 @@ func validInstanceTypeForProvider(insType string, provider cloudprovider.Provide return nil } } + cmd.SetUsageTemplate("Azure instance types:\n" + formatInstanceTypes(azure.InstanceTypes)) + cmd.SilenceUsage = false return fmt.Errorf("%s isn't a valid Azure instance type", insType) default: return fmt.Errorf("%s isn't a valid cloud platform", provider)