Skip to content

Commit 7f5fd83

Browse files
committed
wip
1 parent 30f68e3 commit 7f5fd83

File tree

6 files changed

+129
-38
lines changed

6 files changed

+129
-38
lines changed

cmd/docker-mcp/commands/gateway.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,10 @@ func gatewayCommand(docker docker.Client, dockerCli command.Cli) *cobra.Command
131131
return fmt.Errorf("cannot use --working-set with --enable-all-servers flag")
132132
}
133133

134-
// Read working-set config
135-
wsCfg, err := workingset.ReadConfig()
134+
// Read working-set file
135+
ws, err := workingset.ReadWorkingSetFile(workingSetName)
136136
if err != nil {
137-
return fmt.Errorf("failed to read working-sets config: %w", err)
138-
}
139-
140-
// Get the working-set
141-
ws, exists := wsCfg.WorkingSets[workingSetName]
142-
if !exists {
143-
return fmt.Errorf("working-set %q not found", workingSetName)
137+
return fmt.Errorf("failed to read working-set %q: %w", workingSetName, err)
144138
}
145139

146140
// Set server names from the working-set

cmd/docker-mcp/working-set/config.go

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ package workingset
22

33
import (
44
"encoding/json"
5+
"os"
6+
"path/filepath"
57

68
"github.com/docker/mcp-gateway/pkg/config"
79
)
810

911
type Config struct {
10-
WorkingSets map[string]WorkingSet `json:"workingSets"`
12+
WorkingSets map[string]WorkingSetMetadata `json:"workingSets"`
13+
}
14+
15+
type WorkingSetMetadata struct {
16+
DisplayName string `json:"displayName"`
1117
}
1218

