From fd06118cc672a7fd5ee094f8cfaee4700d4ffd67 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Tue, 12 Mar 2024 16:02:18 -0700 Subject: [PATCH 1/2] [nix-cache] Add cache copy command --- internal/boxcli/cache.go | 48 +++++++++++++++++++++++++++++++++++++++ internal/boxcli/config.go | 27 ++++++++++++++-------- internal/boxcli/pull.go | 2 +- internal/boxcli/root.go | 1 + internal/devbox/cache.go | 16 +++++++++++++ internal/nix/cache.go | 35 ++++++++++++++++++++++++++++ 6 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 internal/boxcli/cache.go create mode 100644 internal/devbox/cache.go create mode 100644 internal/nix/cache.go diff --git a/internal/boxcli/cache.go b/internal/boxcli/cache.go new file mode 100644 index 00000000000..70a4040ed78 --- /dev/null +++ b/internal/boxcli/cache.go @@ -0,0 +1,48 @@ +// 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 ", + Short: "Copy nix store paths to the cache", + Long: "Copies all nix packages in current project to url.", + 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 +} diff --git a/internal/boxcli/config.go b/internal/boxcli/config.go index 2bd10c5cfe4..cd2f3f47c49 100644 --- a/internal/boxcli/config.go +++ b/internal/boxcli/config.go @@ -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", + ) } diff --git a/internal/boxcli/pull.go b/internal/boxcli/pull.go index 4a925e8e472..23feb300bb9 100644 --- a/internal/boxcli/pull.go +++ b/internal/boxcli/pull.go @@ -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}}}, ) } diff --git a/internal/boxcli/root.go b/internal/boxcli/root.go index 4132425a976..370c4c7a868 100644 --- a/internal/boxcli/root.go +++ b/internal/boxcli/root.go @@ -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()) diff --git a/internal/devbox/cache.go b/internal/devbox/cache.go new file mode 100644 index 00000000000..2eef4cf23d1 --- /dev/null +++ b/internal/devbox/cache.go @@ -0,0 +1,16 @@ +package devbox + +import ( + "context" + + "go.jetpack.io/devbox/internal/nix" +) + +func (d *Devbox) CacheCopy(ctx context.Context, cacheURL string) error { + profilePath, err := d.profilePath() + if err != nil { + return err + } + + return nix.CopyInstallableToCache(ctx, d.stderr, cacheURL, profilePath) +} diff --git a/internal/nix/cache.go b/internal/nix/cache.go new file mode 100644 index 00000000000..23d47332f81 --- /dev/null +++ b/internal/nix/cache.go @@ -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() +} From 1a0e7ed068bed4169ea2567462c9b3c28f826b50 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Wed, 13 Mar 2024 15:36:08 -0700 Subject: [PATCH 2/2] Fixes --- internal/boxcli/cache.go | 5 ++--- internal/devbox/cache.go | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/boxcli/cache.go b/internal/boxcli/cache.go index 70a4040ed78..1b16c19ab33 100644 --- a/internal/boxcli/cache.go +++ b/internal/boxcli/cache.go @@ -23,9 +23,8 @@ func cacheCmd() *cobra.Command { } copyCommand := &cobra.Command{ - Use: "copy ", - Short: "Copy nix store paths to the cache", - Long: "Copies all nix packages in current project to url.", + Use: "copy ", + Short: "Copies all nix packages in current project to the cache at ", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { box, err := devbox.Open(&devopt.Opts{ diff --git a/internal/devbox/cache.go b/internal/devbox/cache.go index 2eef4cf23d1..fa09ef5a413 100644 --- a/internal/devbox/cache.go +++ b/internal/devbox/cache.go @@ -6,11 +6,11 @@ import ( "go.jetpack.io/devbox/internal/nix" ) -func (d *Devbox) CacheCopy(ctx context.Context, cacheURL string) error { +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, cacheURL, profilePath) + return nix.CopyInstallableToCache(ctx, d.stderr, cacheURI, profilePath) }