Skip to content

Commit 9aef39e

Browse files
authored
Fix a bug where plugin init was being overriden (#266)
* Fix plugin prerun not running. * Add feature check.
1 parent 36b94ea commit 9aef39e

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

cmd/docker-mcp/commands/catalog_next.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,14 @@ import (
99

1010
catalognext "github.com/docker/mcp-gateway/pkg/catalog_next"
1111
"github.com/docker/mcp-gateway/pkg/db"
12-
"github.com/docker/mcp-gateway/pkg/docker"
13-
"github.com/docker/mcp-gateway/pkg/migrate"
1412
"github.com/docker/mcp-gateway/pkg/oci"
1513
"github.com/docker/mcp-gateway/pkg/workingset"
1614
)
1715

18-
func catalogNextCommand(docker docker.Client) *cobra.Command {
16+
func catalogNextCommand() *cobra.Command {
1917
cmd := &cobra.Command{
2018
Use: "catalog-next",
2119
Short: "Manage catalogs (next generation)",
22-
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
23-
dao, err := db.New()
24-
if err != nil {
25-
return err
26-
}
27-
defer dao.Close()
28-
migrate.MigrateConfig(cmd.Context(), docker, dao)
29-
return nil
30-
},
3120
}
3221

3322
cmd.AddCommand(createCatalogNextCommand())

cmd/docker-mcp/commands/root.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package commands
33
import (
44
"context"
55
"os"
6+
"slices"
67

78
"github.com/docker/cli/cli-plugins/plugin"
89
"github.com/docker/cli/cli/command"
910
"github.com/spf13/cobra"
1011

1112
"github.com/docker/mcp-gateway/cmd/docker-mcp/version"
13+
"github.com/docker/mcp-gateway/pkg/db"
1214
"github.com/docker/mcp-gateway/pkg/desktop"
1315
"github.com/docker/mcp-gateway/pkg/docker"
16+
"github.com/docker/mcp-gateway/pkg/migrate"
1417
)
1518

1619
// Note: We use a custom help template to make it more brief.
@@ -31,6 +34,8 @@ Examples:
3134

3235
// Root returns the root command for the init plugin
3336
func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command {
37+
dockerClient := docker.NewClient(dockerCli)
38+
3439
cmd := &cobra.Command{
3540
Use: "mcp [OPTIONS]",
3641
Short: "Manage MCP servers and clients",
@@ -46,6 +51,17 @@ func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command
4651
}
4752

4853
if os.Getenv("DOCKER_MCP_IN_CONTAINER") != "1" {
54+
if isWorkingSetsFeatureEnabled(dockerCli) {
55+
if isSubcommandOf(cmd, []string{"catalog-next", "catalog", "profile"}) {
56+
dao, err := db.New()
57+
if err != nil {
58+
return err
59+
}
60+
defer dao.Close()
61+
migrate.MigrateConfig(cmd.Context(), dockerClient, dao)
62+
}
63+
}
64+
4965
runningInDockerCE, err := docker.RunningInDockerCE(ctx, dockerCli)
5066
if err != nil {
5167
return err
@@ -68,11 +84,9 @@ func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command
6884
return []string{"--help"}, cobra.ShellCompDirectiveNoFileComp
6985
})
7086

71-
dockerClient := docker.NewClient(dockerCli)
72-
7387
if isWorkingSetsFeatureEnabled(dockerCli) {
74-
cmd.AddCommand(workingSetCommand(dockerClient))
75-
cmd.AddCommand(catalogNextCommand(dockerClient))
88+
cmd.AddCommand(workingSetCommand())
89+
cmd.AddCommand(catalogNextCommand())
7690
}
7791
cmd.AddCommand(catalogCommand(dockerCli))
7892
cmd.AddCommand(clientCommand(dockerCli, cwd))
@@ -101,3 +115,15 @@ func unhideHiddenCommands(cmd *cobra.Command) {
101115
unhideHiddenCommands(c)
102116
}
103117
}
118+
119+
func isSubcommandOf(cmd *cobra.Command, names []string) bool {
120+
if cmd == nil {
121+
return false
122+
}
123+
124+
if slices.Contains(names, cmd.Name()) {
125+
return true
126+
}
127+
128+
return isSubcommandOf(cmd.Parent(), names)
129+
}

cmd/docker-mcp/commands/workingset.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,18 @@ import (
99

1010
"github.com/docker/mcp-gateway/pkg/client"
1111
"github.com/docker/mcp-gateway/pkg/db"
12-
"github.com/docker/mcp-gateway/pkg/docker"
13-
"github.com/docker/mcp-gateway/pkg/migrate"
1412
"github.com/docker/mcp-gateway/pkg/oci"
1513
"github.com/docker/mcp-gateway/pkg/registryapi"
1614
"github.com/docker/mcp-gateway/pkg/sliceutil"
1715
"github.com/docker/mcp-gateway/pkg/workingset"
1816
)
1917

20-
func workingSetCommand(docker docker.Client) *cobra.Command {
18+
func workingSetCommand() *cobra.Command {
2119
cfg := client.ReadConfig()
2220

2321
cmd := &cobra.Command{
2422
Use: "profile",
2523
Short: "Manage profiles",
26-
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
27-
dao, err := db.New()
28-
if err != nil {
29-
return err
30-
}
31-
defer dao.Close()
32-
migrate.MigrateConfig(cmd.Context(), docker, dao)
33-
return nil
34-
},
3524
}
3625

3726
cmd.AddCommand(exportWorkingSetCommand())

0 commit comments

Comments
 (0)