Skip to content

Commit f2f17ca

Browse files
committed
Extract command for policies
Signed-off-by: David Gageot <[email protected]>
1 parent 27be505 commit f2f17ca

File tree

6 files changed

+72
-81
lines changed

6 files changed

+72
-81
lines changed

cmd/docker-mcp/commands/policy.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package commands
2+
3+
import (
4+
"os"
5+
"strings"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/tui"
10+
"github.com/docker/mcp-gateway/cmd/docker-mcp/secret-management/policy"
11+
)
12+
13+
const setPolicyExample = `
14+
# Backup the current policy to a file
15+
docker mcp policy dump > policy.conf
16+
17+
# Set a new policy
18+
docker mcp policy set "my-secret allows postgres"
19+
20+
# Restore the previous policy
21+
cat policy.conf | docker mcp policy set
22+
`
23+
24+
func policyCommand() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Use: "policy",
27+
Aliases: []string{"policies"},
28+
Short: "Manage secret policies",
29+
}
30+
31+
cmd.AddCommand(&cobra.Command{
32+
Use: "set <content>",
33+
Short: "Set a policy for secret management in Docker Desktop",
34+
Args: cobra.MaximumNArgs(1),
35+
RunE: func(cmd *cobra.Command, args []string) error {
36+
if len(args) == 0 {
37+
bytes, err := tui.ReadAllWithContext(cmd.Context(), os.Stdin)
38+
if err != nil {
39+
return err
40+
}
41+
args = append(args, string(bytes))
42+
}
43+
return policy.Set(cmd.Context(), args[0])
44+
},
45+
Example: strings.Trim(setPolicyExample, "\n"),
46+
})
47+
48+
cmd.AddCommand(&cobra.Command{
49+
Use: "dump",
50+
Short: "Dump the policy content",
51+
Args: cobra.NoArgs,
52+
RunE: func(cmd *cobra.Command, _ []string) error {
53+
return policy.Dump(cmd.Context())
54+
},
55+
})
56+
57+
return cmd
58+
}

cmd/docker-mcp/commands/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/desktop"
1414
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/docker"
1515
"github.com/docker/mcp-gateway/cmd/docker-mcp/oauth"
16-
"github.com/docker/mcp-gateway/cmd/docker-mcp/secret-management/policy"
1716
"github.com/docker/mcp-gateway/cmd/docker-mcp/version"
1817
)
1918

@@ -74,7 +73,7 @@ func Root(ctx context.Context, cwd string, dockerCli command.Cli) *cobra.Command
7473
dockerClient := docker.NewClient(dockerCli)
7574

7675
cmd.AddCommand(secretCommand(dockerClient))
77-
cmd.AddCommand(policy.NewPolicyCmd())
76+
cmd.AddCommand(policyCommand())
7877
cmd.AddCommand(oauth.NewOAuthCmd())
7978
cmd.AddCommand(client.NewClientCmd(cwd))
8079
cmd.AddCommand(catalog.NewCatalogCmd())

cmd/docker-mcp/commands/secret.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/docker/mcp-gateway/cmd/docker-mcp/secret-management/secret"
1212
)
1313

14-
const setExample = `
14+
const setSecretExample = `
1515
# Using secrets for postgres password with default policy:
1616
docker mcp secret set POSTGRES_PASSWORD=my-secret-password
1717
docker run -d -l x-secret:POSTGRES_PASSWORD=/pwd.txt -e POSTGRES_PASSWORD_FILE=/pwd.txt -p 5432 postgres
@@ -25,16 +25,16 @@ func secretCommand(docker docker.Client) *cobra.Command {
2525
cmd := &cobra.Command{
2626
Use: "secret",
2727
Short: "Manage secrets",
28-
Example: strings.Trim(setExample, "\n"),
28+
Example: strings.Trim(setSecretExample, "\n"),
2929
}
30-
cmd.AddCommand(rmCommand())
31-
cmd.AddCommand(listCommand())
32-
cmd.AddCommand(setCommand())
33-
cmd.AddCommand(exportCommand(docker))
30+
cmd.AddCommand(rmSecretCommand())
31+
cmd.AddCommand(listSecretCommand())
32+
cmd.AddCommand(setSecretCommand())
33+
cmd.AddCommand(exportSecretCommand(docker))
3434
return cmd
3535
}
3636

