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
47 changes: 47 additions & 0 deletions internal/boxcli/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2024 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.

package boxcli

import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox/internal/devbox"
"go.jetpack.io/devbox/internal/devbox/devopt"
)

type cacheFlags struct {
pathFlag
}

func cacheCmd() *cobra.Command {
flags := cacheFlags{}
cacheCommand := &cobra.Command{
Use: "cache",
Short: "Collection of commands to interact with nix cache",
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),
RunE: func(cmd *cobra.Command, args []string) error {
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])
},
}

flags.pathFlag.register(copyCommand)

cacheCommand.AddCommand(copyCommand)
cacheCommand.Hidden = true

return cacheCommand
}
27 changes: 18 additions & 9 deletions internal/boxcli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,37 @@ import (

// to be composed into xyzCmdFlags structs
type configFlags struct {
path string
pathFlag
environment string
}

func (flags *configFlags) register(cmd *cobra.Command) {
cmd.Flags().StringVarP(
&flags.path, "config", "c", "", "path to directory containing a devbox.json config file",
)
flags.pathFlag.register(cmd)
cmd.Flags().StringVar(
&flags.environment, "environment", "dev", "environment to use, when supported (e.g.secrets support dev, prod, preview.)",
)
}

func (flags *configFlags) registerPersistent(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(
&flags.path, "config", "c", "", "path to directory containing a devbox.json config file",
)
flags.pathFlag.registerPersistent(cmd)
cmd.PersistentFlags().StringVar(
&flags.environment, "environment", "dev", "environment to use, when supported (e.g. secrets support dev, prod, preview.)",
)
}

func (flags *configFlags) Environment() string {
return flags.environment
// pathFlag is a flag for specifying the path to a devbox.json file
type pathFlag struct {
path string
}

func (flags *pathFlag) register(cmd *cobra.Command) {
cmd.Flags().StringVarP(
&flags.path, "config", "c", "", "path to directory containing a devbox.json config file",
)
}

func (flags *pathFlag) registerPersistent(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(
&flags.path, "config", "c", "", "path to directory containing a devbox.json config file",
)
}
2 changes: 1 addition & 1 deletion internal/boxcli/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func pullCmdFunc(cmd *cobra.Command, url string, flags *pullCmdFlags) error {

return installCmdFunc(
cmd,
runCmdFlags{config: configFlags{path: flags.config.path}},
runCmdFlags{config: configFlags{pathFlag: pathFlag{path: flags.config.path}}},
)
}

Expand Down
1 change: 1 addition & 0 deletions internal/boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func RootCmd() *cobra.Command {
if featureflag.Auth.Enabled() {
command.AddCommand(authCmd())
}
command.AddCommand(cacheCmd())
command.AddCommand(createCmd())
command.AddCommand(secretsCmd())
command.AddCommand(generateCmd())
Expand Down
16 changes: 16 additions & 0 deletions internal/devbox/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package devbox

import (
"context"

"go.jetpack.io/devbox/internal/nix"
)

func (d *Devbox) CacheCopy(ctx context.Context, cacheURI string) error {
profilePath, err := d.profilePath()
if err != nil {
return err
}

return nix.CopyInstallableToCache(ctx, d.stderr, cacheURI, profilePath)
}
35 changes: 35 additions & 0 deletions internal/nix/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nix

import (
"context"
"fmt"
"io"
"os"
)

func CopyInstallableToCache(
ctx context.Context,
out io.Writer,
// Note: installable is a string instead of a flake.Installable
// because flake.Installable does not support store paths yet. It converts
// paths into "path" flakes which is not what we want for /nix/store paths.
// TODO: Add support for store paths in flake.Installable
to, installable string,
) error {
fmt.Fprintf(out, "Copying %s to %s\n", installable, to)
cmd := commandContext(
ctx,
"copy", "--to", to,
// --refresh checks the cache to ensure it is up to date. Otherwise if
// anything has was copied previously from this machine and then purged
// it may not be copied again. It's fairly fast, but not instant.
"--refresh",
installable,
)

cmd.Stdin = os.Stdin
cmd.Stdout = out
cmd.Stderr = out

return cmd.Run()
}