-
Notifications
You must be signed in to change notification settings - Fork 11
added subgroup name #285
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
added subgroup name #285
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,8 +3,10 @@ package cmd | |||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||
| "sort" | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||||||||||||||||||||||||||
| "github.com/lets-cli/lets/set" | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // newRootCmd represents the base command when called without any subcommands. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -58,6 +60,94 @@ func PrintHelpMessage(cmd *cobra.Command) error { | |||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string { | ||||||||||||||||||||||||||||||||||||||||||||
| help := "" | ||||||||||||||||||||||||||||||||||||||||||||
| cmds := []*cobra.Command{} | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // select commands that belong to the specified group | ||||||||||||||||||||||||||||||||||||||||||||
| for _, c := range cmd.Commands() { | ||||||||||||||||||||||||||||||||||||||||||||
| if c.GroupID == group.ID && (c.IsAvailableCommand() || c.Name() == "help") { | ||||||||||||||||||||||||||||||||||||||||||||
| cmds = append(cmds, c) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| sort.Slice(cmds, func(i, j int) bool { | ||||||||||||||||||||||||||||||||||||||||||||
| return cmds[i].Name() < cmds[j].Name() | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Create a list of subgroups | ||||||||||||||||||||||||||||||||||||||||||||
| subGroupNameSet := set.NewSet[string]() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| for _, c := range cmds { | ||||||||||||||||||||||||||||||||||||||||||||
| if subgroup, ok := c.Annotations["SubGroupName"]; ok && subgroup != "" { | ||||||||||||||||||||||||||||||||||||||||||||
| subGroupNameSet.Add(subgroup) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| subGroupNameList := subGroupNameSet.ToList() | ||||||||||||||||||||||||||||||||||||||||||||
| sort.Strings(subGroupNameList) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // generate output | ||||||||||||||||||||||||||||||||||||||||||||
| help += fmt.Sprintf("%s\n", group.Title) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| for _, subgroupName := range subGroupNameList { | ||||||||||||||||||||||||||||||||||||||||||||
| intend := "" | ||||||||||||||||||||||||||||||||||||||||||||
| if len(subGroupNameList) > 1 { | ||||||||||||||||||||||||||||||||||||||||||||
| help += fmt.Sprintf("\n %s\n", subgroupName) | ||||||||||||||||||||||||||||||||||||||||||||
| intend = " " | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| for _, c := range cmds { | ||||||||||||||||||||||||||||||||||||||||||||
| if subgroup, ok := c.Annotations["SubGroupName"]; ok && subgroup == subgroupName { | ||||||||||||||||||||||||||||||||||||||||||||
| help += fmt.Sprintf("%s %-*s %s\n", intend, cmd.NamePadding(), c.Name(), c.Short) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| for _, c := range cmds { | ||||||||||||||||||||||||||||||||||||||||||||
| if _, ok := c.Annotations["SubGroupName"]; !ok { | ||||||||||||||||||||||||||||||||||||||||||||
| help += fmt.Sprintf(" %-*s %s\n", cmd.NamePadding(), c.Name(), c.Short) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| help += "\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| return help | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| func PrintRootHelpMessage(cmd *cobra.Command) error { | ||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
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. Looks like if lets executed as is (without
Contributor
Author
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. Line 111 in 3390a5d
fixed with (len(os.Args) == 1) and added tests Lines 50 to 69 in 3390a5d
|
||||||||||||||||||||||||||||||||||||||||||||
| help := "" | ||||||||||||||||||||||||||||||||||||||||||||
| help = fmt.Sprintf("%s\n\n%s", cmd.Short, help) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // General | ||||||||||||||||||||||||||||||||||||||||||||
| help += "Usage:\n" | ||||||||||||||||||||||||||||||||||||||||||||
| if cmd.Runnable() { | ||||||||||||||||||||||||||||||||||||||||||||
| help += fmt.Sprintf(" %s\n", cmd.UseLine()) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if cmd.HasAvailableSubCommands() { | ||||||||||||||||||||||||||||||||||||||||||||
| help += fmt.Sprintf(" %s [command]\n", cmd.CommandPath()) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| help += "\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Commands | ||||||||||||||||||||||||||||||||||||||||||||
| for _, group := range cmd.Groups() { | ||||||||||||||||||||||||||||||||||||||||||||
| help += buildGroupCommandHelp(cmd, group) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Flags | ||||||||||||||||||||||||||||||||||||||||||||
| if cmd.HasAvailableLocalFlags() { | ||||||||||||||||||||||||||||||||||||||||||||
| help += "Flags:\n" | ||||||||||||||||||||||||||||||||||||||||||||
| help += cmd.LocalFlags().FlagUsagesWrapped(120) | ||||||||||||||||||||||||||||||||||||||||||||
| help += "\n" | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Usage | ||||||||||||||||||||||||||||||||||||||||||||
| help += fmt.Sprintf(`Use "%s help [command]" for more information about a command.`, cmd.CommandPath()) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| _, err := fmt.Fprint(cmd.OutOrStdout(), help) | ||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| func PrintVersionMessage(cmd *cobra.Command) error { | ||||||||||||||||||||||||||||||||||||||||||||
| _, err := fmt.Fprintf(cmd.OutOrStdout(), "lets version %s\n", cmd.Version) | ||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -202,6 +202,9 @@ func newSubcommand(command *config.Command, conf *config.Config, showAll bool, o | |||||||||||||||
| c.Println(err) | ||||||||||||||||
| } | ||||||||||||||||
| }) | ||||||||||||||||
| subCmd.Annotations = map[string]string{ | ||||||||||||||||
| "SubGroupName": command.GroupName, | ||||||||||||||||
|
Comment on lines
+205
to
+206
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. issue (bug_risk): Assigning a new Annotations map may clobber existing annotations on the command. This overwrites any existing if subCmd.Annotations == nil {
subCmd.Annotations = map[string]string{}
}
subCmd.Annotations["SubGroupName"] = command.GroupName |
||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+205
to
+207
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. suggestion (bug_risk): Overwriting If
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| return subCmd | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/lithammer/dedent" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| func CommandFixture(t *testing.T, text string) *Command { | ||
| buf := bytes.NewBufferString(text) | ||
| c := &Command{} | ||
| if err := yaml.NewDecoder(buf).Decode(&c); err != nil { | ||
| t.Fatalf("command fixture decode error: %s", err) | ||
| } | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| func TestParseCommand(t *testing.T) { | ||
| t.Run("default group", func(t *testing.T) { | ||
| text := dedent.Dedent(` | ||
| cmd: [echo, Hello] | ||
| `) | ||
| command := CommandFixture(t, text) | ||
| exp := "Common" | ||
|
|
||
| if command.GroupName != exp { | ||
| t.Errorf("wrong output. \nexpect %s \ngot: %s", exp, command.GroupName) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("provided custom group", func(t *testing.T) { | ||
| text := dedent.Dedent(` | ||
| group: Group Name | ||
| cmd: [echo, Hello] | ||
| `) | ||
| command := CommandFixture(t, text) | ||
| exp := "Group Name" | ||
|
|
||
| if command.GroupName != exp { | ||
| t.Errorf("wrong output. \nexpect %s \ngot: %s", exp, command.GroupName) | ||
| } | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| load test_helpers | ||
|
|
||
| setup() { | ||
| load "${BATS_UTILS_PATH}/bats-support/load.bash" | ||
| load "${BATS_UTILS_PATH}/bats-assert/load.bash" | ||
| cd ./tests/command_group | ||
| } | ||
|
|
||
| HELP_MESSAGE=$(cat <<EOF | ||
| A CLI task runner | ||
|
|
||
| Usage: | ||
| lets [flags] | ||
| lets [command] | ||
|
|
||
| Commands: | ||
|
|
||
| A group | ||
| c c command | ||
|
|
||
| B group | ||
| a a command | ||
| b b command | ||
|
|
||
| Common | ||
| d d command | ||
|
|
||
| Internal commands: | ||
| help Help about any command | ||
| self Manage lets CLI itself | ||
|
|
||
| Flags: | ||
| --all show all commands (including the ones with _) | ||
| -c, --config string config file (default is lets.yaml) | ||
| -d, --debug count show debug logs (or use LETS_DEBUG=1). If used multiple times, shows more verbose logs | ||
| -E, --env stringToString set env variable for running command KEY=VALUE (default []) | ||
| --exclude stringArray run all but excluded command(s) described in cmd as map | ||
| -h, --help help for lets | ||
| --init create a new lets.yaml in the current folder | ||
| --no-depends skip 'depends' for running command | ||
| --only stringArray run only specified command(s) described in cmd as map | ||
| --upgrade upgrade lets to latest version | ||
| -v, --version version for lets | ||
|
|
||
| Use "lets help [command]" for more information about a command. | ||
| EOF | ||
| ) | ||
|
|
||
|
|
||
| @test "help: running 'lets help' should group commands by their group names" { | ||
| run lets help | ||
| assert_success | ||
|
|
||
| assert_output "$HELP_MESSAGE" | ||
| } | ||
|
|
||
| @test "help: running 'lets --help' should group commands by their group names" { | ||
| run lets --help | ||
| assert_success | ||
|
|
||
| assert_output "$HELP_MESSAGE" | ||
| } | ||
|
|
||
| @test "help: running 'lets' should group commands by their group names" { | ||
| run lets | ||
| assert_success | ||
|
|
||
| assert_output "$HELP_MESSAGE" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| shell: bash | ||
|
|
||
| commands: | ||
| b: | ||
| group: B group | ||
| description: b command | ||
| cmd: echo | ||
|
|
||
| a: | ||
| group: B group | ||
| description: a command | ||
| cmd: echo | ||
|
|
||
| c: | ||
| group: A group | ||
| description: c command | ||
| cmd: echo | ||
|
|
||
| d: | ||
| description: d command | ||
| cmd: echo |
Uh oh!
There was an error while loading. Please reload this page.