From 1e810364b0233bd1d67340b7b23e144a7237c3af Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Mon, 25 Sep 2023 14:44:28 -0700 Subject: [PATCH] [sync-lock] Add sync-lock-reference flag --- internal/boxcli/update.go | 20 ++++++++++++++----- internal/impl/devopt/devboxopts.go | 5 +++-- internal/impl/update.go | 2 +- internal/lock/sync.go | 31 ++++++++++++++++++++---------- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/internal/boxcli/update.go b/internal/boxcli/update.go index dba96dc5965..7ccaae8e8ec 100644 --- a/internal/boxcli/update.go +++ b/internal/boxcli/update.go @@ -13,8 +13,9 @@ import ( ) type updateCmdFlags struct { - config configFlags - sync bool + config configFlags + sync bool + ReferenceLockFilePath string } func updateCmd() *cobra.Command { @@ -39,7 +40,15 @@ func updateCmd() *cobra.Command { "sync-lock", false, "Sync all devbox.lock dependencies in multiple projects. "+ - "Dependencies will sync to the latest local version.", + "Dependencies will sync to the latest resolved local version.", + ) + command.Flags().StringVar( + &flags.ReferenceLockFilePath, + "sync-lock-reference", + "", + "Path to a devbox.lock file to use as a reference when syncing lockfiles. "+ + "If none is provided then most recent last modified dependency is used. "+ + "Must be used with --sync-lock flag.", ) return command } @@ -58,7 +67,8 @@ func updateCmdFunc(cmd *cobra.Command, args []string, flags *updateCmdFlags) err } return box.Update(cmd.Context(), devopt.UpdateOpts{ - Pkgs: args, - Sync: flags.sync, + Pkgs: args, + ReferenceLockFilePath: flags.ReferenceLockFilePath, + Sync: flags.sync, }) } diff --git a/internal/impl/devopt/devboxopts.go b/internal/impl/devopt/devboxopts.go index 2edc71c2e01..37af276cb32 100644 --- a/internal/impl/devopt/devboxopts.go +++ b/internal/impl/devopt/devboxopts.go @@ -39,6 +39,7 @@ type Credentials struct { } type UpdateOpts struct { - Pkgs []string - Sync bool + Pkgs []string + ReferenceLockFilePath string + Sync bool } diff --git a/internal/impl/update.go b/internal/impl/update.go index 584bb9dd780..bbdf96acdf1 100644 --- a/internal/impl/update.go +++ b/internal/impl/update.go @@ -21,7 +21,7 @@ import ( func (d *Devbox) Update(ctx context.Context, opts devopt.UpdateOpts) error { if opts.Sync { - return lock.SyncLockfiles() + return lock.SyncLockfiles(opts) } inputs, err := d.inputsToUpdate(opts.Pkgs...) diff --git a/internal/lock/sync.go b/internal/lock/sync.go index 60d10e6efd8..92797852dac 100644 --- a/internal/lock/sync.go +++ b/internal/lock/sync.go @@ -8,19 +8,30 @@ import ( "go.jetpack.io/devbox/internal/cuecfg" "go.jetpack.io/devbox/internal/debug" + "go.jetpack.io/devbox/internal/impl/devopt" ) -func SyncLockfiles() error { +func SyncLockfiles(opts devopt.UpdateOpts) error { lockfilePaths, err := collectLockfiles() if err != nil { return err } - latestPackages, err := latestPackages(lockfilePaths) + preferredPackages, err := latestPackages(lockfilePaths) if err != nil { return err } + if opts.ReferenceLockFilePath != "" { + var referenceLockFile File + if err := cuecfg.ParseFile(opts.ReferenceLockFilePath, &referenceLockFile); err != nil { + return err + } + for key, pkg := range referenceLockFile.Packages { + preferredPackages[key] = pkg + } + } + for _, lockfilePath := range lockfilePaths { var lockFile File if err := cuecfg.ParseFile(lockfilePath, &lockFile); err != nil { @@ -28,16 +39,16 @@ func SyncLockfiles() error { } changed := false - for key, latestPkg := range latestPackages { + for key, preferredPkg := range preferredPackages { if pkg, exists := lockFile.Packages[key]; exists { - if pkg.LastModified != latestPkg.LastModified { - lockFile.Packages[key].AllowInsecure = latestPkg.AllowInsecure - lockFile.Packages[key].LastModified = latestPkg.LastModified + if pkg.LastModified != preferredPkg.LastModified { + lockFile.Packages[key].AllowInsecure = preferredPkg.AllowInsecure + lockFile.Packages[key].LastModified = preferredPkg.LastModified // PluginVersion is intentionally omitted - lockFile.Packages[key].Resolved = latestPkg.Resolved - lockFile.Packages[key].Source = latestPkg.Source - lockFile.Packages[key].Version = latestPkg.Version - lockFile.Packages[key].Systems = latestPkg.Systems + lockFile.Packages[key].Resolved = preferredPkg.Resolved + lockFile.Packages[key].Source = preferredPkg.Source + lockFile.Packages[key].Version = preferredPkg.Version + lockFile.Packages[key].Systems = preferredPkg.Systems changed = true } }