* feat(auth): support multi-profile login
* fix(auth): complete multi-org profile acceptance
* feat(auth): 完成多组织 profile 验收
* docs(auth): 补充多组织 Ralph 验收材料
* feat(auth): 支持 auth switch TUI 切换 profile
* feat(auth): logout 默认清理所有组织
* feat(auth): login 默认新增组织授权
* feat(profile): 使用 profile switch 切换组织
* docs(ralph): 更新 profile switch 验收材料
* fix(profile): 展示全部可切换组织
* feat(profile): support multi-org switch tui
* chore(install): add branch source installer
* fix(profile): keep global profile out of tool params
* feat(profile): support csv multi-profile runtime
* ci: add multi-profile e2e workflow
* ci: run multi-profile e2e on all branches
* docs: document multi-profile e2e ci gate
* docs: remove multi-profile test cases from pr
* ci: harden multi-profile e2e gates
* fix(auth): serialize profiles.json RMW and harden multi-profile persistence
Wrap all profiles.json read-modify-write paths (profile switch/use/remove,
status marking, token save, logout) in the existing dual-layer lock via a new
withProfilesLock helper. Split each writer into a public (locking) entry point
plus a lock-free *Locked variant so the non-reentrant lock is never re-acquired;
the refresh path (oauth_helpers) and the load-path legacy migration now call the
lock-free saver to avoid self-deadlock.
Also: write profiles.json and the token marker via per-write random temp names
(uuid) to stop concurrent writers from corrupting a fixed .tmp file; quarantine
an unparseable profiles.json and rebuild an empty config so the CLI can
self-heal instead of locking out auth reset/logout; make DeleteAllTokenData
proceed even if profiles.json cannot be read; and stop SyncLegacyTokenMirror
from deleting the legacy mirror on a transient keychain read error.
* fix(auth): do not fall back to a different org's legacy token slot
When no explicit --profile is given, LoadTokenDataForProfile resolves the
current/primary profile and reads its per-corp keychain slot. If that slot
read failed, the code silently fell through to the legacy single token slot,
which after any drift between the legacy mirror and the current profile could
belong to a different organization. The command would then run as the wrong
org with no indication to the user.
Reproduction (conceptual):
- profiles.json currentProfile = corpA
- corpA's keychain slot is unreadable, legacy single slot still holds corpB
- any read command (no --profile) silently used corpB's token
Fix: when a profile is resolved but its slot read fails and no --profile was
given, only fall back to the legacy single slot when its CorpID matches the
resolved profile (same org); otherwise return the original error instead of
acting as a different organization. The no-profile legacy path (pre-migration
installs with no resolved profile) is unchanged.
Tests:
- Covered by the existing internal/auth suite under go test -race; the
same-org fallback preserves the legacy-mirror case while the cross-org
case now surfaces the read error.
* feat(skill): document multi-org profile usage and always ship dws-shared
The skills had no guidance on the multi-profile capability, so an agent would
treat the CLI as single-org: when a lookup missed in the current org it would
give up or ask the user instead of searching other logged-in orgs. The multi
skill set also referenced a `dws-shared` prerequisite that was never actually
installed, and the only multi-org hints lived inline in three product skills.
This adds, in source only:
- A "multi-org / profile" section in the mono SKILL.md (concept, commands,
cross-org rule, aggregation, safety guardrails) plus a decision-tree entry,
trigger conditions, and a corrected logout danger-table row (logout removes
all orgs by default; removing the primary silently re-elects a new primary,
confirm before removing the primary).
- A standalone skills/multi/dingtalk-profile skill mirroring the same content.
- A new skills/multi/dws-shared skill that carries auth, global flags and the
multi-org rule, so every product skill's PREREQUISITE resolves and all
read/search skills inherit the cross-org behavior without per-skill edits.
- Cross-org fallback notes on dingtalk-aisearch / chat / contact.
To guarantee the prerequisite actually ships, multi-mode install now force-
includes dws-shared even when --skill / --exclude narrows the set (no-op when
the source has no dws-shared, preserving older layouts).
Tests:
- internal/app: TestP1SharedAlwaysIncludedWithSkillFilter installs with
`-s aitable` and asserts dws-shared still lands in the destination;
TestP1SharedNoopWhenAbsent guards the older-layout no-op.
- go test -race ./internal/auth/... ./internal/app/... passes.
---------
Co-authored-by: shangguanxuan.sgx <shangguanxuan.sgx@alibaba-inc.com>
Co-authored-by: qinze <audanye@gmail.com>
222 lines
6.1 KiB
Go
222 lines
6.1 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"text/tabwriter"
|
|
|
|
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/i18n"
|
|
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/tui"
|
|
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/edition"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
)
|
|
|
|
func configureRootHelp(root *cobra.Command) {
|
|
if root == nil {
|
|
return
|
|
}
|
|
|
|
// Replace the cobra-default English help command with a localized one so
|
|
// that both its listing short (shown in `dws --help`) and its own
|
|
// `dws help --help` long text follow the active locale.
|
|
root.SetHelpCommand(&cobra.Command{
|
|
Use: "help [command]",
|
|
Short: i18n.T("查看任意命令的帮助信息"),
|
|
Long: i18n.T("显示任意命令的帮助文案。\n" +
|
|
"用法:dws help [命令路径] 查看完整说明。"),
|
|
DisableAutoGenTag: true,
|
|
Run: func(c *cobra.Command, args []string) {
|
|
target, _, err := c.Root().Find(args)
|
|
if target == nil || err != nil {
|
|
c.Root().HelpFunc()(c.Root(), args)
|
|
return
|
|
}
|
|
target.InitDefaultHelpFlag()
|
|
_ = target.Help()
|
|
},
|
|
})
|
|
|
|
defaultHelpFunc := root.HelpFunc()
|
|
root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
|
|
if cmd != root {
|
|
defaultHelpFunc(cmd, args)
|
|
return
|
|
}
|
|
renderRootHelp(root)
|
|
})
|
|
}
|
|
|
|
func renderRootHelp(root *cobra.Command) {
|
|
services := visibleMCPRootCommands(root)
|
|
utilities := visibleUtilityRootCommands(root)
|
|
w := root.OutOrStdout()
|
|
|
|
_, _ = fmt.Fprintln(w, tui.Header("Workspace CLI", "DingTalk blue-white technical console"))
|
|
_, _ = fmt.Fprintln(w, tui.Rule(76))
|
|
_, _ = fmt.Fprintln(w)
|
|
|
|
if len(services) == 0 {
|
|
_, _ = fmt.Fprintf(w, "%s %s\n", tui.StateMark("warning"), tui.Warning("No MCP services discovered."))
|
|
_, _ = fmt.Fprintln(w)
|
|
} else {
|
|
_, _ = fmt.Fprintln(w, tui.Section("Discovered MCP Services:"))
|
|
_, _ = fmt.Fprintln(w)
|
|
|
|
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
|
for _, service := range services {
|
|
_, _ = fmt.Fprintf(tw, " %s %s\t%s\n", tui.StateMark("ok"), tui.Bold(service.Name()), tui.Dim(strings.TrimSpace(service.Short)))
|
|
}
|
|
_ = tw.Flush()
|
|
_, _ = fmt.Fprintln(w)
|
|
}
|
|
|
|
_, _ = fmt.Fprintln(w, tui.Section("Usage:"))
|
|
_, _ = fmt.Fprintf(w, " %s %s\n", tui.Bullet(), tui.White("dws <service> [command] [flags]"))
|
|
if len(utilities) > 0 {
|
|
_, _ = fmt.Fprintf(w, " %s %s\n", tui.Bullet(), tui.White("dws <command> [flags]"))
|
|
}
|
|
_, _ = fmt.Fprintln(w)
|
|
if len(utilities) > 0 {
|
|
_, _ = fmt.Fprintln(w, tui.Section("Utility Commands:"))
|
|
_, _ = fmt.Fprintln(w)
|
|
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
|
for _, utility := range utilities {
|
|
_, _ = fmt.Fprintf(tw, " %s %s\t%s\n", tui.Bullet(), tui.Bold(utility.Name()), tui.Dim(commandShort(utility)))
|
|
}
|
|
_ = tw.Flush()
|
|
_, _ = fmt.Fprintln(w)
|
|
}
|
|
renderRootGlobalFlags(root)
|
|
_, _ = fmt.Fprintf(w, "%s %s\n", tui.Key("Next"), `Use "dws <service> --help" for more information about a discovered MCP service or "dws <command> --help" for utility commands.`)
|
|
|
|
// Render root.Long after the command list so agents see the upgrade
|
|
// hint (or any other root-level guidance) after browsing all available
|
|
// commands and concluding none of them fit. Cobra's default help template
|
|
// would render Long automatically; the custom SetHelpFunc above replaces
|
|
// it and dropped this, so we restore it explicitly here.
|
|
if long := strings.TrimSpace(root.Long); long != "" {
|
|
_, _ = fmt.Fprintln(w)
|
|
_, _ = fmt.Fprintln(w, tui.Dim(long))
|
|
}
|
|
}
|
|
|
|
func renderRootGlobalFlags(root *cobra.Command) {
|
|
if root == nil {
|
|
return
|
|
}
|
|
flags := visiblePersistentFlags(root)
|
|
if len(flags) == 0 {
|
|
return
|
|
}
|
|
w := root.OutOrStdout()
|
|
_, _ = fmt.Fprintln(w, tui.Section("Global Flags:"))
|
|
_, _ = fmt.Fprintln(w)
|
|
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
|
for _, flag := range flags {
|
|
_, _ = fmt.Fprintf(tw, " %s\t%s\n", formatRootFlag(flag), tui.Dim(strings.TrimSpace(flag.Usage)))
|
|
}
|
|
_ = tw.Flush()
|
|
_, _ = fmt.Fprintln(w)
|
|
}
|
|
|
|
func visiblePersistentFlags(root *cobra.Command) []*pflag.Flag {
|
|
if root == nil {
|
|
return nil
|
|
}
|
|
flags := make([]*pflag.Flag, 0)
|
|
root.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
|
|
if flag == nil || flag.Hidden {
|
|
return
|
|
}
|
|
flags = append(flags, flag)
|
|
})
|
|
return flags
|
|
}
|
|
|
|
func formatRootFlag(flag *pflag.Flag) string {
|
|
if flag == nil {
|
|
return ""
|
|
}
|
|
name := "--" + flag.Name
|
|
if flag.Value != nil && flag.Value.Type() != "bool" {
|
|
name += " " + flag.Value.Type()
|
|
}
|
|
if flag.Shorthand == "" {
|
|
return " " + name
|
|
}
|
|
return "-" + flag.Shorthand + ", " + name
|
|
}
|
|
|
|
func commandShort(cmd *cobra.Command) string {
|
|
if cmd == nil {
|
|
return ""
|
|
}
|
|
short := strings.TrimSpace(cmd.Short)
|
|
if cmd.Name() == "help" && short == "Help about any command" {
|
|
return i18n.T("查看任意命令的帮助信息")
|
|
}
|
|
return short
|
|
}
|
|
|
|
// resolveVisibleProducts returns the set of top-level product IDs that should
|
|
// be treated as visible. It unions the edition's VisibleProducts hook (when
|
|
// set) with DirectRuntimeProductIDs(), so dynamically-registered products —
|
|
// including plugins loaded via AppendDynamicServer — are never silently hidden
|
|
// by a static VisibleProducts list.
|
|
func resolveVisibleProducts() map[string]bool {
|
|
allowed := map[string]bool{}
|
|
if fn := edition.Get().VisibleProducts; fn != nil {
|
|
for _, p := range fn() {
|
|
allowed[p] = true
|
|
}
|
|
}
|
|
for id := range DirectRuntimeProductIDs() {
|
|
allowed[id] = true
|
|
}
|
|
return allowed
|
|
}
|
|
|
|
func visibleMCPRootCommands(root *cobra.Command) []*cobra.Command {
|
|
if root == nil {
|
|
return nil
|
|
}
|
|
|
|
allowed := resolveVisibleProducts()
|
|
if len(allowed) == 0 {
|
|
return nil
|
|
}
|
|
|
|
commands := make([]*cobra.Command, 0)
|
|
for _, cmd := range root.Commands() {
|
|
if cmd == nil || cmd.Hidden {
|
|
continue
|
|
}
|
|
if !allowed[cmd.Name()] {
|
|
continue
|
|
}
|
|
commands = append(commands, cmd)
|
|
}
|
|
return commands
|
|
}
|
|
|
|
func visibleUtilityRootCommands(root *cobra.Command) []*cobra.Command {
|
|
if root == nil {
|
|
return nil
|
|
}
|
|
|
|
productCommands := resolveVisibleProducts()
|
|
|
|
commands := make([]*cobra.Command, 0)
|
|
for _, cmd := range root.Commands() {
|
|
if cmd == nil || cmd.Hidden {
|
|
continue
|
|
}
|
|
if productCommands[cmd.Name()] {
|
|
continue
|
|
}
|
|
commands = append(commands, cmd)
|
|
}
|
|
return commands
|
|
}
|