Skip to content
Open
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
16 changes: 15 additions & 1 deletion internal/cli/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -29,10 +30,23 @@ Use this from an agent rather than scraping the human --help output.`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
rt := RuntimeFrom(cmd.Context())
target, _, err := root.Find(args)
// `rc commands` emits colon-form capability IDs (apps:create), so
// accept them here as if space-separated.
tokens := make([]string, 0, len(args))
for _, a := range args {
tokens = append(tokens, strings.Split(a, ":")...)
}
target, rest, err := root.Find(tokens)
if err != nil {
return err
}
// Find never fails on unknown names; it stops at the deepest match
// and returns the leftovers, which for a pure command path means
// the name didn't resolve. Without this check an unknown name
// silently yields the root schema.
if len(rest) > 0 {
return fmt.Errorf("unknown command %q — run `rc commands` to list valid names", strings.Join(args, " "))
}
return rt.Out.RenderJSON(commandSchema(target))
},
}
Expand Down
24 changes: 24 additions & 0 deletions internal/cli/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,27 @@ func hasRunnableDescendant(c *cobra.Command) bool {
}
return false
}

func TestSchemaUnknownCommandErrors(t *testing.T) {
root := NewRootCmd("test")
root.SetArgs([]string{"schema", "apps:bogus"})
root.SetOut(&strings.Builder{})
root.SetErr(&strings.Builder{})
if err := root.Execute(); err == nil {
t.Fatal("expected an error for an unknown command name, got nil")
}
}

func TestSchemaAcceptsColonCapabilityIDs(t *testing.T) {
root := NewRootCmd("test")
var out strings.Builder
root.SetArgs([]string{"schema", "apps:create"})
root.SetOut(&out)
root.SetErr(&strings.Builder{})
if err := root.Execute(); err != nil {
t.Fatalf("schema apps:create: %v", err)
}
if !strings.Contains(out.String(), `"name": "create"`) {
t.Fatalf("expected the apps create schema, got: %s", out.String())
}
}
Loading