Skip to content

Commit 458522a

Browse files
committed
Extract command for clients
Signed-off-by: David Gageot <[email protected]>
1 parent f2f17ca commit 458522a

File tree

13 files changed

+198
-220
lines changed

13 files changed

+198
-220
lines changed

cmd/docker-mcp/client/config.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ package client
33
import (
44
_ "embed"
55
"errors"
6-
"fmt"
76
"maps"
87
"os"
98
"path/filepath"
109
"runtime"
1110
"slices"
12-
"strings"
1311

14-
"github.com/spf13/cobra"
15-
"github.com/spf13/pflag"
1612
"gopkg.in/yaml.v3"
1713
)
1814

@@ -24,25 +20,12 @@ var (
2420
errNotInGitRepo = errors.New("not in a git repo")
2521
)
2622

27-
func NewClientCmd(cwd string) *cobra.Command {
28-
cfg := readConfig()
29-
cmd := &cobra.Command{
30-
Use: fmt.Sprintf("client (Supported: %s)", strings.Join(getSupportedMCPClients(*cfg), ", ")),
31-
Short: "Manage MCP clients",
32-
}
33-
cmd.AddCommand(ListCommand(cwd, *cfg))
34-
cmd.AddCommand(ConnectCommand(cwd, *cfg))
35-
cmd.AddCommand(DisconnectCommand(cwd, *cfg))
36-
cmd.AddCommand(ManualCommand())
37-
return cmd
38-
}
39-
4023
type Config struct {
4124
System map[string]globalCfg `yaml:"system"`
4225
Project map[string]localCfg `yaml:"project"`
4326
}
4427

