diff --git a/cmd/docker-mcp/commands/catalog_next.go b/cmd/docker-mcp/commands/catalog_next.go index 42ee735d..23162a6f 100644 --- a/cmd/docker-mcp/commands/catalog_next.go +++ b/cmd/docker-mcp/commands/catalog_next.go @@ -9,25 +9,14 @@ import ( catalognext "github.com/docker/mcp-gateway/pkg/catalog_next" "github.com/docker/mcp-gateway/pkg/db" - "github.com/docker/mcp-gateway/pkg/docker" - "github.com/docker/mcp-gateway/pkg/migrate" "github.com/docker/mcp-gateway/pkg/oci" "github.com/docker/mcp-gateway/pkg/workingset" ) -func catalogNextCommand(docker docker.Client) *cobra.Command { +func catalogNextCommand() *cobra.Command { cmd := &cobra.Command{ Use: "catalog-next", Short: "Manage catalogs (next generation)", - PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { - dao, err := db.New() - if err != nil { - return err - } - defer dao.Close() - migrate.MigrateConfig(cmd.Context(), docker, dao) - return nil - }, } cmd.AddCommand(createCatalogNextCommand()) diff --git a/cmd/docker-mcp/commands/root.go b/cmd/docker-mcp/commands/root.go index 24bf6eaf..e9d5a441 100644 --- a/cmd/docker-mcp/commands/root.go +++ b/cmd/docker-mcp/commands/root.go @@ -3,14 +3,17 @@ package commands import ( "context" "os" + "slices" "github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli/command" "github.com/spf13/cobra" "github.com/docker/mcp-gateway/cmd/docker-mcp/version" + "github.com/docker/mcp-gateway/pkg/db" "github.com/docker/mcp-gateway/pkg/desktop" "github.com/docker/mcp-gateway/pkg/docker" + "github.com/docker/mcp-gateway/pkg/migrate" ) // Note: We use a custom help template to make it more brief. @@ -31,6 +34,8 @@ Examples: // Root returns the root command for the init plugin func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command { + dockerClient := docker.NewClient(dockerCli) + cmd := &cobra.Command{ Use: "mcp [OPTIONS]", Short: "Manage MCP servers and clients", @@ -46,6 +51,17 @@ func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command } if os.Getenv("DOCKER_MCP_IN_CONTAINER") != "1" { + if isWorkingSetsFeatureEnabled(dockerCli) { + if isSubcommandOf(cmd, []string{"catalog-next", "catalog", "profile"}) { + dao, err := db.New() + if err != nil { + return err + } + defer dao.Close() + migrate.MigrateConfig(cmd.Context(), dockerClient, dao) + } + } + runningInDockerCE, err := docker.RunningInDockerCE(ctx, dockerCli) if err != nil { return err @@ -68,11 +84,9 @@ func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command return []string{"--help"}, cobra.ShellCompDirectiveNoFileComp }) - dockerClient := docker.NewClient(dockerCli) - if isWorkingSetsFeatureEnabled(dockerCli) { - cmd.AddCommand(workingSetCommand(dockerClient)) - cmd.AddCommand(catalogNextCommand(dockerClient)) + cmd.AddCommand(workingSetCommand()) + cmd.AddCommand(catalogNextCommand()) } cmd.AddCommand(catalogCommand(dockerCli)) cmd.AddCommand(clientCommand(dockerCli, cwd)) @@ -101,3 +115,15 @@ func unhideHiddenCommands(cmd *cobra.Command) { unhideHiddenCommands(c) } } + +func isSubcommandOf(cmd *cobra.Command, names []string) bool { + if cmd == nil { + return false + } + + if slices.Contains(names, cmd.Name()) { + return true + } + + return isSubcommandOf(cmd.Parent(), names) +} diff --git a/cmd/docker-mcp/commands/workingset.go b/cmd/docker-mcp/commands/workingset.go index d1482014..f30576cf 100644 --- a/cmd/docker-mcp/commands/workingset.go +++ b/cmd/docker-mcp/commands/workingset.go @@ -9,29 +9,18 @@ import ( "github.com/docker/mcp-gateway/pkg/client" "github.com/docker/mcp-gateway/pkg/db" - "github.com/docker/mcp-gateway/pkg/docker" - "github.com/docker/mcp-gateway/pkg/migrate" "github.com/docker/mcp-gateway/pkg/oci" "github.com/docker/mcp-gateway/pkg/registryapi" "github.com/docker/mcp-gateway/pkg/sliceutil" "github.com/docker/mcp-gateway/pkg/workingset" ) -func workingSetCommand(docker docker.Client) *cobra.Command { +func workingSetCommand() *cobra.Command { cfg := client.ReadConfig() cmd := &cobra.Command{ Use: "profile", Short: "Manage profiles", - PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { - dao, err := db.New() - if err != nil { - return err - } - defer dao.Close() - migrate.MigrateConfig(cmd.Context(), docker, dao) - return nil - }, } cmd.AddCommand(exportWorkingSetCommand())