Compare commits
2 commits
955c96650a
...
9a52b5dce1
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a52b5dce1 | |||
| 4a7248cfa9 |
2 changed files with 143 additions and 14 deletions
|
|
@ -52,17 +52,18 @@ type Hooks struct {
|
|||
}
|
||||
|
||||
type Options struct {
|
||||
BinaryName string
|
||||
Description string
|
||||
Version string
|
||||
Aliases map[string][]string
|
||||
AliasDescriptions map[string]string
|
||||
EnableDoctorAlias bool
|
||||
Args []string
|
||||
Stdin io.Reader
|
||||
Stdout io.Writer
|
||||
Stderr io.Writer
|
||||
Hooks Hooks
|
||||
BinaryName string
|
||||
Description string
|
||||
Version string
|
||||
Aliases map[string][]string
|
||||
AliasDescriptions map[string]string
|
||||
EnableDoctorAlias bool
|
||||
DisabledCommands []string
|
||||
Args []string
|
||||
Stdin io.Reader
|
||||
Stdout io.Writer
|
||||
Stderr io.Writer
|
||||
Hooks Hooks
|
||||
}
|
||||
|
||||
type Invocation struct {
|
||||
|
|
@ -146,6 +147,10 @@ func Run(ctx context.Context, opts Options) error {
|
|||
return printHelp(normalized, "")
|
||||
}
|
||||
|
||||
if isCommandDisabled(command, normalized.DisabledCommands) {
|
||||
return fmt.Errorf("%w: %s", ErrUnknownCommand, command)
|
||||
}
|
||||
|
||||
if command == CommandConfig {
|
||||
return runConfigCommand(ctx, normalized, commandArgs)
|
||||
}
|
||||
|
|
@ -200,6 +205,11 @@ func normalize(opts Options) Options {
|
|||
}
|
||||
opts.Aliases = normalizeAliases(opts.Aliases, opts.EnableDoctorAlias)
|
||||
opts.AliasDescriptions = normalizeAliasDescriptions(opts.AliasDescriptions, opts.Aliases, opts.EnableDoctorAlias)
|
||||
for _, cmd := range autoDisabledCommands(opts) {
|
||||
if !isCommandDisabled(cmd, opts.DisabledCommands) {
|
||||
opts.DisabledCommands = append(opts.DisabledCommands, cmd)
|
||||
}
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
|
|
@ -456,6 +466,10 @@ func printHelp(opts Options, command string, args ...[]string) error {
|
|||
return printGlobalHelp(opts)
|
||||
}
|
||||
|
||||
if isCommandDisabled(command, opts.DisabledCommands) {
|
||||
return fmt.Errorf("%w: %s", ErrUnknownCommand, command)
|
||||
}
|
||||
|
||||
if command == CommandConfig {
|
||||
return printConfigHelp(opts, commandArgs)
|
||||
}
|
||||
|
|
@ -535,6 +549,9 @@ func printGlobalHelp(opts Options) error {
|
|||
}
|
||||
|
||||
for _, def := range commands {
|
||||
if isCommandDisabled(def.Name, opts.DisabledCommands) {
|
||||
continue
|
||||
}
|
||||
if _, err := fmt.Fprintf(opts.Stdout, " %-7s %s\n", def.Name, def.Description); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -571,6 +588,39 @@ func printGlobalHelp(opts Options) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func autoDisabledCommands(opts Options) []string {
|
||||
h := opts.Hooks
|
||||
var disabled []string
|
||||
if h.Setup == nil {
|
||||
disabled = append(disabled, CommandSetup)
|
||||
}
|
||||
if h.Login == nil {
|
||||
disabled = append(disabled, CommandLogin)
|
||||
}
|
||||
if h.MCP == nil {
|
||||
disabled = append(disabled, CommandMCP)
|
||||
}
|
||||
if h.Config == nil && h.ConfigShow == nil && h.ConfigTest == nil && h.ConfigDelete == nil {
|
||||
disabled = append(disabled, CommandConfig)
|
||||
}
|
||||
if h.Update == nil {
|
||||
disabled = append(disabled, CommandUpdate)
|
||||
}
|
||||
if h.Version == nil && strings.TrimSpace(opts.Version) == "" {
|
||||
disabled = append(disabled, CommandVersion)
|
||||
}
|
||||
return disabled
|
||||
}
|
||||
|
||||
func isCommandDisabled(command string, disabled []string) bool {
|
||||
for _, d := range disabled {
|
||||
if d == command {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func aliasDescription(descriptions map[string]string, name, target string) string {
|
||||
description := strings.TrimSpace(descriptions[name])
|
||||
if description == "" {
|
||||
|
|
|
|||
|
|
@ -91,11 +91,13 @@ func TestRunReturnsCommandNotConfigured(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Args: []string{"config"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{ConfigShow: noop},
|
||||
})
|
||||
if !errors.Is(err, ErrSubcommandRequired) {
|
||||
t.Fatalf("Run error = %v, want ErrSubcommandRequired", err)
|
||||
|
|
@ -148,7 +150,7 @@ func TestRunVersionHookOverridesDefault(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRunRequiresVersionWithoutVersionHook(t *testing.T) {
|
||||
func TestRunVersionAutoHiddenWithoutHookOrVersionString(t *testing.T) {
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
|
|
@ -158,8 +160,8 @@ func TestRunRequiresVersionWithoutVersionHook(t *testing.T) {
|
|||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
})
|
||||
if !errors.Is(err, ErrVersionRequired) {
|
||||
t.Fatalf("Run error = %v, want ErrVersionRequired", err)
|
||||
if !errors.Is(err, ErrUnknownCommand) {
|
||||
t.Fatalf("Run error = %v, want ErrUnknownCommand", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -167,12 +169,15 @@ func TestRunPrintsGlobalHelp(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Description: "Binaire MCP de test.",
|
||||
Version: "v1.2.3",
|
||||
Args: []string{"help"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{Setup: noop, MCP: noop, ConfigShow: noop, Update: noop},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error = %v", err)
|
||||
|
|
@ -197,12 +202,15 @@ func TestRunPrintsGlobalHelpWhenNoArgs(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Description: "Binaire MCP de test.",
|
||||
Version: "v1.2.3",
|
||||
Args: []string{},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{Setup: noop, MCP: noop, ConfigShow: noop, Update: noop},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error = %v", err)
|
||||
|
|
@ -227,11 +235,13 @@ func TestRunPrintsCommandHelp(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Args: []string{"help", "update"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{Update: noop},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error = %v", err)
|
||||
|
|
@ -250,11 +260,13 @@ func TestRunPrintsLoginCommandHelp(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Args: []string{"help", "login"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{Login: noop},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error = %v", err)
|
||||
|
|
@ -273,11 +285,13 @@ func TestRunPrintsConfigHelp(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Args: []string{"help", "config"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{ConfigShow: noop},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error = %v", err)
|
||||
|
|
@ -299,11 +313,13 @@ func TestRunPrintsConfigSubcommandHelp(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Args: []string{"help", "config", "show"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{ConfigShow: noop},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error = %v", err)
|
||||
|
|
@ -349,11 +365,13 @@ func TestRunConfigShowReturnsCommandNotConfigured(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Args: []string{"config", "show"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{ConfigTest: noop},
|
||||
})
|
||||
if !errors.Is(err, ErrCommandNotConfigured) {
|
||||
t.Fatalf("Run error = %v, want ErrCommandNotConfigured", err)
|
||||
|
|
@ -364,11 +382,13 @@ func TestRunConfigReturnsUnknownSubcommand(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Args: []string{"config", "sync"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{ConfigShow: noop},
|
||||
})
|
||||
if !errors.Is(err, ErrUnknownSubcommand) {
|
||||
t.Fatalf("Run error = %v, want ErrUnknownSubcommand", err)
|
||||
|
|
@ -412,6 +432,7 @@ func TestRunPrintsAliasHelpFromHelpCommand(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Aliases: map[string][]string{
|
||||
|
|
@ -420,6 +441,7 @@ func TestRunPrintsAliasHelpFromHelpCommand(t *testing.T) {
|
|||
Args: []string{"help", "doctor"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{ConfigTest: noop},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error = %v", err)
|
||||
|
|
@ -435,6 +457,7 @@ func TestRunPrintsAliasHelpFromTrailingHelpFlag(t *testing.T) {
|
|||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Aliases: map[string][]string{
|
||||
|
|
@ -443,6 +466,7 @@ func TestRunPrintsAliasHelpFromTrailingHelpFlag(t *testing.T) {
|
|||
Args: []string{"doctor", "--help"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
Hooks: Hooks{ConfigTest: noop},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Run error = %v", err)
|
||||
|
|
@ -607,3 +631,58 @@ func TestRunAcceptsGlobalDebugFlagAfterCommand(t *testing.T) {
|
|||
t.Fatalf("MCP_FRAMEWORK_BITWARDEN_DEBUG = %q, want %q", os.Getenv("MCP_FRAMEWORK_BITWARDEN_DEBUG"), "1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisabledCommandReturnsUnknown(t *testing.T) {
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
err := Run(context.Background(), Options{
|
||||
BinaryName: "my-mcp",
|
||||
Args: []string{"login"},
|
||||
DisabledCommands: []string{"login"},
|
||||
Stdout: &stdout,
|
||||
Stderr: &stderr,
|
||||
})
|
||||
|
||||
if !errors.Is(err, ErrUnknownCommand) {
|
||||
t.Fatalf("err = %v, want ErrUnknownCommand", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisabledCommandHiddenFromHelp(t *testing.T) {
|
||||
var stdout bytes.Buffer
|
||||
|
||||
err := printGlobalHelp(Options{
|
||||
BinaryName: "my-mcp",
|
||||
DisabledCommands: []string{"login", "setup"},
|
||||
Stdout: &stdout,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("printGlobalHelp error = %v", err)
|
||||
}
|
||||
|
||||
out := stdout.String()
|
||||
if strings.Contains(out, "login") {
|
||||
t.Fatalf("help should not contain disabled command %q, got:\n%s", "login", out)
|
||||
}
|
||||
if strings.Contains(out, "setup") {
|
||||
t.Fatalf("help should not contain disabled command %q, got:\n%s", "setup", out)
|
||||
}
|
||||
if !strings.Contains(out, "mcp") {
|
||||
t.Fatalf("help should contain enabled command %q, got:\n%s", "mcp", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisabledCommandHelpReturnsUnknown(t *testing.T) {
|
||||
var stdout bytes.Buffer
|
||||
|
||||
err := printHelp(Options{
|
||||
BinaryName: "my-mcp",
|
||||
DisabledCommands: []string{"login"},
|
||||
Stdout: &stdout,
|
||||
}, "login")
|
||||
|
||||
if !errors.Is(err, ErrUnknownCommand) {
|
||||
t.Fatalf("err = %v, want ErrUnknownCommand", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue