Skip to content

Commit e2f3395

Browse files
Group proxy tool commands under a Tools heading in help (#334)
Co-authored-by: linear-code[bot] <222613912+linear-code[bot]@users.noreply.github.com>
1 parent 9c2dc97 commit e2f3395

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

cmd/help_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ func TestRootHelpOutputTemplate(t *testing.T) {
3838
assertNotContains(t, out, "\n version ")
3939
}
4040

41+
func TestRootHelpGroupsToolsSeparately(t *testing.T) {
42+
out, err := executeWithArgs(t, "--help")
43+
if err != nil {
44+
t.Fatalf("expected no error, got %v", err)
45+
}
46+
47+
assertContains(t, out, "Commands:")
48+
assertContains(t, out, "Tools:")
49+
50+
// The proxy commands must be listed under the Tools group, not among the
51+
// regular commands.
52+
toolsSection := out[strings.Index(out, "Tools:"):]
53+
for _, tool := range []string{"aws", "az", "cdk", "sam", "terraform"} {
54+
assertContains(t, toolsSection, tool)
55+
}
56+
57+
// Tools come after the management commands in the help output.
58+
if strings.Index(out, "Commands:") > strings.Index(out, "Tools:") {
59+
t.Fatalf("expected Commands group to appear before Tools group\noutput:\n%s", out)
60+
}
61+
62+
// No commands should fall through to an ungrouped section.
63+
assertNotContains(t, out, "Additional Commands:")
64+
}
65+
4166
func TestSubcommandHelpUsesSubcommandUsageLine(t *testing.T) {
4267
out, err := executeWithArgs(t, "start", "--help")
4368
if err != nil {

cmd/root.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ import (
3434
// emit the same name as their canonical subcommand.
3535
const canonicalCommandAnnotation = "lstk.canonical"
3636

37+
// Command group IDs used to separate the proxy "tool" commands (aws, terraform,
38+
// cdk, sam, az) from the rest of lstk's commands in the help output.
39+
const (
40+
groupCommands = "commands"
41+
groupTools = "tools"
42+
)
43+
3744
func NewRootCmd(cfg *env.Env, tel *telemetry.Client, logger log.Logger) *cobra.Command {
3845
var firstRun bool
3946
root := &cobra.Command{
@@ -69,7 +76,12 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client, logger log.Logger) *cobra.C
6976
root.Flags().Lookup("version").Usage = "Show version"
7077
root.SetVersionTemplate(versionLine() + "\n")
7178

72-
root.AddCommand(
79+
root.AddGroup(
80+
&cobra.Group{ID: groupCommands, Title: "Commands:"},
81+
&cobra.Group{ID: groupTools, Title: "Tools:"},
82+
)
83+
84+
commands := []*cobra.Command{
7385
newStartCmd(cfg, tel, logger),
7486
newStopCmd(cfg, tel),
7587
newRestartCmd(cfg, tel, logger),
@@ -82,16 +94,33 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client, logger log.Logger) *cobra.C
8294
newVolumeCmd(cfg),
8395
newUpdateCmd(cfg),
8496
newDocsCmd(),
97+
newSnapshotCmd(cfg, tel, logger),
98+
newResetCmd(cfg),
99+
newSaveCmd(cfg),
100+
newLoadCmd(cfg, tel, logger),
101+
}
102+
for _, c := range commands {
103+
c.GroupID = groupCommands
104+
}
105+
106+
// Proxy commands that forward to a wrapped tool (AWS/Azure CLI, Terraform,
107+
// CDK, SAM) configured to target LocalStack.
108+
tools := []*cobra.Command{
85109
newAWSCmd(cfg),
86110
newTerraformCmd(cfg, logger),
87111
newCDKCmd(cfg, logger),
88112
newSamCmd(cfg, logger),
89-
newSnapshotCmd(cfg, tel, logger),
90113
newAzCmd(cfg),
91-
newResetCmd(cfg),
92-
newSaveCmd(cfg),
93-
newLoadCmd(cfg, tel, logger),
94-
)
114+
}
115+
for _, c := range tools {
116+
c.GroupID = groupTools
117+
}
118+
119+
root.AddCommand(commands...)
120+
root.AddCommand(tools...)
121+
122+
root.SetHelpCommandGroupID(groupCommands)
123+
root.SetCompletionCommandGroupID(groupCommands)
95124

96125
return root
97126
}

0 commit comments

Comments
 (0)