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
3 changes: 3 additions & 0 deletions cli/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,9 @@ Warning: These commands are intended for advanced users and developers. They may
// Internal commands (hidden from help, used by miren internals)
d.Dispatch("internal", Section("internal", "Internal commands used by miren components", ""))

// Help command (registered last so it can reference all other commands)
d.Dispatch("help", NewHelpCommand(d))

addCommands(d)
}

Expand Down
121 changes: 121 additions & 0 deletions cli/commands/help_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package commands

import (
"fmt"
"os"
"strings"
"text/tabwriter"

"miren.dev/mflags"
)

type helpOpts struct {
FormatOptions
ListCommands bool `long:"commands" description:"List all commands with their synopsis"`
Commands []string `rest:"commands"`
}

// commandSummary is used for JSON output of --commands.
type commandSummary struct {
Path string `json:"path"`
Usage string `json:"usage"`
}

// NewHelpCommand returns an Infer-based help command bound to the given dispatcher.
func NewHelpCommand(d *mflags.Dispatcher) *Cmd {
return Infer("help", "Show help for one or more commands",
func(ctx *Context, opts helpOpts) error {
if opts.ListCommands {
return runListCommands(d, opts)
}
if len(opts.Commands) > 0 {
return runMultiHelp(d, opts)
}
return mflags.ErrShowHelp
},
WithExample(mflags.Example{
Name: "List all commands",
Body: "miren help --commands",
}),
WithExample(mflags.Example{
Name: "List all commands as JSON",
Body: "miren help --commands --format json",
}),
WithExample(mflags.Example{
Name: "Show help for multiple commands",
Body: "miren help app.list version sandbox.stop",
}),
)
}

func runListCommands(d *mflags.Dispatcher, opts helpOpts) error {
doc := d.HelpDoc()

if opts.IsJSON() {
var summaries []commandSummary
collectSummaries(doc.Commands, &summaries)
return PrintJSON(summaries)
}

w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
printSummaryRows(w, doc.Commands)
return w.Flush()
}

func collectSummaries(cmds []mflags.CommandDoc, out *[]commandSummary) {
for _, cmd := range cmds {
*out = append(*out, commandSummary{
Path: cmd.Path,
Usage: cmd.Usage,
})
collectSummaries(cmd.Subcommands, out)
}
}

func printSummaryRows(w *tabwriter.Writer, cmds []mflags.CommandDoc) {
for _, cmd := range cmds {
fmt.Fprintf(w, "%s\t%s\n", cmd.Path, cmd.Usage)
printSummaryRows(w, cmd.Subcommands)
}
}

func runMultiHelp(d *mflags.Dispatcher, opts helpOpts) error {
if opts.IsJSON() {
doc := d.HelpDoc()
index := make(map[string]*mflags.CommandDoc)
indexCommands(doc.Commands, index)

var results []mflags.CommandDoc
for _, name := range opts.Commands {
path := strings.ReplaceAll(name, ".", " ")
cmd, ok := index[path]
if !ok {
return fmt.Errorf("unknown command: %s", name)
}
results = append(results, *cmd)
}
return PrintJSON(results)
}

for i, name := range opts.Commands {
path := strings.ReplaceAll(name, ".", " ")

if i > 0 {
fmt.Println()
fmt.Println("---")
fmt.Println()
}

if err := d.ShowCommandHelp(path); err != nil {
return fmt.Errorf("unknown command: %s", name)
}
}
return nil
}

func indexCommands(cmds []mflags.CommandDoc, index map[string]*mflags.CommandDoc) {
for i := range cmds {
index[cmds[i].Path] = &cmds[i]
indexCommands(cmds[i].Subcommands, index)
}
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ require (
golang.org/x/net v0.49.0
golang.org/x/oauth2 v0.32.0
golang.org/x/sync v0.19.0
golang.org/x/sys v0.41.0
golang.org/x/term v0.40.0
golang.org/x/sys v0.42.0
golang.org/x/term v0.41.0
golang.org/x/text v0.34.0
golang.org/x/time v0.14.0
golang.org/x/tools v0.41.0
Expand All @@ -124,7 +124,7 @@ require (
k8s.io/klog/v2 v2.130.1
miren.dev/jsonrpc3/go/jsonrpc3 v0.0.0-20260106052505-c98e2702b093
miren.dev/lbd v0.0.0-20260224020427-8914d8db2233
miren.dev/mflags v0.0.0-20260223221956-2ab5ba72a45d
miren.dev/mflags v0.0.0-20260313175018-d9ee90a8bb13
)

require github.com/aws/aws-sdk-go-v2/service/signin v1.0.5 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1656,8 +1656,8 @@ golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand All @@ -1672,8 +1672,8 @@ golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.40.0 h1:36e4zGLqU4yhjlmxEaagx2KuYbJq3EwY8K943ZsHcvg=
golang.org/x/term v0.40.0/go.mod h1:w2P8uVp06p2iyKKuvXIm7N/y0UCRt3UfJTfZ7oOpglM=
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -1931,8 +1931,8 @@ miren.dev/jsonrpc3/go/jsonrpc3 v0.0.0-20260106052505-c98e2702b093 h1:Sd+M5HSUati
miren.dev/jsonrpc3/go/jsonrpc3 v0.0.0-20260106052505-c98e2702b093/go.mod h1:B4A3wSzkcSZQw6Y5opU+rk1+1lEuCWIWkAiN7TkltGE=
miren.dev/lbd v0.0.0-20260224020427-8914d8db2233 h1:9DxH7Dhnmu7hn1OA2JC5fHpLuVgAqBySws9GLNssLl4=
miren.dev/lbd v0.0.0-20260224020427-8914d8db2233/go.mod h1:+x9fy2p45csBnGUJdqxCUmzlUTCipoVDbv6zIapTgDA=
miren.dev/mflags v0.0.0-20260223221956-2ab5ba72a45d h1:+GRdLd/WRVsZAQT3qX+ywaraqdUijQ5vyHTfmCAfG+0=
miren.dev/mflags v0.0.0-20260223221956-2ab5ba72a45d/go.mod h1:G1eQ/upWVdO6BGT6dlh5Yqjt+9ncH5RUAKX6UKi1F9Q=
miren.dev/mflags v0.0.0-20260313175018-d9ee90a8bb13 h1:QPWolXOmZOvh5CBG0i+7QWxBgTzT0nwDlRMHgMpo9P8=
miren.dev/mflags v0.0.0-20260313175018-d9ee90a8bb13/go.mod h1:G1eQ/upWVdO6BGT6dlh5Yqjt+9ncH5RUAKX6UKi1F9Q=
modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI=
modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
Expand Down
Loading