Compare commits
No commits in common. "9a52b5dce1bbab451534bd0d13dab05cde097942" and "955c96650ae6ecbb3b0b25d0f8838e85584a801e" have entirely different histories.
9a52b5dce1
...
955c96650a
2 changed files with 14 additions and 143 deletions
|
|
@ -58,7 +58,6 @@ type Options struct {
|
||||||
Aliases map[string][]string
|
Aliases map[string][]string
|
||||||
AliasDescriptions map[string]string
|
AliasDescriptions map[string]string
|
||||||
EnableDoctorAlias bool
|
EnableDoctorAlias bool
|
||||||
DisabledCommands []string
|
|
||||||
Args []string
|
Args []string
|
||||||
Stdin io.Reader
|
Stdin io.Reader
|
||||||
Stdout io.Writer
|
Stdout io.Writer
|
||||||
|
|
@ -147,10 +146,6 @@ func Run(ctx context.Context, opts Options) error {
|
||||||
return printHelp(normalized, "")
|
return printHelp(normalized, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
if isCommandDisabled(command, normalized.DisabledCommands) {
|
|
||||||
return fmt.Errorf("%w: %s", ErrUnknownCommand, command)
|
|
||||||
}
|
|
||||||
|
|
||||||
if command == CommandConfig {
|
if command == CommandConfig {
|
||||||
return runConfigCommand(ctx, normalized, commandArgs)
|
return runConfigCommand(ctx, normalized, commandArgs)
|
||||||
}
|
}
|
||||||
|
|
@ -205,11 +200,6 @@ func normalize(opts Options) Options {
|
||||||
}
|
}
|
||||||
opts.Aliases = normalizeAliases(opts.Aliases, opts.EnableDoctorAlias)
|
opts.Aliases = normalizeAliases(opts.Aliases, opts.EnableDoctorAlias)
|
||||||
opts.AliasDescriptions = normalizeAliasDescriptions(opts.AliasDescriptions, 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
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -466,10 +456,6 @@ func printHelp(opts Options, command string, args ...[]string) error {
|
||||||
return printGlobalHelp(opts)
|
return printGlobalHelp(opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
if isCommandDisabled(command, opts.DisabledCommands) {
|
|
||||||
return fmt.Errorf("%w: %s", ErrUnknownCommand, command)
|
|
||||||
}
|
|
||||||
|
|
||||||
if command == CommandConfig {
|
if command == CommandConfig {
|
||||||
return printConfigHelp(opts, commandArgs)
|
return printConfigHelp(opts, commandArgs)
|
||||||
}
|
}
|
||||||
|
|
@ -549,9 +535,6 @@ func printGlobalHelp(opts Options) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, def := range commands {
|
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 {
|
if _, err := fmt.Fprintf(opts.Stdout, " %-7s %s\n", def.Name, def.Description); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -588,39 +571,6 @@ func printGlobalHelp(opts Options) error {
|
||||||
return err
|
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 {
|
func aliasDescription(descriptions map[string]string, name, target string) string {
|
||||||
description := strings.TrimSpace(descriptions[name])
|
description := strings.TrimSpace(descriptions[name])
|
||||||
if description == "" {
|
if description == "" {
|
||||||
|
|
|
||||||
|
|
@ -91,13 +91,11 @@ func TestRunReturnsCommandNotConfigured(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Args: []string{"config"},
|
Args: []string{"config"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{ConfigShow: noop},
|
|
||||||
})
|
})
|
||||||
if !errors.Is(err, ErrSubcommandRequired) {
|
if !errors.Is(err, ErrSubcommandRequired) {
|
||||||
t.Fatalf("Run error = %v, want ErrSubcommandRequired", err)
|
t.Fatalf("Run error = %v, want ErrSubcommandRequired", err)
|
||||||
|
|
@ -150,7 +148,7 @@ func TestRunVersionHookOverridesDefault(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunVersionAutoHiddenWithoutHookOrVersionString(t *testing.T) {
|
func TestRunRequiresVersionWithoutVersionHook(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
|
|
@ -160,8 +158,8 @@ func TestRunVersionAutoHiddenWithoutHookOrVersionString(t *testing.T) {
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
})
|
})
|
||||||
if !errors.Is(err, ErrUnknownCommand) {
|
if !errors.Is(err, ErrVersionRequired) {
|
||||||
t.Fatalf("Run error = %v, want ErrUnknownCommand", err)
|
t.Fatalf("Run error = %v, want ErrVersionRequired", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -169,15 +167,12 @@ func TestRunPrintsGlobalHelp(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Description: "Binaire MCP de test.",
|
Description: "Binaire MCP de test.",
|
||||||
Version: "v1.2.3",
|
|
||||||
Args: []string{"help"},
|
Args: []string{"help"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{Setup: noop, MCP: noop, ConfigShow: noop, Update: noop},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Run error = %v", err)
|
t.Fatalf("Run error = %v", err)
|
||||||
|
|
@ -202,15 +197,12 @@ func TestRunPrintsGlobalHelpWhenNoArgs(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Description: "Binaire MCP de test.",
|
Description: "Binaire MCP de test.",
|
||||||
Version: "v1.2.3",
|
|
||||||
Args: []string{},
|
Args: []string{},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{Setup: noop, MCP: noop, ConfigShow: noop, Update: noop},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Run error = %v", err)
|
t.Fatalf("Run error = %v", err)
|
||||||
|
|
@ -235,13 +227,11 @@ func TestRunPrintsCommandHelp(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Args: []string{"help", "update"},
|
Args: []string{"help", "update"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{Update: noop},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Run error = %v", err)
|
t.Fatalf("Run error = %v", err)
|
||||||
|
|
@ -260,13 +250,11 @@ func TestRunPrintsLoginCommandHelp(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Args: []string{"help", "login"},
|
Args: []string{"help", "login"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{Login: noop},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Run error = %v", err)
|
t.Fatalf("Run error = %v", err)
|
||||||
|
|
@ -285,13 +273,11 @@ func TestRunPrintsConfigHelp(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Args: []string{"help", "config"},
|
Args: []string{"help", "config"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{ConfigShow: noop},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Run error = %v", err)
|
t.Fatalf("Run error = %v", err)
|
||||||
|
|
@ -313,13 +299,11 @@ func TestRunPrintsConfigSubcommandHelp(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Args: []string{"help", "config", "show"},
|
Args: []string{"help", "config", "show"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{ConfigShow: noop},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Run error = %v", err)
|
t.Fatalf("Run error = %v", err)
|
||||||
|
|
@ -365,13 +349,11 @@ func TestRunConfigShowReturnsCommandNotConfigured(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Args: []string{"config", "show"},
|
Args: []string{"config", "show"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{ConfigTest: noop},
|
|
||||||
})
|
})
|
||||||
if !errors.Is(err, ErrCommandNotConfigured) {
|
if !errors.Is(err, ErrCommandNotConfigured) {
|
||||||
t.Fatalf("Run error = %v, want ErrCommandNotConfigured", err)
|
t.Fatalf("Run error = %v, want ErrCommandNotConfigured", err)
|
||||||
|
|
@ -382,13 +364,11 @@ func TestRunConfigReturnsUnknownSubcommand(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Args: []string{"config", "sync"},
|
Args: []string{"config", "sync"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{ConfigShow: noop},
|
|
||||||
})
|
})
|
||||||
if !errors.Is(err, ErrUnknownSubcommand) {
|
if !errors.Is(err, ErrUnknownSubcommand) {
|
||||||
t.Fatalf("Run error = %v, want ErrUnknownSubcommand", err)
|
t.Fatalf("Run error = %v, want ErrUnknownSubcommand", err)
|
||||||
|
|
@ -432,7 +412,6 @@ func TestRunPrintsAliasHelpFromHelpCommand(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Aliases: map[string][]string{
|
Aliases: map[string][]string{
|
||||||
|
|
@ -441,7 +420,6 @@ func TestRunPrintsAliasHelpFromHelpCommand(t *testing.T) {
|
||||||
Args: []string{"help", "doctor"},
|
Args: []string{"help", "doctor"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{ConfigTest: noop},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Run error = %v", err)
|
t.Fatalf("Run error = %v", err)
|
||||||
|
|
@ -457,7 +435,6 @@ func TestRunPrintsAliasHelpFromTrailingHelpFlag(t *testing.T) {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
var stderr bytes.Buffer
|
var stderr bytes.Buffer
|
||||||
|
|
||||||
noop := func(_ context.Context, _ Invocation) error { return nil }
|
|
||||||
err := Run(context.Background(), Options{
|
err := Run(context.Background(), Options{
|
||||||
BinaryName: "my-mcp",
|
BinaryName: "my-mcp",
|
||||||
Aliases: map[string][]string{
|
Aliases: map[string][]string{
|
||||||
|
|
@ -466,7 +443,6 @@ func TestRunPrintsAliasHelpFromTrailingHelpFlag(t *testing.T) {
|
||||||
Args: []string{"doctor", "--help"},
|
Args: []string{"doctor", "--help"},
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
Stderr: &stderr,
|
Stderr: &stderr,
|
||||||
Hooks: Hooks{ConfigTest: noop},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Run error = %v", err)
|
t.Fatalf("Run error = %v", err)
|
||||||
|
|
@ -631,58 +607,3 @@ func TestRunAcceptsGlobalDebugFlagAfterCommand(t *testing.T) {
|
||||||
t.Fatalf("MCP_FRAMEWORK_BITWARDEN_DEBUG = %q, want %q", os.Getenv("MCP_FRAMEWORK_BITWARDEN_DEBUG"), "1")
|
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