Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions internal/boxcli/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,6 +13,7 @@ import (

type cacheFlags struct {
pathFlag
to string
}

func cacheCmd() *cobra.Command {
Expand All @@ -22,25 +24,40 @@ func cacheCmd() *cobra.Command {
PersistentPreRunE: ensureNixInstalled,
}

copyCommand := &cobra.Command{
Use: "copy <uri>",
Short: "Copies all nix packages in current project to the cache at <uri>",
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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow this description at all!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upload specified nix installable or nix packages in current project to cache

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will improve

Long: heredoc.Doc(`
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
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(),
})
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
Expand Down
22 changes: 21 additions & 1 deletion internal/devbox/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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)
}