From cdc8a4f267410e4035ef11de47e3cb9c403b5480 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Thu, 28 Mar 2024 14:31:33 -0700 Subject: [PATCH 1/2] [cache] Rename cache copy, add single package functionality --- internal/boxcli/cache.go | 31 ++++++++++++++++++++++++------- internal/devbox/cache.go | 22 +++++++++++++++++++++- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/internal/boxcli/cache.go b/internal/boxcli/cache.go index 1b16c19ab33..2f9fc632591 100644 --- a/internal/boxcli/cache.go +++ b/internal/boxcli/cache.go @@ -4,6 +4,7 @@ package boxcli import ( + "github.com/MakeNowJust/heredoc/v2" "github.com/pkg/errors" "github.com/spf13/cobra" "go.jetpack.io/devbox/internal/devbox" @@ -12,6 +13,7 @@ import ( type cacheFlags struct { pathFlag + to string } func cacheCmd() *cobra.Command { @@ -22,11 +24,24 @@ func cacheCmd() *cobra.Command { PersistentPreRunE: ensureNixInstalled, } - copyCommand := &cobra.Command{ - Use: "copy ", - Short: "Copies all nix packages in current project to the cache at ", - Args: cobra.ExactArgs(1), + uploadCommand := &cobra.Command{ + Use: "upload [installable]", + Aliases: []string{"copy"}, // This mimics the nix command + Short: "upload specified or nix packages in current project to cache", + Long: heredoc.Doc(` + Upload specified nix installable or packages in current project to cache. + If [installable] is provided, only that installable will be uploaded. + Otherwise, all packages in the project will be uploaded. + To upload to specific cache, use --to flag. Otherwise, a cache from + the cache provider will be used, if available. + `), + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return devbox.UploadInstallableToCache( + cmd.Context(), cmd.ErrOrStderr(), flags.to, args[0], + ) + } box, err := devbox.Open(&devopt.Opts{ Dir: flags.path, Stderr: cmd.ErrOrStderr(), @@ -34,13 +49,15 @@ func cacheCmd() *cobra.Command { if err != nil { return errors.WithStack(err) } - return box.CacheCopy(cmd.Context(), args[0]) + return box.UploadProjectToCache(cmd.Context(), flags.to) }, } - flags.pathFlag.register(copyCommand) + flags.pathFlag.register(uploadCommand) + uploadCommand.Flags().StringVar( + &flags.to, "to", "", "URI of the cache to copy to") - cacheCommand.AddCommand(copyCommand) + cacheCommand.AddCommand(uploadCommand) cacheCommand.Hidden = true return cacheCommand diff --git a/internal/devbox/cache.go b/internal/devbox/cache.go index 4c4d8d474a5..b49c607084b 100644 --- a/internal/devbox/cache.go +++ b/internal/devbox/cache.go @@ -2,12 +2,16 @@ package devbox import ( "context" + "io" "go.jetpack.io/devbox/internal/devbox/providers/nixcache" "go.jetpack.io/devbox/internal/nix" ) -func (d *Devbox) CacheCopy(ctx context.Context, cacheURI string) error { +func (d *Devbox) UploadProjectToCache( + ctx context.Context, + cacheURI string, +) error { var err error cacheConfig := nixcache.NixCacheConfig{URI: cacheURI} if cacheConfig.URI == "" { @@ -23,3 +27,19 @@ func (d *Devbox) CacheCopy(ctx context.Context, cacheURI string) error { return nix.CopyInstallableToCache(ctx, d.stderr, cacheConfig.URI, profilePath) } + +func UploadInstallableToCache( + ctx context.Context, + stderr io.Writer, + cacheURI, installable string, +) error { + var err error + cacheConfig := nixcache.NixCacheConfig{URI: cacheURI} + if cacheConfig.URI == "" { + cacheConfig, err = nixcache.Get().Config(ctx) + if err != nil { + return err + } + } + return nix.CopyInstallableToCache(ctx, stderr, cacheConfig.URI, installable) +} From 1b54264399b94996d72a99b435b2b178daa8240e Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 29 Mar 2024 09:27:12 -0700 Subject: [PATCH 2/2] Improve description --- internal/boxcli/cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/boxcli/cache.go b/internal/boxcli/cache.go index 2f9fc632591..46d5aaa76ea 100644 --- a/internal/boxcli/cache.go +++ b/internal/boxcli/cache.go @@ -29,7 +29,7 @@ func cacheCmd() *cobra.Command { Aliases: []string{"copy"}, // This mimics the nix command Short: "upload specified or nix packages in current project to cache", Long: heredoc.Doc(` - Upload specified nix installable or packages in current project to cache. + Upload specified nix installable or nix packages in current project to cache. If [installable] is provided, only that installable will be uploaded. Otherwise, all packages in the project will be uploaded. To upload to specific cache, use --to flag. Otherwise, a cache from