45-
func readConfig() *Config {
28+
func ReadConfig() *Config {
4629
var result Config
4730
// We know it parses since it's embedded and covered by tests.
4831
if err := yaml.Unmarshal([]byte(configYaml), &result); err != nil {
@@ -51,14 +34,6 @@ func readConfig() *Config {
5134
return &result
5235
}
5336

54-
func addGlobalFlag(flags *pflag.FlagSet, p *bool) {
55-
flags.BoolVarP(p, "global", "g", false, "Change the system wide configuration or the clients setup in your current git repo.")
56-
}
57-
58-
func addQuietFlag(flags *pflag.FlagSet, p *bool) {
59-
flags.BoolVarP(p, "quiet", "q", false, "Only display errors.")
60-
}
61-
6237
func findGitProjectRoot(dir string) string {
6338
for {
6439
gitPath := filepath.Join(dir, ".git")
@@ -74,7 +49,7 @@ func findGitProjectRoot(dir string) string {
7449
return ""
7550
}
7651

77-
func getSupportedMCPClients(cfg Config) []string {
52+
func GetSupportedMCPClients(cfg Config) []string {
7853
tmp := map[string]struct{}{
7954
vendorGordon: {},
8055
}

cmd/docker-mcp/client/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
var testData embed.FS
1414

1515
func Test_yq_list(t *testing.T) {
16-
config := readConfig()
16+
config := ReadConfig()
1717
tests := []struct {
1818
name string
1919
cfg any
@@ -121,7 +121,7 @@ func readTestData(t *testing.T, path string) []byte {
121121
}
122122

123123
func Test_yq_add_del(t *testing.T) {
124-
config := readConfig()
124+
config := ReadConfig()
125125
tests := []struct {
126126
name string
127127
cfg any

cmd/docker-mcp/client/connect.go

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,26 @@ package client
33
import (
44
"context"
55
"fmt"
6-
"strings"
7-
8-
"github.com/spf13/cobra"
96
)
107

11-
type connectOpts struct {
12-
Global bool
13-
Quiet bool
14-
}
15-
16-
func ConnectCommand(cwd string, cfg Config) *cobra.Command {
17-
opts := &connectOpts{}
18-
cmd := &cobra.Command{
19-
Use: fmt.Sprintf("connect [OPTIONS] <mcp-client>\n\nSupported clients: %s", strings.Join(getSupportedMCPClients(cfg), " ")),
20-
Short: "Connect the Docker MCP Toolkit to a client",
21-
Args: cobra.ExactArgs(1),
22-
RunE: func(cmd *cobra.Command, args []string) error {
23-
return runConnect(cmd.Context(), cwd, cfg, args[0], *opts)
24-
},
25-
}
26-
flags := cmd.Flags()
27-
addGlobalFlag(flags, &opts.Global)
28-
addQuietFlag(flags, &opts.Quiet)
29-
return cmd
30-
}
31-
32-
func runConnect(ctx context.Context, cwd string, config Config, vendor string, opts connectOpts) error {
33-
if vendor == vendorGordon && opts.Global {
8+
func Connect(ctx context.Context, cwd string, config Config, vendor string, global, quiet bool) error {
9+
if vendor == vendorGordon && global {
3410
if err := connectGordon(ctx); err != nil {
3511
return err
3612
}
3713
} else {
38-
updater, err := GetUpdater(vendor, opts.Global, cwd, config)
14+
updater, err := GetUpdater(vendor, global, cwd, config)
3915
if err != nil {
4016
return err
4117
}
4218
if err := updater(DockerMCPCatalog, newMCPGatewayServer()); err != nil {
4319
return err
4420
}
4521
}
46-
if opts.Quiet {
22+
if quiet {
4723
return nil
4824
}
49-
if err := runList(ctx, cwd, config, listOptions{Global: opts.Global}); err != nil {
25+
if err := List(ctx, cwd, config, global, false); err != nil {
5026
return err
5127
}
5228
fmt.Printf("You might have to restart '%s'.\n", vendor)

cmd/docker-mcp/client/disconnect.go

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,26 @@ package client
33
import (
44
"context"
55
"fmt"
6-
"strings"
7-
8-
"github.com/spf13/cobra"
96
)
107

11-
type disconnectOpts struct {
12-
Global bool
13-
Quiet bool
14-
}
15-
16-
func DisconnectCommand(cwd string, cfg Config) *cobra.Command {
17-
opts := &disconnectOpts{}
18-
cmd := &cobra.Command{
19-
Use: fmt.Sprintf("disconnect [OPTIONS] <mcp-client>\n\nSupported clients: %s", strings.Join(getSupportedMCPClients(cfg), " ")),
20-
Short: "Disconnect the Docker MCP Toolkit from a client",
21-
Args: cobra.ExactArgs(1),
22-
RunE: func(cmd *cobra.Command, args []string) error {
23-
return runDisconnect(cmd.Context(), cwd, cfg, args[0], *opts)
24-
},
25-
}
26-
flags := cmd.Flags()
27-
addGlobalFlag(flags, &opts.Global)
28-
addQuietFlag(flags, &opts.Quiet)
29-
return cmd
30-
}
31-
32-
func runDisconnect(ctx context.Context, cwd string, config Config, vendor string, opts disconnectOpts) error {
33-
if vendor == vendorGordon && opts.Global {
8+
func Disconnect(ctx context.Context, cwd string, config Config, vendor string, global, quiet bool) error {
9+
if vendor == vendorGordon && global {
3410
if err := disconnectGordon(ctx); err != nil {
3511
return err
3612
}
3713
} else {
38-
updater, err := GetUpdater(vendor, opts.Global, cwd, config)
14+
updater, err := GetUpdater(vendor, global, cwd, config)
3915
if err != nil {
4016
return err
4117
}
4218
if err := updater(DockerMCPCatalog, nil); err != nil {
4319
return err
4420
}
4521
}
46-
if opts.Quiet {
22+
if quiet {
4723
return nil
4824
}
49-
if err := runList(ctx, cwd, config, listOptions{Global: opts.Global}); err != nil {
25+
if err := List(ctx, cwd, config, global, false); err != nil {
5026
return err
5127
}
5228
fmt.Printf("You might have to restart '%s'.\n", vendor)

cmd/docker-mcp/client/ls.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"fmt"
77
"sort"
88

9-
"github.com/spf13/cobra"
10-
119
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/desktop"
1210
)
1311

@@ -32,30 +30,9 @@ var (
3230
orangeCircle = fmt.Sprintf("%s\u25CF%s", orangeYellowColor, resetColor)
3331
)
3432

35-
type listOptions struct {
36-
Global bool
37-
JSON bool
38-
}
39-
40-
func ListCommand(cwd string, cfg Config) *cobra.Command {
41-
opts := &listOptions{}
42-
cmd := &cobra.Command{
43-
Use: "ls",
44-
Short: "List client configurations",
45-
Args: cobra.NoArgs,
46-
RunE: func(cmd *cobra.Command, _ []string) error {
47-
return runList(cmd.Context(), cwd, cfg, *opts)
48-
},
49-
}
50-
flags := cmd.Flags()
51-
addGlobalFlag(flags, &opts.Global)
52-
flags.BoolVar(&opts.JSON, "json", false, "Print as JSON.")
53-
return cmd
54-
}
55-
56-
func runList(ctx context.Context, cwd string, config Config, opts listOptions) error {
33+
func List(ctx context.Context, cwd string, config Config, global, outputJSON bool) error {
5734
var result Configs
58-
if opts.Global {
35+
if global {
5936
result = parseGlobalConfigs(ctx, config)
6037
} else {
6138
projectRoot := findGitProjectRoot(cwd)
@@ -64,7 +41,7 @@ func runList(ctx context.Context, cwd string, config Config, opts listOptions) e
6441
}
6542
result = parseLocalProjectConfigs(projectRoot, config)
6643
}
67-
if opts.JSON {
44+
if outputJSON {
6845
jsonData, err := json.MarshalIndent(result.GetData(), "", " ")
6946
if err != nil {
7047
return err

cmd/docker-mcp/client/manual.go

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

0 commit comments

Comments
 (0)