37-
func rmCommand() *cobra.Command {
37+
func rmSecretCommand() *cobra.Command {
3838
var opts secret.RmOpts
3939
cmd := &cobra.Command{
4040
Use: "rm name1 name2 ...",
@@ -58,7 +58,7 @@ func validateRmArgs(args []string, opts secret.RmOpts) error {
5858
return nil
5959
}
6060

61-
func listCommand() *cobra.Command {
61+
func listSecretCommand() *cobra.Command {
6262
var opts secret.ListOptions
6363
cmd := &cobra.Command{
6464
Use: "ls",
@@ -73,12 +73,12 @@ func listCommand() *cobra.Command {
7373
return cmd
7474
}
7575

76-
func setCommand() *cobra.Command {
76+
func setSecretCommand() *cobra.Command {
7777
opts := &secret.SetOpts{}
7878
cmd := &cobra.Command{
7979
Use: "set key[=value]",
8080
Short: "Set a secret in Docker Desktop's secret store",
81-
Example: strings.Trim(setExample, "\n"),
81+
Example: strings.Trim(setSecretExample, "\n"),
8282
Args: cobra.ExactArgs(1),
8383
RunE: func(cmd *cobra.Command, args []string) error {
8484
if !secret.IsValidProvider(opts.Provider) {
@@ -110,7 +110,7 @@ func isNotImplicitReadFromStdinSyntax(args []string, opts secret.SetOpts) bool {
110110
return strings.Contains(args[0], "=") || len(args) > 1 || opts.Provider != ""
111111
}
112112

113-
func exportCommand(docker docker.Client) *cobra.Command {
113+
func exportSecretCommand(docker docker.Client) *cobra.Command {
114114
return &cobra.Command{
115115
Use: "export [server1] [server2] ...",
116116
Short: "Export secrets for the specified servers",

cmd/docker-mcp/secret-management/policy/dump.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,15 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/spf13/cobra"
8-
97
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/desktop"
108
)
119

12-
func DumpCommand() *cobra.Command {
13-
return &cobra.Command{
14-
Use: "dump",
15-
Short: "Dump the policy content",
16-
Args: cobra.NoArgs,
17-
RunE: func(cmd *cobra.Command, _ []string) error {
18-
return runDump(cmd.Context())
19-
},
20-
}
21-
}
22-
23-
func runDump(ctx context.Context) error {
10+
func Dump(ctx context.Context) error {
2411
l, err := desktop.NewSecretsClient().GetJfsPolicy(ctx)
2512
if err != nil {
2613
return err
2714
}
2815

2916
fmt.Println(l)
30-
3117
return nil
3218
}

cmd/docker-mcp/secret-management/policy/root.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

cmd/docker-mcp/secret-management/policy/set.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,10 @@ package policy
22

33
import (
44
"context"
5-
"os"
6-
"strings"
7-
8-
"github.com/spf13/cobra"
95

106
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/desktop"
11-
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/tui"
127
)
138

14-
const setExample = `
15-
# Backup the current policy to a file
16-
docker mcp policy dump > policy.conf
17-
18-
# Set a new policy
19-
docker mcp policy set "my-secret allows postgres"
20-
21-
# Restore the previous policy
22-
cat policy.conf | docker mcp policy set
23-
`
24-
25-
func SetCommand() *cobra.Command {
26-
cmd := &cobra.Command{
27-
Use: "set <content>",
28-
Short: "Set a policy for secret management in Docker Desktop",
29-
Args: cobra.MaximumNArgs(1),
30-
RunE: func(cmd *cobra.Command, args []string) error {
31-
if len(args) == 0 {
32-
bytes, err := tui.ReadAllWithContext(cmd.Context(), os.Stdin)
33-
if err != nil {
34-
return err
35-
}
36-
args = append(args, string(bytes))
37-
}
38-
return runSet(cmd.Context(), args[0])
39-
},
40-
Example: strings.Trim(setExample, "\n"),
41-
}
42-
return cmd
43-
}
44-
45-
func runSet(ctx context.Context, data string) error {
9+
func Set(ctx context.Context, data string) error {
4610
return desktop.NewSecretsClient().SetJfsPolicy(ctx, data)
4711
}

0 commit comments

Comments
 (0)