Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"fmt"
"strings"
"sort"
"strings"

"github.com/spf13/cobra"
"github.com/lets-cli/lets/set"
"github.com/spf13/cobra"
)

// newRootCmd represents the base command when called without any subcommands.
Expand Down Expand Up @@ -60,6 +60,36 @@ func PrintHelpMessage(cmd *cobra.Command) error {
return err
}

func maxCommandNameLen(cmd *cobra.Command) int {
maxLen := 0

for _, c := range cmd.Commands() {
if l := len(c.Name()); l > maxLen {
maxLen = l
}
}

return maxLen
}

func rpad(s string, padding int) string {
formattedString := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(formattedString, s)
}

func hasSubgroup(cmd *cobra.Command) bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call it for each group. I didn’t want to call it before buildGroupCommandHelp and pass it as a parameter. Since we only have two groups, this is probably fine.

subgroups := make(map[string]struct{})
for _, c := range cmd.Commands() {
if subgroup, ok := c.Annotations["SubGroupName"]; ok && subgroup != "" {
subgroups[subgroup] = struct{}{}
if len(subgroups) > 1 {
return true
}
}
}
return false
}

func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string {
help := ""
cmds := []*cobra.Command{}
Expand All @@ -71,6 +101,8 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string {
}
}

padding := maxCommandNameLen(cmd)

sort.Slice(cmds, func(i, j int) bool {
return cmds[i].Name() < cmds[j].Name()
})
Expand All @@ -88,24 +120,27 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string {
sort.Strings(subGroupNameList)

// generate output
help += fmt.Sprintf("%s\n", group.Title)
help += group.Title + "\n"

intend := ""
if hasSubgroup(cmd) {
intend = " "
}

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)
help += fmt.Sprintf(" %s%s %s\n", intend, rpad(c.Name(), padding), 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 += fmt.Sprintf(" %s%s %s\n", rpad(c.Name(), padding), intend, c.Short)
}
}

Expand All @@ -114,7 +149,6 @@ func buildGroupCommandHelp(cmd *cobra.Command, group *cobra.Group) string {
return help
}


func PrintRootHelpMessage(cmd *cobra.Command) error {
help := ""
help = fmt.Sprintf("%s\n\n%s", cmd.Short, help)
Expand Down
2 changes: 1 addition & 1 deletion config/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type Command struct {
Name string
Name string
GroupName string
// Represents a list of commands (scripts)
Cmds Cmds
Expand Down
4 changes: 2 additions & 2 deletions tests/command_group.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Commands:
d d command

Internal commands:
help Help about any command
self Manage lets CLI itself
help Help about any command
self Manage lets CLI itself

Flags:
--all show all commands (including the ones with _)
Expand Down
70 changes: 70 additions & 0 deletions tests/command_group_long.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
load test_helpers

setup() {
load "${BATS_UTILS_PATH}/bats-support/load.bash"
load "${BATS_UTILS_PATH}/bats-assert/load.bash"
cd ./tests/command_group_long
}

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
super_long_command_longer_than_usual Super long 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"
}
25 changes: 25 additions & 0 deletions tests/command_group_long/lets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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

super_long_command_longer_than_usual:
description: Super long command
cmd: echo "long command"
48 changes: 48 additions & 0 deletions tests/help_long.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
load test_helpers

setup() {
load "${BATS_UTILS_PATH}/bats-support/load.bash"
load "${BATS_UTILS_PATH}/bats-assert/load.bash"
cd ./tests/help_long
}

HELP_MESSAGE=$(cat <<EOF
A CLI task runner

Usage:
lets [flags]
lets [command]

Commands:
bar Print bar
foo Print foo
super_long_command_longer_than_usual Super long 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: run 'lets' as is" {
run lets
assert_success

assert_output "$HELP_MESSAGE"
}
18 changes: 18 additions & 0 deletions tests/help_long/lets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
shell: bash

commands:
_x:
description: Hidden x
cmd: echo "x"

foo:
description: Print foo
cmd: echo "Foo"

bar:
description: Print bar
cmd: echo "Bar"

super_long_command_longer_than_usual:
description: Super long command
cmd: echo "long command"