feat(shortcuts): tag service groups with cmdmeta.Domain

RegisterShortcutsWithContext now calls cmdmeta.SetDomain on each
service-level cobra.Command (im, docs, drive, calendar, ...) so the
business-domain axis is actually populated on every shortcut leaf via
parent-chain inheritance.

Before this change, platform.ByDomain("docs") never matched any
command: the domain annotation was unset across the entire shortcut
tree, so the selector's d != "" guard always failed and risk-style
selectors silently degraded to no-op.

The SetDomain call is placed AFTER the create-or-reuse branch so it
fires whether the service command was freshly created here or had
already been added by cmd/service/service.go's OpenAPI auto-
registration (which runs first and creates im, drive, calendar, etc.).
Without this placement only pure-shortcut services like docs would
have been tagged.

Adds a regression test asserting:
  - service-group cobra.Command carries the cmdmeta.domain annotation
  - leaf shortcuts inherit the domain via parent-chain walk
This commit is contained in:
shanglei
2026-05-13 16:45:00 +08:00
parent 2bedf8cba9
commit b0e2d4de11
2 changed files with 45 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/larksuite/cli/shortcuts/okr"
"github.com/spf13/cobra"
"github.com/larksuite/cli/internal/cmdmeta"
"github.com/larksuite/cli/internal/cmdutil"
"github.com/larksuite/cli/internal/registry"
"github.com/larksuite/cli/shortcuts/base"
@@ -92,6 +93,18 @@ func RegisterShortcutsWithContext(ctx context.Context, program *cobra.Command, f
}
program.AddCommand(svc)
}
// Tag the service group with its domain so platform.ByDomain
// and Rule.Allow path-globs work without each leaf shortcut
// having to declare the domain itself: cmdmeta.Domain walks up
// the parent chain and stops at the first annotated ancestor
// (this command).
//
// Done OUTSIDE the create branch so the tag is still applied
// when the service command was pre-created by cmd/service
// (OpenAPI auto-registration adds im, drive, calendar, etc.
// before shortcuts run). Without this, only pure-shortcut
// services like `docs` would get tagged.
cmdmeta.SetDomain(svc, service)
if service == "docs" {
doc.ConfigureServiceHelp(svc)
}

View File

@@ -13,6 +13,7 @@ import (
"strings"
"testing"
"github.com/larksuite/cli/internal/cmdmeta"
"github.com/larksuite/cli/internal/cmdutil"
"github.com/larksuite/cli/internal/core"
"github.com/larksuite/cli/internal/output"
@@ -98,6 +99,37 @@ func TestRegisterShortcutsMountsBaseCommands(t *testing.T) {
}
}
// Service-level cobra commands created by RegisterShortcuts must carry
// the cmdmeta.Domain annotation so plugin Selectors (platform.ByDomain)
// and Rule.Allow path-globs can resolve a command's business domain.
// The annotation is set on the parent; cmdmeta.Domain walks up the
// parent chain so every leaf shortcut inherits without extra tagging.
func TestRegisterShortcutsTagsServiceDomain(t *testing.T) {
program := &cobra.Command{Use: "root"}
RegisterShortcuts(program, newRegisterTestFactory(t))
for _, svc := range []string{"im", "docs", "drive", "calendar", "base"} {
group, _, err := program.Find([]string{svc})
if err != nil || group == nil {
t.Errorf("service %q not mounted", svc)
continue
}
if got := cmdmeta.Domain(group); got != svc {
t.Errorf("service %q domain = %q, want %q", svc, got, svc)
}
}
// Inheritance: a leaf shortcut under a service must also resolve
// to the parent's domain via cmdmeta.Domain's parent-chain walk.
leaf, _, err := program.Find([]string{"im", "+messages-send"})
if err != nil || leaf == nil {
t.Fatalf("expected im/+messages-send to be mounted")
}
if got := cmdmeta.Domain(leaf); got != "im" {
t.Errorf("leaf domain via parent inheritance = %q, want %q", got, "im")
}
}
func TestRegisterShortcutsMountsDocsMediaPreview(t *testing.T) {
program := &cobra.Command{Use: "root"}
RegisterShortcuts(program, newRegisterTestFactory(t))