-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_registry.go
More file actions
198 lines (169 loc) · 5.95 KB
/
remote_registry.go
File metadata and controls
198 lines (169 loc) · 5.95 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package plugin
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"time"
)
// RemoteRegistry discovers and downloads plugins from a remote HTTP registry.
type RemoteRegistry struct {
baseURL string
httpClient *http.Client
mu sync.RWMutex
cache map[string]*PluginManifest
cacheTTL time.Duration
lastFetch time.Time
}
// RemoteRegistryOption configures a RemoteRegistry.
type RemoteRegistryOption func(*RemoteRegistry)
// WithHTTPClient sets the HTTP client used by the remote registry.
func WithHTTPClient(client *http.Client) RemoteRegistryOption {
return func(r *RemoteRegistry) {
r.httpClient = client
}
}
// WithCacheTTL sets how long cached manifests remain valid.
func WithCacheTTL(ttl time.Duration) RemoteRegistryOption {
return func(r *RemoteRegistry) {
r.cacheTTL = ttl
}
}
// NewRemoteRegistry creates a new remote registry client.
func NewRemoteRegistry(baseURL string, opts ...RemoteRegistryOption) *RemoteRegistry {
r := &RemoteRegistry{
baseURL: baseURL,
httpClient: &http.Client{Timeout: 30 * time.Second},
cache: make(map[string]*PluginManifest),
cacheTTL: 5 * time.Minute,
}
for _, opt := range opts {
opt(r)
}
return r
}
// Search queries the remote registry for plugins matching the given query string.
func (r *RemoteRegistry) Search(ctx context.Context, query string) ([]*PluginManifest, error) {
u := fmt.Sprintf("%s/api/v1/plugins?q=%s", r.baseURL, url.QueryEscape(query))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("create search request: %w", err)
}
resp, err := r.httpClient.Do(req) //nolint:gosec // G704: URL from configured registry endpoint
if err != nil {
return nil, fmt.Errorf("search remote registry: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("remote registry search returned %d: %s", resp.StatusCode, string(body))
}
var manifests []*PluginManifest
if err := json.NewDecoder(resp.Body).Decode(&manifests); err != nil {
return nil, fmt.Errorf("decode search results: %w", err)
}
// Update cache
r.mu.Lock()
for _, m := range manifests {
r.cache[m.Name] = m
}
r.lastFetch = time.Now()
r.mu.Unlock()
return manifests, nil
}
// GetManifest retrieves the manifest for a specific plugin version from the remote registry.
func (r *RemoteRegistry) GetManifest(ctx context.Context, name, version string) (*PluginManifest, error) {
// Check cache first
r.mu.RLock()
if cached, ok := r.cache[name]; ok && time.Since(r.lastFetch) < r.cacheTTL {
if cached.Version == version || version == "" {
r.mu.RUnlock()
return cached, nil
}
}
r.mu.RUnlock()
u := fmt.Sprintf("%s/api/v1/plugins/%s/versions/%s", r.baseURL, url.PathEscape(name), url.PathEscape(version))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("create manifest request: %w", err)
}
resp, err := r.httpClient.Do(req) //nolint:gosec // G704: URL from configured registry endpoint
if err != nil {
return nil, fmt.Errorf("fetch manifest from remote: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("plugin %s@%s not found in remote registry", name, version)
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("remote registry returned %d: %s", resp.StatusCode, string(body))
}
var manifest PluginManifest
if err := json.NewDecoder(resp.Body).Decode(&manifest); err != nil {
return nil, fmt.Errorf("decode manifest: %w", err)
}
// Update cache
r.mu.Lock()
r.cache[name] = &manifest
r.lastFetch = time.Now()
r.mu.Unlock()
return &manifest, nil
}
// Download retrieves the plugin archive for a specific version.
func (r *RemoteRegistry) Download(ctx context.Context, name, version string) (io.ReadCloser, error) {
u := fmt.Sprintf("%s/api/v1/plugins/%s/versions/%s/download", r.baseURL, url.PathEscape(name), url.PathEscape(version))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("create download request: %w", err)
}
resp, err := r.httpClient.Do(req) //nolint:gosec // G704: URL from configured registry endpoint
if err != nil {
return nil, fmt.Errorf("download plugin from remote: %w", err)
}
if resp.StatusCode == http.StatusNotFound {
resp.Body.Close()
return nil, fmt.Errorf("plugin %s@%s not found in remote registry", name, version)
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
return nil, fmt.Errorf("remote registry download returned %d: %s", resp.StatusCode, string(body))
}
return resp.Body, nil
}
// ListVersions retrieves available versions for a plugin from the remote registry.
func (r *RemoteRegistry) ListVersions(ctx context.Context, name string) ([]string, error) {
u := fmt.Sprintf("%s/api/v1/plugins/%s/versions", r.baseURL, url.PathEscape(name))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("create versions request: %w", err)
}
resp, err := r.httpClient.Do(req) //nolint:gosec // G704: URL from configured registry endpoint
if err != nil {
return nil, fmt.Errorf("list versions from remote: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("plugin %s not found in remote registry", name)
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("remote registry returned %d: %s", resp.StatusCode, string(body))
}
var versions []string
if err := json.NewDecoder(resp.Body).Decode(&versions); err != nil {
return nil, fmt.Errorf("decode versions: %w", err)
}
return versions, nil
}
// ClearCache clears the in-memory manifest cache.
func (r *RemoteRegistry) ClearCache() {
r.mu.Lock()
r.cache = make(map[string]*PluginManifest)
r.lastFetch = time.Time{}
r.mu.Unlock()
}