@@ -12,6 +12,9 @@ import (
1212 "time"
1313
1414 "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"
1518 "github.com/gookit/color"
1619 log "github.com/sirupsen/logrus"
1720 "github.com/spf13/cobra"
@@ -84,7 +87,7 @@ var buildCmd = &cobra.Command{
8487 },
8588}
8689
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 ) {
8891 br , exists := localCache .Location (pkg )
8992 if ! exists {
9093 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
121124 }
122125}
123126
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 ) {
125128 br , exists := localCache .Location (pkg )
126129 if ! exists {
127130 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) {
178181 cmd .Flags ().Bool ("report-github" , os .Getenv ("GITHUB_OUTPUT" ) != "" , "Report package build success/failure to GitHub Actions using the GITHUB_OUTPUT environment variable" )
179182}
180183
181- func getBuildOpts (cmd * cobra.Command ) ([]leeway.BuildOption , * leeway. FilesystemCache ) {
184+ func getBuildOpts (cmd * cobra.Command ) ([]leeway.BuildOption , cache. LocalCache ) {
182185 cm , _ := cmd .Flags ().GetString ("cache" )
183186 log .WithField ("cacheMode" , cm ).Debug ("configuring caches" )
184187 cacheLevel := leeway .CacheLevel (cm )
185188
186189 remoteCache := getRemoteCache ()
187190 switch cacheLevel {
188191 case leeway .CacheNone , leeway .CacheLocal :
189- remoteCache = leeway. NoRemoteCache {}
192+ remoteCache = remote . NewNoRemoteCache ()
190193 case leeway .CacheRemotePull :
191194 remoteCache = & pullOnlyRemoteCache {C : remoteCache }
192195 case leeway .CacheRemotePush :
@@ -212,7 +215,7 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemC
212215 }
213216 }
214217 log .WithField ("location" , localCacheLoc ).Debug ("set up local cache" )
215- localCache , err := leeway .NewFilesystemCache (localCacheLoc )
218+ localCache , err := local .NewFilesystemCache (localCacheLoc )
216219 if err != nil {
217220 log .Fatal (err )
218221 }
@@ -310,33 +313,67 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemC
310313}
311314
312315type pushOnlyRemoteCache struct {
313- C leeway .RemoteCache
316+ C cache .RemoteCache
314317}
315318
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 )
318321}
319322
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 {
321324 return nil
322325}
323326
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 )
326329}
327330
328331type pullOnlyRemoteCache struct {
329- C leeway .RemoteCache
332+ C cache .RemoteCache
330333}
331334
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 )
334337}
335338
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 )
338341}
339342
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 {
341344 return nil
342345}
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