Skip to content

Commit 8bf4dfa

Browse files
authored
[cache] Rename cache copy, add single package functionality (#1943)
## Summary Renames `devbox cache copy` to `devbox cache upload`. Also changes the interface to use `--to` flag for cache URI. This allows easier use if cache is provided by jetpack API. e.g. ``` # upload project packages to jetpack provided cache devbox cache upload # upload installable to jetpack provided cache devbox cache upload <installable> ``` If user wishes to specify cache URI, they can do: ``` # upload project devbox cache upload --to 's3://mike-test-nix-cache?region=us-west-2' # upload single installable devbox cache upload --to 's3://mike-test-nix-cache?region=us-west-2' nixpkgs#php ``` ## How was it tested? ``` devbox cache upload --to 's3://mike-test-nix-cache?region=us-west-2' devbox cache upload --to 's3://mike-test-nix-cache?region=us-west-2' nixpkgs#php ``` Inspected cache on aws.
1 parent 28ca84d commit 8bf4dfa

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

internal/boxcli/cache.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package boxcli
55

66
import (
7+
"github.com/MakeNowJust/heredoc/v2"
78
"github.com/pkg/errors"
89
"github.com/spf13/cobra"
910
"go.jetpack.io/devbox/internal/devbox"
@@ -12,6 +13,7 @@ import (
1213

1314
type cacheFlags struct {
1415
pathFlag
16+
to string
1517
}
1618

1719
func cacheCmd() *cobra.Command {
@@ -22,25 +24,40 @@ func cacheCmd() *cobra.Command {
2224
PersistentPreRunE: ensureNixInstalled,
2325
}
2426

25-
copyCommand := &cobra.Command{
26-
Use: "copy <uri>",
27-
Short: "Copies all nix packages in current project to the cache at <uri>",
28-
Args: cobra.ExactArgs(1),
27+
uploadCommand := &cobra.Command{
28+
Use: "upload [installable]",
29+
Aliases: []string{"copy"}, // This mimics the nix command
30+
Short: "upload specified or nix packages in current project to cache",
31+
Long: heredoc.Doc(`
32+
Upload specified nix installable or nix packages in current project to cache.
33+
If [installable] is provided, only that installable will be uploaded.
34+
Otherwise, all packages in the project will be uploaded.
35+
To upload to specific cache, use --to flag. Otherwise, a cache from
36+
the cache provider will be used, if available.
37+
`),
38+
Args: cobra.MaximumNArgs(1),
2939
RunE: func(cmd *cobra.Command, args []string) error {
40+
if len(args) > 0 {
41+
return devbox.UploadInstallableToCache(
42+
cmd.Context(), cmd.ErrOrStderr(), flags.to, args[0],
43+
)
44+
}
3045
box, err := devbox.Open(&devopt.Opts{
3146
Dir: flags.path,
3247
Stderr: cmd.ErrOrStderr(),
3348
})
3449
if err != nil {
3550
return errors.WithStack(err)
3651
}
37-
return box.CacheCopy(cmd.Context(), args[0])
52+
return box.UploadProjectToCache(cmd.Context(), flags.to)
3853
},
3954
}
4055

41-
flags.pathFlag.register(copyCommand)
56+
flags.pathFlag.register(uploadCommand)
57+
uploadCommand.Flags().StringVar(
58+
&flags.to, "to", "", "URI of the cache to copy to")
4259

43-
cacheCommand.AddCommand(copyCommand)
60+
cacheCommand.AddCommand(uploadCommand)
4461
cacheCommand.Hidden = true
4562

4663
return cacheCommand

internal/devbox/cache.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package devbox
22

33
import (
44
"context"
5+
"io"
56

67
"go.jetpack.io/devbox/internal/devbox/providers/nixcache"
78
"go.jetpack.io/devbox/internal/nix"
89
)
910

10-
func (d *Devbox) CacheCopy(ctx context.Context, cacheURI string) error {
11+
func (d *Devbox) UploadProjectToCache(
12+
ctx context.Context,
13+
cacheURI string,
14+
) error {
1115
var err error
1216
cacheConfig := nixcache.NixCacheConfig{URI: cacheURI}
1317
if cacheConfig.URI == "" {
@@ -23,3 +27,19 @@ func (d *Devbox) CacheCopy(ctx context.Context, cacheURI string) error {
2327

2428
return nix.CopyInstallableToCache(ctx, d.stderr, cacheConfig.URI, profilePath)
2529
}
30+
31+
func UploadInstallableToCache(
32+
ctx context.Context,
33+
stderr io.Writer,
34+
cacheURI, installable string,
35+
) error {
36+
var err error
37+
cacheConfig := nixcache.NixCacheConfig{URI: cacheURI}
38+
if cacheConfig.URI == "" {
39+
cacheConfig, err = nixcache.Get().Config(ctx)
40+
if err != nil {
41+
return err
42+
}
43+
}
44+
return nix.CopyInstallableToCache(ctx, stderr, cacheConfig.URI, installable)
45+
}

0 commit comments

Comments
 (0)