Skip to content
Merged
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
24 changes: 5 additions & 19 deletions internal/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,11 @@ func registerAllFlags(cmd *cobra.Command) {

// registerFlagCompletions registers custom completion functions for flags
func registerFlagCompletions(cmd *cobra.Command) {
// Custom completion for --config flag (complete with .toml files)
cmd.RegisterFlagCompletionFunc("config", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"toml"}, cobra.ShellCompDirectiveFilterFileExt
})

// Custom completion for --log-dir flag (complete with directories)
cmd.RegisterFlagCompletionFunc("log-dir", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveFilterDirs
})

// Custom completion for --payload-dir flag (complete with directories)
cmd.RegisterFlagCompletionFunc("payload-dir", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveFilterDirs
})

// Custom completion for --env flag (complete with .env files)
cmd.RegisterFlagCompletionFunc("env", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"env"}, cobra.ShellCompDirectiveFilterFileExt
})
// File and directory completions using idiomatic cobra helpers
cmd.MarkFlagFilename("config", "toml")
cmd.MarkFlagDirname("log-dir")
cmd.MarkFlagDirname("payload-dir")
cmd.MarkFlagFilename("env", "env")

// Enum completions for DIFC flags
cmd.RegisterFlagCompletionFunc("guards-mode", cobra.FixedCompletions(
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var rootCmd = &cobra.Command{
Version: cliVersion,
Long: `MCPG is a proxy server for Model Context Protocol (MCP) servers.
It provides routing, aggregation, and management of multiple MCP backend servers.`,
Args: cobra.NoArgs,
SilenceUsage: true, // Don't show help on runtime errors
SilenceErrors: true, // Prevent cobra from printing errors — Execute() caller handles display
PersistentPreRunE: preRun,
Expand Down
18 changes: 9 additions & 9 deletions internal/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ func TestRunRequiresConfigSource(t *testing.T) {
t.Run("config file provided", func(t *testing.T) {
configFile = "test.toml"
configStdin = false
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
// Should pass validation when --config is provided
assert.NoError(t, err, "Should not error when --config is provided")
})

t.Run("config stdin provided", func(t *testing.T) {
configFile = ""
configStdin = true
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
// Should pass validation when --config-stdin is provided
assert.NoError(t, err, "Should not error when --config-stdin is provided")
})

t.Run("both config file and stdin provided", func(t *testing.T) {
configFile = "test.toml"
configStdin = true
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
// When both are provided, should pass validation
assert.NoError(t, err, "Should not error when both are provided")
})
Expand All @@ -78,15 +78,15 @@ func TestPreRunValidation(t *testing.T) {
configFile = "test.toml"
configStdin = false
verbosity = 0
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
assert.NoError(t, err)
})

t.Run("validation passes with config stdin", func(t *testing.T) {
configFile = ""
configStdin = true
verbosity = 0
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
assert.NoError(t, err)
})

Expand All @@ -108,7 +108,7 @@ func TestPreRunValidation(t *testing.T) {
configFile = "test.toml"
configStdin = false
verbosity = 1
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
assert.NoError(t, err)
// Level 1 doesn't set DEBUG env var
assert.Empty(t, os.Getenv(logger.EnvDebug))
Expand All @@ -129,7 +129,7 @@ func TestPreRunValidation(t *testing.T) {
configFile = "test.toml"
configStdin = false
verbosity = 2
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
assert.NoError(t, err)
assert.Equal(t, "cmd:*,server:*,launcher:*", os.Getenv(logger.EnvDebug))
})
Expand All @@ -149,7 +149,7 @@ func TestPreRunValidation(t *testing.T) {
configFile = "test.toml"
configStdin = false
verbosity = 3
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
assert.NoError(t, err)
assert.Equal(t, "*", os.Getenv(logger.EnvDebug))
})
Expand All @@ -169,7 +169,7 @@ func TestPreRunValidation(t *testing.T) {
configFile = "test.toml"
configStdin = false
verbosity = 2
err := preRun(nil, nil)
err := preRun(&cobra.Command{}, nil)
assert.NoError(t, err)
// Should not override existing DEBUG
assert.Equal(t, "custom:*", os.Getenv(logger.EnvDebug))
Expand Down
Loading