-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Group cluster create flags for readability #328
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,13 @@ package create | |
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/kubicorn/kubicorn/pkg/logger" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/weaveworks/eksctl/pkg/ami" | ||
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils" | ||
"github.com/weaveworks/eksctl/pkg/eks" | ||
|
@@ -94,9 +97,58 @@ func createClusterCmd() *cobra.Command { | |
|
||
fs.BoolVarP(&ng.PrivateNetworking, "node-private-networking", "P", false, "whether to make initial nodegroup networking private") | ||
|
||
groupFlagsInUsage(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func groupFlagsInUsage(cmd *cobra.Command) { | ||
// Group flags by their categories determined by name prefixes | ||
groupToPatterns := map[string][]string{ | ||
"Node": {"node", "storage-class", "ssh", "max-pods-per-node", "full-ecr-access", "asg-access"}, | ||
"Networking": {"vpc", "zones",}, | ||
"Stack": {"region", "tags"}, | ||
"Other": {}, | ||
} | ||
groups := []string{} | ||
for k := range groupToPatterns { | ||
groups = append(groups, k) | ||
} | ||
groupToFlagSet := make(map[string]*pflag.FlagSet) | ||
for _, g := range groups { | ||
groupToFlagSet[g] = pflag.NewFlagSet(g, /* Unused. Can be anythng. */ pflag.ContinueOnError) | ||
} | ||
cmd.LocalFlags().VisitAll(func(f *pflag.Flag) { | ||
for _, g := range groups { | ||
for _, p := range groupToPatterns[g] { | ||
if strings.HasPrefix(f.Name, p) { | ||
groupToFlagSet[g].AddFlag(f) | ||
return | ||
} | ||
} | ||
} | ||
groupToFlagSet["Other"].AddFlag(f) | ||
}) | ||
|
||
// The usage template is based on the one bundled into cobra | ||
// https://github.com/spf13/cobra/blob/1e58aa3361fd650121dceeedc399e7189c05674a/command.go#L397 | ||
origFlagUsages := ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use https://github.com/lithammer/dedent here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Umm, the whole purpose of this seemingly redundant empty lines is to make necessary and enough space between the usage` and the flags section. That is, if I just wrap it with Decent like
Probably this isn't your intention? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I didn't notice that. The reason I was suggesting this is to avoid braking indentation, as currently the string has no indentation, and it's in a middle of a function, so it jumps straight at me when I look at it. Perhaps this is just my OCD :) |
||
|
||
Flags: | ||
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}` | ||
|
||
altFlagUsages := `` | ||
for _, g := range groups { | ||
set := groupToFlagSet[g] | ||
altFlagUsages += fmt.Sprintf(` | ||
|
||
%s Flags: | ||
%s`, g, strings.TrimRightFunc(set.FlagUsages(), unicode.IsSpace)) | ||
} | ||
|
||
cmd.SetUsageTemplate(strings.Replace(cmd.UsageTemplate(), origFlagUsages, altFlagUsages, 1)) | ||
} | ||
|
||
func doCreateCluster(p *api.ProviderConfig, cfg *api.ClusterConfig, ng *api.NodeGroup, nameArg string) error { | ||
meta := cfg.Metadata | ||
ctl := eks.New(p, cfg) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually would be nice to automate this, so we don't have to keep two copies, but I'm happy to merge this as is to start with!