@@ -12,6 +12,9 @@ import (
12
12
"time"
13
13
14
14
"github.com/gitpod-io/leeway/pkg/leeway"
15
+ "github.com/gitpod-io/leeway/pkg/leeway/cache"
16
+ "github.com/gitpod-io/leeway/pkg/leeway/cache/local"
17
+ "github.com/gitpod-io/leeway/pkg/leeway/cache/remote"
15
18
"github.com/gookit/color"
16
19
log "github.com/sirupsen/logrus"
17
20
"github.com/spf13/cobra"
@@ -84,7 +87,7 @@ var buildCmd = &cobra.Command{
84
87
},
85
88
}
86
89
87
- func serveBuildResult (ctx context.Context , addr string , localCache * leeway. FilesystemCache , pkg * leeway.Package ) {
90
+ func serveBuildResult (ctx context.Context , addr string , localCache cache. LocalCache , pkg * leeway.Package ) {
88
91
br , exists := localCache .Location (pkg )
89
92
if ! exists {
90
93
log .Fatal ("build result is not in local cache despite just being built. Something's wrong with the cache." )
@@ -121,7 +124,7 @@ func serveBuildResult(ctx context.Context, addr string, localCache *leeway.Files
121
124
}
122
125
}
123
126
124
- func saveBuildResult (ctx context.Context , loc string , localCache * leeway. FilesystemCache , pkg * leeway.Package ) {
127
+ func saveBuildResult (ctx context.Context , loc string , localCache cache. LocalCache , pkg * leeway.Package ) {
125
128
br , exists := localCache .Location (pkg )
126
129
if ! exists {
127
130
log .Fatal ("build result is not in local cache despite just being built. Something's wrong with the cache." )
@@ -178,15 +181,15 @@ func addBuildFlags(cmd *cobra.Command) {
178
181
cmd .Flags ().Bool ("report-github" , os .Getenv ("GITHUB_OUTPUT" ) != "" , "Report package build success/failure to GitHub Actions using the GITHUB_OUTPUT environment variable" )
179
182
}
180
183
181
- func getBuildOpts (cmd * cobra.Command ) ([]leeway.BuildOption , * leeway. FilesystemCache ) {
184
+ func getBuildOpts (cmd * cobra.Command ) ([]leeway.BuildOption , cache. LocalCache ) {
182
185
cm , _ := cmd .Flags ().GetString ("cache" )
183
186
log .WithField ("cacheMode" , cm ).Debug ("configuring caches" )
184
187
cacheLevel := leeway .CacheLevel (cm )
185
188
186
189
remoteCache := getRemoteCache ()
187
190
switch cacheLevel {
188
191
case leeway .CacheNone , leeway .CacheLocal :
189
- remoteCache = leeway. NoRemoteCache {}
192
+ remoteCache = remote . NewNoRemoteCache ()
190
193
case leeway .CacheRemotePull :
191
194
remoteCache = & pullOnlyRemoteCache {C : remoteCache }
192
195
case leeway .CacheRemotePush :
@@ -212,7 +215,7 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemC
212
215
}
213
216
}
214
217
log .WithField ("location" , localCacheLoc ).Debug ("set up local cache" )
215
- localCache , err := leeway .NewFilesystemCache (localCacheLoc )
218
+ localCache , err := local .NewFilesystemCache (localCacheLoc )
216
219
if err != nil {
217
220
log .Fatal (err )
218
221
}
@@ -310,33 +313,67 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemC
310
313
}
311
314
312
315
type pushOnlyRemoteCache struct {
313
- C leeway .RemoteCache
316
+ C cache .RemoteCache
314
317
}
315
318
316
- func (c * pushOnlyRemoteCache ) ExistingPackages (pkgs []* leeway .Package ) (map [* leeway .Package ]struct {}, error ) {
317
- return c .C .ExistingPackages (pkgs )
319
+ func (c * pushOnlyRemoteCache ) ExistingPackages (ctx context. Context , pkgs []cache .Package ) (map [cache .Package ]struct {}, error ) {
320
+ return c .C .ExistingPackages (ctx , pkgs )
318
321
}
319
322
320
- func (c * pushOnlyRemoteCache ) Download (dst leeway. Cache , pkgs []* leeway .Package ) error {
323
+ func (c * pushOnlyRemoteCache ) Download (ctx context. Context , dst cache. LocalCache , pkgs []cache .Package ) error {
321
324
return nil
322
325
}
323
326
324
- func (c * pushOnlyRemoteCache ) Upload (src leeway. Cache , pkgs []* leeway .Package ) error {
325
- return c .C .Upload (src , pkgs )
327
+ func (c * pushOnlyRemoteCache ) Upload (ctx context. Context , src cache. LocalCache , pkgs []cache .Package ) error {
328
+ return c .C .Upload (ctx , src , pkgs )
326
329
}
327
330
328
331
type pullOnlyRemoteCache struct {
329
- C leeway .RemoteCache
332
+ C cache .RemoteCache
330
333
}
331
334
332
- func (c * pullOnlyRemoteCache ) ExistingPackages (pkgs []* leeway .Package ) (map [* leeway .Package ]struct {}, error ) {
333
- return c .C .ExistingPackages (pkgs )
335
+ func (c * pullOnlyRemoteCache ) ExistingPackages (ctx context. Context , pkgs []cache .Package ) (map [cache .Package ]struct {}, error ) {
336
+ return c .C .ExistingPackages (ctx , pkgs )
334
337
}
335
338
336
- func (c * pullOnlyRemoteCache ) Download (dst leeway. Cache , pkgs []* leeway .Package ) error {
337
- return c .C .Download (dst , pkgs )
339
+ func (c * pullOnlyRemoteCache ) Download (ctx context. Context , dst cache. LocalCache , pkgs []cache .Package ) error {
340
+ return c .C .Download (ctx , dst , pkgs )
338
341
}
339
342
340
- func (c * pullOnlyRemoteCache ) Upload (src leeway. Cache , pkgs []* leeway .Package ) error {
343
+ func (c * pullOnlyRemoteCache ) Upload (ctx context. Context , src cache. LocalCache , pkgs []cache .Package ) error {
341
344
return nil
342
345
}
346
+
347
+ func getRemoteCache () cache.RemoteCache {
348
+ remoteCacheBucket := os .Getenv (EnvvarRemoteCacheBucket )
349
+ remoteStorage := os .Getenv (EnvvarRemoteCacheStorage )
350
+ if remoteCacheBucket != "" {
351
+ switch remoteStorage {
352
+ case "GCP" :
353
+ return remote .NewGSUtilCache (
354
+ & cache.RemoteConfig {
355
+ BucketName : remoteCacheBucket ,
356
+ },
357
+ )
358
+ case "AWS" :
359
+ rc , err := remote .NewS3Cache (
360
+ & cache.RemoteConfig {
361
+ BucketName : remoteCacheBucket ,
362
+ },
363
+ )
364
+ if err != nil {
365
+ log .Fatalf ("cannot access remote S3 cache: %v" , err )
366
+ }
367
+
368
+ return rc
369
+ default :
370
+ return remote .NewGSUtilCache (
371
+ & cache.RemoteConfig {
372
+ BucketName : remoteCacheBucket ,
373
+ },
374
+ )
375
+ }
376
+ }
377
+
378
+ return remote .NewNoRemoteCache ()
379
+ }
0 commit comments