Skip to content

Commit 04c0aa1

Browse files
committed
fix: as suggested switched to using StringSlice and cache_shared.go
1 parent 527e40c commit 04c0aa1

4 files changed

Lines changed: 77 additions & 75 deletions

File tree

clicommand/cache_shared.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package clicommand
2+
3+
import "github.com/urfave/cli"
4+
5+
// CacheConfig includes cache-related shared options for easy inclusion across
6+
// cache command config structs (via embedding).
7+
type CacheConfig struct {
8+
Ids []string `cli:"ids"`
9+
Registry string `cli:"registry"`
10+
BucketURL string `cli:"bucket-url" validate:"required"`
11+
Branch string `cli:"branch" validate:"required"`
12+
Pipeline string `cli:"pipeline" validate:"required"`
13+
Organization string `cli:"organization" validate:"required"`
14+
CacheConfigFile string `cli:"cache-config-file"`
15+
}
16+
17+
func cacheFlags() []cli.Flag {
18+
return []cli.Flag{
19+
cli.StringSliceFlag{
20+
Name: "ids",
21+
Value: &cli.StringSlice{},
22+
Usage: "Comma-separated list of cache IDs (if empty, processes all caches)",
23+
EnvVar: "BUILDKITE_CACHE_IDS",
24+
},
25+
cli.StringFlag{
26+
Name: "registry",
27+
Value: "~",
28+
Usage: "The slug of the cache registry to use, defaults to the default registry (~)",
29+
EnvVar: "BUILDKITE_CACHE_REGISTRY",
30+
},
31+
cli.StringFlag{
32+
Name: "bucket-url",
33+
Value: "",
34+
Usage: "The URL of the bucket (e.g., s3://bucket-name)",
35+
EnvVar: "BUILDKITE_CACHE_BUCKET_URL",
36+
},
37+
cli.StringFlag{
38+
Name: "branch",
39+
Value: "",
40+
Usage: "Which branch should the cache be associated with",
41+
EnvVar: "BUILDKITE_BRANCH",
42+
},
43+
cli.StringFlag{
44+
Name: "pipeline",
45+
Value: "",
46+
Usage: "The pipeline slug for this cache",
47+
EnvVar: "BUILDKITE_PIPELINE_SLUG",
48+
},
49+
cli.StringFlag{
50+
Name: "organization",
51+
Value: "",
52+
Usage: "The organization slug for this cache",
53+
EnvVar: "BUILDKITE_ORGANIZATION_SLUG",
54+
},
55+
cli.StringFlag{
56+
Name: "cache-config-file",
57+
Value: ".buildkite/cache.yml",
58+
Usage: "Path to the cache configuration YAML file",
59+
EnvVar: "BUILDKITE_CACHE_CONFIG_FILE",
60+
},
61+
}
62+
}

clicommand/global.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,6 @@ type APIConfig struct {
182182
NoHTTP2 bool `cli:"no-http2"`
183183
}
184184

