Repository navigation
Expand file tree
/
Copy pathcache_shared.go
More file actions
95 lines (87 loc) · 3.1 KB
/
Copy pathcache_shared.go
File metadata and controls
95 lines (87 loc) · 3.1 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
package clicommand
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli/v3"
)
// CacheConfig includes cache-related shared options for easy inclusion across
// cache command config structs (via embedding).
type CacheConfig struct {
Names []string `cli:"name"`
Registry string `cli:"registry"`
BucketURL string `cli:"cache-store-url"`
CacheConfigFile string `cli:"cache-config-file"`
Concurrency int `cli:"concurrency"`
FailOnError bool `cli:"cache-fail-on-error"`
}
func cacheFlags() []cli.Flag {
return []cli.Flag{
&cli.StringSliceFlag{
Name: "name",
Value: []string{},
Usage: "Cache name to process (can be specified multiple times; if empty, processes all caches)",
Sources: cli.EnvVars("BUILDKITE_CACHE_NAMES"),
},
&cli.StringFlag{
Name: "registry",
Value: "~",
Usage: "The slug of the cache registry to use; '~' selects the cluster's default registry",
Sources: cli.EnvVars("BUILDKITE_AGENT_CACHE_REGISTRY"),
},
&cli.StringFlag{
Name: "cache-store-url",
Value: "",
Usage: "The URL of the cache store (e.g., s3://bucket-name); uploads/downloads use ambient credentials",
Sources: cli.EnvVars("BUILDKITE_AGENT_CACHE_STORE_URL"),
},
&cli.StringFlag{
Name: "cache-config-file",
Value: "",
Usage: "Path to the cache configuration YAML file (defaults to .buildkite/cache.yml or .buildkite/cache.yaml)",
Sources: cli.EnvVars("BUILDKITE_CACHE_CONFIG_FILE"),
},
&cli.IntFlag{
Name: "concurrency",
Value: 2,
Usage: "Number of concurrent cache operations",
Sources: cli.EnvVars("BUILDKITE_CACHE_CONCURRENCY"),
},
&cli.BoolFlag{
Name: "cache-fail-on-error",
Value: false,
Usage: "Fail the command (non-zero exit) when a cache save or restore fails. By default a cache is best-effort: failures are logged and skipped so they never fail the build",
Sources: cli.EnvVars("BUILDKITE_AGENT_CACHE_FAIL_ON_ERROR"),
},
}
}
// defaultCacheConfigPaths lists the candidate cache configuration files, in
// precedence order, used when --cache-config-file is not provided.
var defaultCacheConfigPaths = []string{
filepath.FromSlash(".buildkite/cache.yml"),
filepath.FromSlash(".buildkite/cache.yaml"),
}
// resolveCacheConfigFile returns the cache configuration path to load. An
// explicitly provided path is used as-is. Otherwise it searches the default
// locations, returning the one that exists and erroring if more than one, or
// none, are present.
func resolveCacheConfigFile(explicit string) (string, error) {
if explicit != "" {
return explicit, nil
}
var exists []string
for _, path := range defaultCacheConfigPaths {
if _, err := os.Stat(path); err == nil {
exists = append(exists, path)
}
}
switch len(exists) {
case 0:
return "", fmt.Errorf("could not find a default cache configuration file; tried %s", strings.Join(defaultCacheConfigPaths, ", "))
case 1:
return exists[0], nil
default:
return "", fmt.Errorf("found multiple cache configuration files: %s; please keep only 1 configuration file present", strings.Join(exists, ", "))
}
}