1319
type WorkingSet struct {
@@ -17,7 +23,7 @@ type WorkingSet struct {
1723
}
1824

1925
func ReadConfig() (*Config, error) {
20-
buf, err := config.ReadWorkingSets()
26+
buf, err := config.ReadWorkingSetConfig()
2127
if err != nil {
2228
return nil, err
2329
}
@@ -30,21 +36,73 @@ func ReadConfig() (*Config, error) {
3036
}
3137

3238
if result.WorkingSets == nil {
33-
result.WorkingSets = map[string]WorkingSet{}
39+
result.WorkingSets = map[string]WorkingSetMetadata{}
3440
}
3541

3642
return &result, nil
3743
}
3844

3945
func WriteConfig(cfg *Config) error {
4046
if cfg.WorkingSets == nil {
41-
cfg.WorkingSets = map[string]WorkingSet{}
47+
cfg.WorkingSets = map[string]WorkingSetMetadata{}
4248
}
4349

4450
data, err := json.MarshalIndent(cfg, "", " ")
4551
if err != nil {
4652
return err
4753
}
4854

49-
return config.WriteWorkingSets(data)
55+
return config.WriteWorkingSetConfig(data)
56+
}
57+
58+
func ReadWorkingSetFile(name string) (*WorkingSet, error) {
59+
buf, err := config.ReadWorkingSetFile(name)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
if len(buf) == 0 {
65+
return nil, os.ErrNotExist
66+
}
67+
68+
var ws WorkingSet
69+
if err := json.Unmarshal(buf, &ws); err != nil {
70+
return nil, err
71+
}
72+
73+
return &ws, nil
74+
}
75+
76+
func WriteWorkingSetFile(name string, ws *WorkingSet) error {
77+
data, err := json.MarshalIndent(ws, "", " ")
78+
if err != nil {
79+
return err
80+
}
81+
82+
return config.WriteWorkingSetFile(name, data)
83+
}
84+
85+
func ListWorkingSets() (map[string]WorkingSet, error) {
86+
cfg, err := ReadConfig()
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
workingSets := make(map[string]WorkingSet)
92+
for name := range cfg.WorkingSets {
93+
ws, err := ReadWorkingSetFile(name)
94+
if err != nil {
95+
if os.IsNotExist(err) {
96+
continue // Skip missing files
97+
}
98+
return nil, err
99+
}
100+
workingSets[name] = *ws
101+
}
102+
103+
return workingSets, nil
104+
}
105+
106+
func WorkingSetFilePath(name string) (string, error) {
107+
return config.FilePath(filepath.Join("working-sets", name+".json"))
50108
}

cmd/docker-mcp/working-set/create.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,27 @@ func Create(name, description string, servers []string) error {
2525
return fmt.Errorf("working-set %q already exists", name)
2626
}
2727

28-
// Create new working-set
29-
cfg.WorkingSets[name] = WorkingSet{
28+
// Create working-set object
29+
ws := &WorkingSet{
3030
Name: name,
3131
Description: description,
3232
Servers: servers,
3333
}
3434

35+
// Write working-set file
36+
if err := WriteWorkingSetFile(name, ws); err != nil {
37+
return fmt.Errorf("failed to write working-set file: %w", err)
38+
}
39+
40+
// Update config with metadata
41+
displayName := name
42+
if description != "" {
43+
displayName = description
44+
}
45+
cfg.WorkingSets[name] = WorkingSetMetadata{
46+
DisplayName: displayName,
47+
}
48+
3549
// Write config
3650
if err := WriteConfig(cfg); err != nil {
3751
return fmt.Errorf("failed to write working-sets config: %w", err)

cmd/docker-mcp/working-set/list.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,51 +46,51 @@ func SupportedFormats() string {
4646
return strings.Join(quoted, ", ")
4747
}
4848

49-
// WorkingSetMetadata contains summary info about a working-set (excluding full server list)
50-
type WorkingSetMetadata struct {
49+
// WorkingSetListMetadata contains summary info about a working-set (excluding full server list)
50+
type WorkingSetListMetadata struct {
5151
Description string `json:"description,omitempty" yaml:"description,omitempty"`
5252
ServerCount int `json:"serverCount" yaml:"serverCount"`
5353
}
5454

5555
type ListOutput struct {
56-
WorkingSets map[string]WorkingSetMetadata `json:"workingSets" yaml:"workingSets"`
56+
WorkingSets map[string]WorkingSetListMetadata `json:"workingSets" yaml:"workingSets"`
5757
}
5858

5959
func List(format Format) error {
60-
cfg, err := ReadConfig()
60+
workingSets, err := ListWorkingSets()
6161
if err != nil {
62-
return fmt.Errorf("failed to read working-sets config: %w", err)
62+
return fmt.Errorf("failed to read working-sets: %w", err)
6363
}
6464

6565
switch format {
6666
case JSON:
67-
output := buildListOutput(cfg.WorkingSets)
67+
output := buildListOutput(workingSets)
6868
data, err := json.MarshalIndent(output, "", " ")
6969
if err != nil {
7070
return fmt.Errorf("failed to marshal to JSON: %w", err)
7171
}
7272
fmt.Println(string(data))
7373
case YAML:
74-
output := buildListOutput(cfg.WorkingSets)
74+
output := buildListOutput(workingSets)
7575
data, err := yaml.Marshal(output)
7676
if err != nil {
7777
return fmt.Errorf("failed to marshal to YAML: %w", err)
7878
}
7979
fmt.Print(string(data))
8080
default:
81-
humanPrintWorkingSetsList(cfg.WorkingSets)
81+
humanPrintWorkingSetsList(workingSets)
8282
}
8383

8484
return nil
8585
}
8686

8787
func buildListOutput(workingSets map[string]WorkingSet) ListOutput {
8888
output := ListOutput{
89-
WorkingSets: make(map[string]WorkingSetMetadata),
89+
WorkingSets: make(map[string]WorkingSetListMetadata),
9090
}
9191

9292
for name, ws := range workingSets {
93-
output.WorkingSets[name] = WorkingSetMetadata{
93+
output.WorkingSets[name] = WorkingSetListMetadata{
9494
Description: ws.Description,
9595
ServerCount: len(ws.Servers),
9696
}

cmd/docker-mcp/working-set/show.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ package workingset
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67

78
"gopkg.in/yaml.v3"
89
)
910

1011
func Show(name string, format Format) error {
11-
cfg, err := ReadConfig()
12+
ws, err := ReadWorkingSetFile(name)
1213
if err != nil {
13-
return fmt.Errorf("failed to read working-sets config: %w", err)
14-
}
15-
16-
ws, exists := cfg.WorkingSets[name]
17-
if !exists {
18-
return fmt.Errorf("working-set %q not found", name)
14+
if os.IsNotExist(err) {
15+
return fmt.Errorf("working-set %q not found", name)
16+
}
17+
return fmt.Errorf("failed to read working-set: %w", err)
1918
}
2019

2120
switch format {
@@ -32,7 +31,7 @@ func Show(name string, format Format) error {
3231
}
3332
fmt.Print(string(data))
3433
default:
35-
humanPrintWorkingSet(name, ws)
34+
humanPrintWorkingSet(name, *ws)
3635
}
3736

3837
return nil

pkg/config/readwrite.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,17 @@ func ReadCatalog() ([]byte, error) {
3232
return readFileOrEmpty(path)
3333
}
3434

35-
func ReadWorkingSets() ([]byte, error) {
36-
path, err := FilePath("working-sets.json")
35+
func ReadWorkingSetConfig() ([]byte, error) {
36+
path, err := FilePath("working-set-config.json")
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return readFileOrEmpty(path)
42+
}
43+
44+
func ReadWorkingSetFile(name string) ([]byte, error) {
45+
path, err := FilePath(workingSetFilename(name))
3746
if err != nil {
3847
return nil, err
3948
}
@@ -66,8 +75,12 @@ func WriteCatalog(content []byte) error {
6675
return writeConfigFile("catalog.json", content)
6776
}
6877

69-
func WriteWorkingSets(content []byte) error {
70-
return writeConfigFile("working-sets.json", content)
78+
func WriteWorkingSetConfig(content []byte) error {
79+
return writeConfigFile("working-set-config.json", content)
80+
}
81+
82+
func WriteWorkingSetFile(name string, content []byte) error {
83+
return writeConfigFile(workingSetFilename(name), content)
7184
}
7285

7386
func WriteCatalogFile(name string, content []byte) error {
@@ -83,6 +96,15 @@ func RemoveCatalogFile(name string) error {
8396
return os.Remove(path)
8497
}
8598

99+
func RemoveWorkingSetFile(name string) error {
100+
path, err := FilePath(workingSetFilename(name))
101+
if err != nil {
102+
return err
103+
}
104+
105+
return os.Remove(path)
106+
}
107+
86108
func ReadConfigFile(ctx context.Context, docker docker.Client, name string) ([]byte, error) {
87109
path, err := FilePath(name)
88110
if err != nil {
@@ -144,6 +166,10 @@ func catalogFilename(name string) string {
144166
return filepath.Join("catalogs", sanitizeFilename(name)+".yaml")
145167
}
146168

169+
func workingSetFilename(name string) string {
170+
return filepath.Join("working-sets", sanitizeFilename(name)+".json")
171+
}
172+
147173
func readFileOrEmpty(path string) ([]byte, error) {
148174
buf, err := os.ReadFile(path)
149175
if err != nil {

0 commit comments

Comments
 (0)