185-
// CacheConfig includes cache-related shared options for easy inclusion across
186-
// cache command config structs (via embedding).
187-
type CacheConfig struct {
188-
Ids string `cli:"ids"`
189-
Registry string `cli:"registry"`
190-
BucketURL string `cli:"bucket-url" validate:"required"`
191-
Branch string `cli:"branch" validate:"required"`
192-
Pipeline string `cli:"pipeline" validate:"required"`
193-
Organization string `cli:"organization" validate:"required"`
194-
CacheConfigFile string `cli:"cache-config-file"`
195-
}
196-
197185
func globalFlags() []cli.Flag {
198186
return []cli.Flag{
199187
NoColorFlag,
@@ -214,53 +202,6 @@ func apiFlags() []cli.Flag {
214202
}
215203
}
216204

217-
func cacheFlags() []cli.Flag {
218-
return []cli.Flag{
219-
cli.StringFlag{
220-
Name: "ids",
221-
Value: "",
222-
Usage: "Comma-separated list of cache IDs (if empty, processes all caches)",
223-
EnvVar: "BUILDKITE_CACHE_IDS",
224-
},
225-
cli.StringFlag{
226-
Name: "registry",
227-
Value: "~",
228-
Usage: "The slug of the cache registry to use, defaults to the default registry (~)",
229-
EnvVar: "BUILDKITE_CACHE_REGISTRY",
230-
},
231-
cli.StringFlag{
232-
Name: "bucket-url",
233-
Value: "",
234-
Usage: "The URL of the bucket (e.g., s3://bucket-name)",
235-
EnvVar: "BUILDKITE_CACHE_BUCKET_URL",
236-
},
237-
cli.StringFlag{
238-
Name: "branch",
239-
Value: "",
240-
Usage: "Which branch should the cache be associated with",
241-
EnvVar: "BUILDKITE_BRANCH",
242-
},
243-
cli.StringFlag{
244-
Name: "pipeline",
245-
Value: "",
246-
Usage: "The pipeline slug for this cache",
247-
EnvVar: "BUILDKITE_PIPELINE_SLUG",
248-
},
249-
cli.StringFlag{
250-
Name: "organization",
251-
Value: "",
252-
Usage: "The organization slug for this cache",
253-
EnvVar: "BUILDKITE_ORGANIZATION_SLUG",
254-
},
255-
cli.StringFlag{
256-
Name: "cache-config-file",
257-
Value: ".buildkite/cache.yml",
258-
Usage: "Path to the cache configuration YAML file",
259-
EnvVar: "BUILDKITE_CACHE_CONFIG_FILE",
260-
},
261-
}
262-
}
263-
264205
func CreateLogger(cfg any) logger.Logger {
265206
var l logger.Logger
266207
logFormat := "text"

internal/cache/cache.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type Config struct {
2727
Organization string
2828
// CacheConfigFile is the path to the cache configuration YAML file
2929
CacheConfigFile string
30-
// Ids is a comma-separated list of cache IDs (if empty, processes all caches)
31-
Ids string
30+
// Ids is a list of cache IDs (if empty, processes all caches)
31+
Ids []string
3232
// APIEndpoint is the Agent API endpoint
3333
APIEndpoint string
3434
// APIToken is the access token used to authenticate
@@ -128,18 +128,15 @@ func setupCacheClient(ctx context.Context, l logger.Logger, cfg Config) (*zstash
128128
}
129129

130130
// Determine which cache IDs to process
131-
var cacheIDs []string
132-
if cfg.Ids != "" {
133-
cacheIDs = strings.Split(cfg.Ids, ",")
134-
131+
if len(cfg.Ids) > 0 {
135132
// Validate that specified cache IDs exist
136133
validIDs := make(map[string]bool)
137134
for _, cache := range cacheClient.ListCaches() {
138135
validIDs[cache.ID] = true
139136
}
140137

141138
var invalidIDs []string
142-
for _, id := range cacheIDs {
139+
for _, id := range cfg.Ids {
143140
if !validIDs[id] {
144141
invalidIDs = append(invalidIDs, id)
145142
}
@@ -148,14 +145,16 @@ func setupCacheClient(ctx context.Context, l logger.Logger, cfg Config) (*zstash
148145
if len(invalidIDs) > 0 {
149146
return nil, nil, fmt.Errorf("cache IDs not found in configuration: %s", strings.Join(invalidIDs, ", "))
150147
}
151-
} else {
152-
// Process all caches configured in the client
153-
for _, cache := range cacheClient.ListCaches() {
154-
cacheIDs = append(cacheIDs, cache.ID)
155-
}
148+
149+
return cacheClient, cfg.Ids, nil
150+
}
151+
152+
var cacheDs []string
153+
for _, cache := range cacheClient.ListCaches() {
154+
cacheDs = append(cacheDs, cache.ID)
156155
}
157156

158-
return cacheClient, cacheIDs, nil
157+
return cacheClient, cacheDs, nil
159158
}
160159

161160
// restoreWithClient performs the restore operation for the given cache IDs using the provided client

internal/cache/cache_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ func TestSetupCacheClient_InvalidCacheIDs(t *testing.T) {
368368

369369
cfg := Config{
370370
CacheConfigFile: configFile,
371-
Ids: "cache1,invalid1,cache2,invalid2",
371+
Ids: []string{"cache1", "invalid1", "cache2", "invalid2"},
372372
BucketURL: "s3://test-bucket",
373373
Branch: "main",
374374
Pipeline: "test-pipeline",
@@ -402,7 +402,7 @@ func TestSetupCacheClient_ValidCacheIDs(t *testing.T) {
402402

403403
cfg := Config{
404404
CacheConfigFile: configFile,
405-
Ids: "cache1,cache2",
405+
Ids: []string{"cache1", "cache2"},
406406
BucketURL: "s3://test-bucket",
407407
Branch: "main",
408408
Pipeline: "test-pipeline",
@@ -435,7 +435,7 @@ func TestSetupCacheClient_AllCaches(t *testing.T) {
435435

436436
cfg := Config{
437437
CacheConfigFile: configFile,
438-
Ids: "",
438+
Ids: []string{},
439439
BucketURL: "s3://test-bucket",
440440
Branch: "main",
441441
Pipeline: "test-pipeline",

0 commit comments

Comments
 (0)