-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathreadwrite.go
More file actions
155 lines (124 loc) · 3.18 KB
/
readwrite.go
File metadata and controls
155 lines (124 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package config
import (
"context"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/docker"
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/user"
)
func ReadTools(ctx context.Context, docker docker.Client) ([]byte, error) {
return ReadConfigFile(ctx, docker, "tools.yaml")
}
func ReadConfig(ctx context.Context, docker docker.Client) ([]byte, error) {
return ReadConfigFile(ctx, docker, "config.yaml")
}
func ReadRegistry(ctx context.Context, docker docker.Client) ([]byte, error) {
return ReadConfigFile(ctx, docker, "registry.yaml")
}
func ReadCatalog() ([]byte, error) {
path, err := FilePath("catalog.json")
if err != nil {
return nil, err
}
return readFileOrEmpty(path)
}
func ReadCatalogFile(name string) ([]byte, error) {
path, err := FilePath(catalogFilename(name))
if err != nil {
return nil, err
}
return readFileOrEmpty(path)
}
func WriteTools(content []byte) error {
return writeConfigFile("tools.yaml", content)
}
func WriteConfig(content []byte) error {
return writeConfigFile("config.yaml", content)
}
func WriteRegistry(content []byte) error {
return writeConfigFile("registry.yaml", content)
}
func WriteCatalog(content []byte) error {
return writeConfigFile("catalog.json", content)
}
func WriteCatalogFile(name string, content []byte) error {
return writeConfigFile(catalogFilename(name), content)
}
func RemoveCatalogFile(name string) error {
path, err := FilePath(catalogFilename(name))
if err != nil {
return err
}
return os.Remove(path)
}
func ReadConfigFile(ctx context.Context, docker docker.Client, name string) ([]byte, error) {
path, err := FilePath(name)
if err != nil {
return nil, err
}
buf, err := os.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
// File does not exist, import from legacy docker volume
content, err := readFromDockerVolume(ctx, docker, name)
if err != nil {
return nil, err
}
// Write to a file and forget about the legacy volume
if err := writeConfigFile(name, content); err != nil {
return nil, err
}
return content, nil
}
return buf, nil
}
func writeConfigFile(name string, content []byte) error {
path, err := FilePath(name)
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
return os.WriteFile(path, content, 0o644)
}
func FilePath(name string) (string, error) {
if filepath.IsAbs(name) {
return name, nil
}
if strings.HasPrefix(name, "./") {
return filepath.Abs(name)
}
homeDir, err := user.HomeDir()
if err != nil {
return "", err
}
return filepath.Join(homeDir, ".docker", "mcp", name), nil
}
func catalogFilename(name string) string {
return filepath.Join("catalogs", sanitizeFilename(name)+".yaml")
}
func readFileOrEmpty(path string) ([]byte, error) {
buf, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
return buf, nil
}
func sanitizeFilename(input string) string {
s := strings.TrimSpace(input)
s = strings.ToLower(s)
illegalChars := regexp.MustCompile(`[<>:"/\\|?*\x00]`)
s = illegalChars.ReplaceAllString(s, "_")
if len(s) > 250 {
s = s[:250]
}
return s
}