From 7561fdc802b0ae22e3c077fbd2b91398f36e159b Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 22 Sep 2023 11:37:20 -0700 Subject: [PATCH 1/8] [update] Add sync flag which syncs all dependencies to same resolved version under current working dir --- devbox.go | 2 +- internal/boxcli/update.go | 18 ++++++- internal/impl/devopt/devboxopts.go | 5 ++ internal/impl/update.go | 9 +++- internal/lock/sync.go | 86 ++++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 internal/lock/sync.go diff --git a/devbox.go b/devbox.go index 469b8064705..8f77543d1b8 100644 --- a/devbox.go +++ b/devbox.go @@ -50,7 +50,7 @@ type Devbox interface { StopServices(ctx context.Context, allProjects bool, services ...string) error ListServices(ctx context.Context) error - Update(ctx context.Context, pkgs ...string) error + Update(ctx context.Context, opts devopt.UpdateOpts) error } // Open opens a devbox by reading the config file in dir. diff --git a/internal/boxcli/update.go b/internal/boxcli/update.go index 7c7068c31e7..52819da7ac6 100644 --- a/internal/boxcli/update.go +++ b/internal/boxcli/update.go @@ -8,11 +8,13 @@ import ( "github.com/spf13/cobra" "go.jetpack.io/devbox" + "go.jetpack.io/devbox/internal/boxcli/usererr" "go.jetpack.io/devbox/internal/impl/devopt" ) type updateCmdFlags struct { config configFlags + sync bool } func updateCmd() *cobra.Command { @@ -32,6 +34,13 @@ func updateCmd() *cobra.Command { } flags.config.register(command) + command.Flags().BoolVar( + &flags.sync, + "sync", + false, + "Sync all devbox.lock dependencies in multiple projects. "+ + "Dependencies will sync to the latest local version.", + ) return command } @@ -44,5 +53,12 @@ func updateCmdFunc(cmd *cobra.Command, args []string, flags *updateCmdFlags) err return errors.WithStack(err) } - return box.Update(cmd.Context(), args...) + if len(args) > 0 && flags.sync { + return usererr.New("cannot specify both a package and --sync") + } + + return box.Update(cmd.Context(), devopt.UpdateOpts{ + Pkgs: args, + Sync: flags.sync, + }) } diff --git a/internal/impl/devopt/devboxopts.go b/internal/impl/devopt/devboxopts.go index 87a4a5dd570..2edc71c2e01 100644 --- a/internal/impl/devopt/devboxopts.go +++ b/internal/impl/devopt/devboxopts.go @@ -37,3 +37,8 @@ type Credentials struct { Email string Sub string } + +type UpdateOpts struct { + Pkgs []string + Sync bool +} diff --git a/internal/impl/update.go b/internal/impl/update.go index 18b33522bfc..584bb9dd780 100644 --- a/internal/impl/update.go +++ b/internal/impl/update.go @@ -9,6 +9,7 @@ import ( "go.jetpack.io/devbox/internal/boxcli/featureflag" "go.jetpack.io/devbox/internal/devpkg" + "go.jetpack.io/devbox/internal/impl/devopt" "go.jetpack.io/devbox/internal/lock" "go.jetpack.io/devbox/internal/nix" "go.jetpack.io/devbox/internal/nix/nixprofile" @@ -18,8 +19,12 @@ import ( "go.jetpack.io/devbox/internal/wrapnix" ) -func (d *Devbox) Update(ctx context.Context, pkgs ...string) error { - inputs, err := d.inputsToUpdate(pkgs...) +func (d *Devbox) Update(ctx context.Context, opts devopt.UpdateOpts) error { + if opts.Sync { + return lock.SyncLockfiles() + } + + inputs, err := d.inputsToUpdate(opts.Pkgs...) if err != nil { return err } diff --git a/internal/lock/sync.go b/internal/lock/sync.go new file mode 100644 index 00000000000..c2bdc9f138f --- /dev/null +++ b/internal/lock/sync.go @@ -0,0 +1,86 @@ +package lock + +import ( + "fmt" + "os" + "path/filepath" + "time" + + "go.jetpack.io/devbox/internal/cuecfg" +) + +func SyncLockfiles() error { + latestPackages, err := latestPackages() + if err != nil { + return err + } + + // Step 2: Update the devbox.lock files + return filepath.Walk(".", func(path string, fi os.FileInfo, err error) error { + if err != nil { + return err + } + + if !fi.IsDir() && filepath.Base(path) == "devbox.lock" { + var lockFile File + if err := cuecfg.ParseFile(path, &lockFile); err != nil { + return err + } + + changed := false + for key, latestPkg := range latestPackages { + if pkg, exists := lockFile.Packages[key]; exists { + if pkg.LastModified != latestPkg.LastModified { + lockFile.Packages[key] = latestPkg + changed = true + } + } + } + + if changed { + if err = cuecfg.WriteFile(path, lockFile); err != nil { + return err + } + fmt.Printf("Updated: %s\n", path) + } + } + return nil + }) +} + +func latestPackages() (map[string]*Package, error) { + latestPackages := make(map[string]*Package) + + err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error { + if err != nil { + return err + } + + if !fi.IsDir() && filepath.Base(path) == "devbox.lock" { + var lockFile File + if err := cuecfg.ParseFile(path, &lockFile); err != nil { + return err + } + + for key, pkg := range lockFile.Packages { + if latestPkg, exists := latestPackages[key]; exists { + currentTime, err := time.Parse(time.RFC3339, pkg.LastModified) + if err != nil { + return err + } + latestTime, err := time.Parse(time.RFC3339, latestPkg.LastModified) + if err != nil { + return err + } + if currentTime.After(latestTime) { + latestPackages[key] = pkg + } + } else { + latestPackages[key] = pkg + } + } + } + return nil + }) + return latestPackages, err +} From c56a621e84a0b0a9e524589eab92f20dfca27181 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 22 Sep 2023 11:57:10 -0700 Subject: [PATCH 2/8] Don't copy over plugin version --- internal/lock/package.go | 4 +++- internal/lock/sync.go | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/lock/package.go b/internal/lock/package.go index 02df9e97ca1..9198f8c72d8 100644 --- a/internal/lock/package.go +++ b/internal/lock/package.go @@ -14,9 +14,11 @@ type Package struct { PluginVersion string `json:"plugin_version,omitempty"` Resolved string `json:"resolved,omitempty"` Source string `json:"source,omitempty"` - Version string `json:"version,omitempty"` // Systems is keyed by the system name Systems map[string]*SystemInfo `json:"systems,omitempty"` + Version string `json:"version,omitempty"` + + // NOTE: if you add more fields, please update SyncLockfiles } type SystemInfo struct { diff --git a/internal/lock/sync.go b/internal/lock/sync.go index c2bdc9f138f..5a66cf7d037 100644 --- a/internal/lock/sync.go +++ b/internal/lock/sync.go @@ -31,7 +31,13 @@ func SyncLockfiles() error { for key, latestPkg := range latestPackages { if pkg, exists := lockFile.Packages[key]; exists { if pkg.LastModified != latestPkg.LastModified { - lockFile.Packages[key] = latestPkg + lockFile.Packages[key].AllowInsecure = latestPkg.AllowInsecure + lockFile.Packages[key].LastModified = latestPkg.LastModified + // PluginVersion is intentionally omitted + lockFile.Packages[key].Resolved = latestPkg.Resolved + lockFile.Packages[key].Source = latestPkg.Source + lockFile.Packages[key].Systems = latestPkg.Systems + lockFile.Packages[key].Version = latestPkg.Version changed = true } } From 55b3fb83254dfa5b6433ef56b3bfc2876caa0329 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 22 Sep 2023 11:58:55 -0700 Subject: [PATCH 3/8] Revert order --- internal/lock/package.go | 2 +- internal/lock/sync.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/lock/package.go b/internal/lock/package.go index 9198f8c72d8..7c065684e39 100644 --- a/internal/lock/package.go +++ b/internal/lock/package.go @@ -14,9 +14,9 @@ type Package struct { PluginVersion string `json:"plugin_version,omitempty"` Resolved string `json:"resolved,omitempty"` Source string `json:"source,omitempty"` + Version string `json:"version,omitempty"` // Systems is keyed by the system name Systems map[string]*SystemInfo `json:"systems,omitempty"` - Version string `json:"version,omitempty"` // NOTE: if you add more fields, please update SyncLockfiles } diff --git a/internal/lock/sync.go b/internal/lock/sync.go index 5a66cf7d037..7b156cb0675 100644 --- a/internal/lock/sync.go +++ b/internal/lock/sync.go @@ -36,8 +36,8 @@ func SyncLockfiles() error { // PluginVersion is intentionally omitted lockFile.Packages[key].Resolved = latestPkg.Resolved lockFile.Packages[key].Source = latestPkg.Source - lockFile.Packages[key].Systems = latestPkg.Systems lockFile.Packages[key].Version = latestPkg.Version + lockFile.Packages[key].Systems = latestPkg.Systems changed = true } } From 5b2001ed2ed918ef1983ccaff937a19ebeba307b Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 22 Sep 2023 11:59:58 -0700 Subject: [PATCH 4/8] Update lockfiles --- examples/databases/mariadb/devbox.lock | 23 +++- examples/databases/mysql/devbox.lock | 6 +- .../nodejs/nodejs-yarn/devbox.lock | 5 +- examples/stacks/laravel/devbox.lock | 128 +++++++++++++++--- examples/stacks/spring/devbox.lock | 23 +++- 5 files changed, 153 insertions(+), 32 deletions(-) diff --git a/examples/databases/mariadb/devbox.lock b/examples/databases/mariadb/devbox.lock index df6d1997223..32b24bfefcb 100644 --- a/examples/databases/mariadb/devbox.lock +++ b/examples/databases/mariadb/devbox.lock @@ -2,10 +2,25 @@ "lockfile_version": "1", "packages": { "mariadb@latest": { - "last_modified": "2023-05-01T16:53:22Z", + "last_modified": "2023-08-30T00:25:28Z", "plugin_version": "0.0.1", - "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#mariadb", - "version": "10.6.12" + "resolved": "github:NixOS/nixpkgs/a63a64b593dcf2fe05f7c5d666eb395950f36bc9#mariadb_110", + "source": "devbox-search", + "version": "11.0.3", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/d5nb44vw8yy22lc21ld75nndmn9c3cgr-mariadb-server-11.0.3" + }, + "aarch64-linux": { + "store_path": "/nix/store/sz5n9bkcjxklk4jd0p5h26yi5j79wh7h-mariadb-server-11.0.3" + }, + "x86_64-darwin": { + "store_path": "/nix/store/4l6h83flncplm3kmry1w08msyy7b7vdw-mariadb-server-11.0.3" + }, + "x86_64-linux": { + "store_path": "/nix/store/047g9nxp6jb2bqj1f53qk86sjzrscbmj-mariadb-server-11.0.3" + } + } } } -} \ No newline at end of file +} diff --git a/examples/databases/mysql/devbox.lock b/examples/databases/mysql/devbox.lock index f178c2c9ca8..731e9791b84 100644 --- a/examples/databases/mysql/devbox.lock +++ b/examples/databases/mysql/devbox.lock @@ -2,10 +2,10 @@ "lockfile_version": "1", "packages": { "mysql80@latest": { - "last_modified": "2023-05-01T16:53:22Z", + "last_modified": "2023-06-29T16:20:38Z", "plugin_version": "0.0.1", - "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#mysql80", + "resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#mysql80", "version": "8.0.33" } } -} \ No newline at end of file +} diff --git a/examples/development/nodejs/nodejs-yarn/devbox.lock b/examples/development/nodejs/nodejs-yarn/devbox.lock index 7e803a7c8ef..fa8d113fd1e 100644 --- a/examples/development/nodejs/nodejs-yarn/devbox.lock +++ b/examples/development/nodejs/nodejs-yarn/devbox.lock @@ -22,8 +22,9 @@ } }, "yarn@1.22": { - "last_modified": "2023-05-01T16:53:22Z", - "resolved": "github:NixOS/nixpkgs/844ffa82bbe2a2779c86ab3a72ff1b4176cec467#yarn", + "last_modified": "2023-05-21T19:52:09Z", + "resolved": "github:NixOS/nixpkgs/9356eead97d8d16956b0226d78f76bd66e06cb60#yarn", + "source": "devbox-search", "version": "1.22.19", "systems": { "aarch64-darwin": { diff --git a/examples/stacks/laravel/devbox.lock b/examples/stacks/laravel/devbox.lock index 7a236275d5e..f5795a96a80 100644 --- a/examples/stacks/laravel/devbox.lock +++ b/examples/stacks/laravel/devbox.lock @@ -2,37 +2,127 @@ "lockfile_version": "1", "packages": { "mariadb@latest": { - "last_modified": "2023-05-01T16:53:22Z", + "last_modified": "2023-08-30T00:25:28Z", "plugin_version": "0.0.1", - "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#mariadb", - "version": "10.6.12" + "resolved": "github:NixOS/nixpkgs/a63a64b593dcf2fe05f7c5d666eb395950f36bc9#mariadb_110", + "source": "devbox-search", + "version": "11.0.3", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/d5nb44vw8yy22lc21ld75nndmn9c3cgr-mariadb-server-11.0.3" + }, + "aarch64-linux": { + "store_path": "/nix/store/sz5n9bkcjxklk4jd0p5h26yi5j79wh7h-mariadb-server-11.0.3" + }, + "x86_64-darwin": { + "store_path": "/nix/store/4l6h83flncplm3kmry1w08msyy7b7vdw-mariadb-server-11.0.3" + }, + "x86_64-linux": { + "store_path": "/nix/store/047g9nxp6jb2bqj1f53qk86sjzrscbmj-mariadb-server-11.0.3" + } + } }, "nginx@latest": { - "last_modified": "2023-05-01T16:53:22Z", + "last_modified": "2023-09-04T16:24:30Z", "plugin_version": "0.0.1", - "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#nginx", - "version": "1.24.0" + "resolved": "github:NixOS/nixpkgs/3c15feef7770eb5500a4b8792623e2d6f598c9c1#nginx", + "source": "devbox-search", + "version": "1.24.0", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/prz9lx44d3hicpmsri5rm9qk44r79ng8-nginx-1.24.0" + }, + "aarch64-linux": { + "store_path": "/nix/store/b2v5yw11gmywahlyhbqajml7hjdkhsar-nginx-1.24.0" + }, + "x86_64-darwin": { + "store_path": "/nix/store/1nyjvgj3hbhck80wkwi0h18561xbp3sy-nginx-1.24.0" + }, + "x86_64-linux": { + "store_path": "/nix/store/vc66rk5b86lx1myxr18qkgzha0fjx2ks-nginx-1.24.0" + } + } }, "nodejs@18": { - "last_modified": "2023-05-01T16:53:22Z", - "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#nodejs", - "version": "18.16.0" + "last_modified": "2023-08-30T00:25:28Z", + "resolved": "github:NixOS/nixpkgs/a63a64b593dcf2fe05f7c5d666eb395950f36bc9#nodejs_18", + "source": "devbox-search", + "version": "18.17.1", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/xpqj3zg5lx25abv9qybiqd7gcs8b81fp-nodejs-18.17.1" + }, + "aarch64-linux": { + "store_path": "/nix/store/svc3bwhi6i1jd5387w8dwps23s7d4a62-nodejs-18.17.1" + }, + "x86_64-darwin": { + "store_path": "/nix/store/kjsf1qk9w4rknr02silyfq4lxlkh53xq-nodejs-18.17.1" + }, + "x86_64-linux": { + "store_path": "/nix/store/51nhk6ycfnj895q07v94jsrwmk2jmz8j-nodejs-18.17.1" + } + } }, "php81Extensions.xdebug@latest": { - "last_modified": "2023-05-01T16:53:22Z", - "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#php81Extensions.xdebug", - "version": "3.2.1" + "last_modified": "2023-08-30T00:25:28Z", + "resolved": "github:NixOS/nixpkgs/a63a64b593dcf2fe05f7c5d666eb395950f36bc9#php81Extensions.xdebug", + "source": "devbox-search", + "version": "3.2.2", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/isd181vs56d55xarvlzqr0cl2c65skq7-php-xdebug-3.2.2" + }, + "aarch64-linux": { + "store_path": "/nix/store/c0ij58mr8mqcbx6kkrs1wa6x0m433hzc-php-xdebug-3.2.2" + }, + "x86_64-darwin": { + "store_path": "/nix/store/v4i2qbb7aivj3x4i7ipjg1vnfdc3ynvw-php-xdebug-3.2.2" + }, + "x86_64-linux": { + "store_path": "/nix/store/q6gp1rn2p6319l8rbwk0arvbbi8kx6bg-php-xdebug-3.2.2" + } + } }, "php81Packages.composer@latest": { - "last_modified": "2023-05-01T16:53:22Z", - "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#php81Packages.composer", - "version": "2.5.5" + "last_modified": "2023-08-30T00:25:28Z", + "resolved": "github:NixOS/nixpkgs/a63a64b593dcf2fe05f7c5d666eb395950f36bc9#php81Packages.composer", + "source": "devbox-search", + "version": "2.5.8", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/7hxmkxa9bmaplyy1ixl0yxv3j0qvxvc3-php-composer-2.5.8" + }, + "aarch64-linux": { + "store_path": "/nix/store/vwrbpz9wr7blr7alx0fl3x79ym3jbdqa-php-composer-2.5.8" + }, + "x86_64-darwin": { + "store_path": "/nix/store/fjy357jpj9q2bfahsgnak4mrh8y07izw-php-composer-2.5.8" + }, + "x86_64-linux": { + "store_path": "/nix/store/6w2vrfjdwhr3mj1magr2rmbycln379f8-php-composer-2.5.8" + } + } }, "php@8.1": { - "last_modified": "2023-05-01T16:53:22Z", + "last_modified": "2023-09-04T16:24:30Z", "plugin_version": "0.0.1", - "resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#php", - "version": "8.1.18" + "resolved": "github:NixOS/nixpkgs/3c15feef7770eb5500a4b8792623e2d6f598c9c1#php81", + "source": "devbox-search", + "version": "8.1.23", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/lb1kvhs6brfphpmxa36kn9fki89ijx7f-php-with-extensions-8.1.23" + }, + "aarch64-linux": { + "store_path": "/nix/store/2f935v7hg56faxdp0myam0v8ppdidrd1-php-with-extensions-8.1.23" + }, + "x86_64-darwin": { + "store_path": "/nix/store/v5fdxi8pasc8mxacmg76q9qzqpsr04w6-php-with-extensions-8.1.23" + }, + "x86_64-linux": { + "store_path": "/nix/store/ky5w9izkk4yyhni3nh31nicmcfch5ndz-php-with-extensions-8.1.23" + } + } }, "redis@latest": { "last_modified": "2023-05-01T16:53:22Z", @@ -41,4 +131,4 @@ "version": "7.0.11" } } -} \ No newline at end of file +} diff --git a/examples/stacks/spring/devbox.lock b/examples/stacks/spring/devbox.lock index cfb4e3bdb29..d924136b78f 100644 --- a/examples/stacks/spring/devbox.lock +++ b/examples/stacks/spring/devbox.lock @@ -2,10 +2,25 @@ "lockfile_version": "1", "packages": { "gradle@latest": { - "last_modified": "2023-06-29T16:20:38Z", + "last_modified": "2023-08-30T00:25:28Z", "plugin_version": "0.0.1", - "resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#gradle", - "version": "8.0.1" + "resolved": "github:NixOS/nixpkgs/a63a64b593dcf2fe05f7c5d666eb395950f36bc9#gradle", + "source": "devbox-search", + "version": "8.3", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/4xjjls354jr8w7083qxsrpfs1fgl80fj-gradle-8.3" + }, + "aarch64-linux": { + "store_path": "/nix/store/f0l5fscvcl9yand9jfpk5icr2ac8qx2v-gradle-8.3" + }, + "x86_64-darwin": { + "store_path": "/nix/store/rxm9mpxswrd77fd6lafqcxaxizjlk1l4-gradle-8.3" + }, + "x86_64-linux": { + "store_path": "/nix/store/qvp5jiik6q27qwlgiq80fx96wyh0r0nb-gradle-8.3" + } + } }, "jdk@17": { "last_modified": "2023-06-29T16:20:38Z", @@ -19,4 +34,4 @@ "version": "8.0.33" } } -} \ No newline at end of file +} From c83e376e3c74d1828b615594d3b5bdccb0dfa455 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 22 Sep 2023 13:46:05 -0700 Subject: [PATCH 5/8] Rename flag to sync-lock --- internal/boxcli/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/boxcli/update.go b/internal/boxcli/update.go index 52819da7ac6..dba96dc5965 100644 --- a/internal/boxcli/update.go +++ b/internal/boxcli/update.go @@ -36,7 +36,7 @@ func updateCmd() *cobra.Command { flags.config.register(command) command.Flags().BoolVar( &flags.sync, - "sync", + "sync-lock", false, "Sync all devbox.lock dependencies in multiple projects. "+ "Dependencies will sync to the latest local version.", From 66bf7eaf97ee62f71b15caf5b0320f57efea1178 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Mon, 25 Sep 2023 13:42:40 -0700 Subject: [PATCH 6/8] Make sync more efficient --- go.mod | 2 +- go.sum | 8 +- internal/lock/sync.go | 167 ++++++++++++++++++++++++++++-------------- 3 files changed, 114 insertions(+), 63 deletions(-) diff --git a/go.mod b/go.mod index c5b7c0387e1..98c840e2948 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect github.com/pkg/errors v0.9.1 github.com/rogpeppe/go-internal v1.11.0 + github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 github.com/samber/lo v1.38.1 github.com/segmentio/analytics-go v3.1.0+incompatible github.com/spf13/cobra v1.7.0 @@ -50,7 +51,6 @@ require ( github.com/InVisionApp/go-health/v2 v2.1.3 // indirect github.com/InVisionApp/go-logger v1.0.1 // indirect github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect - github.com/adrg/xdg v0.4.0 // indirect github.com/andybalholm/brotli v1.0.5 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.3 // indirect diff --git a/go.sum b/go.sum index e99de721548..fa6042c0a1d 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,6 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDe github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA= github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g= github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= -github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls= -github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E= github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0= github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= @@ -223,6 +221,8 @@ github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w= github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI= +github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/segmentio/analytics-go v3.1.0+incompatible h1:IyiOfUgQFVHvsykKKbdI7ZsH374uv3/DfZUo9+G0Z80= @@ -245,7 +245,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= @@ -264,8 +263,6 @@ github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036/go.mod h1:gqRgreBU github.com/zaffka/mongodb-boltdb-mock v0.0.0-20221014194232-b4bb03fbe3a0/go.mod h1:GsDD1qsG+86MeeCG7ndi6Ei3iGthKL3wQ7PTFigDfNY= github.com/zealic/go2node v0.1.0 h1:ofxpve08cmLJBwFdI0lPCk9jfwGWOSD+s6216x0oAaA= github.com/zealic/go2node v0.1.0/go.mod h1:GrkFr+HctXwP7vzcU9RsgtAeJjTQ6Ud0IPCQAqpTfBg= -go.jetpack.io/pkg v0.0.0-20230919193042-473f1790dbf6 h1:X0sXrWsTsQcUcikm+uKOg6hJ7H8g+0SaK05s8kfg7a0= -go.jetpack.io/pkg v0.0.0-20230919193042-473f1790dbf6/go.mod h1:6RVzBortLFlql8s8oKJTX2+H7DDzp8Lr7wiIOI3FauU= go.jetpack.io/pkg v0.0.0-20230920232528-54278537129b h1:8sbFeLQ7GtVP7CxvpmBoOh6w2ZTK4DyZuMkyiIGFdjs= go.jetpack.io/pkg v0.0.0-20230920232528-54278537129b/go.mod h1:drBQ4v8Hxs501Y3KK3vbsNBhn/TEMEDHrdXK7cOb9yg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -300,7 +297,6 @@ golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/lock/sync.go b/internal/lock/sync.go index 7b156cb0675..82ec73cf50b 100644 --- a/internal/lock/sync.go +++ b/internal/lock/sync.go @@ -2,91 +2,146 @@ package lock import ( "fmt" - "os" + "io/fs" "path/filepath" + "strings" "time" + "github.com/pkg/errors" + ignore "github.com/sabhiram/go-gitignore" "go.jetpack.io/devbox/internal/cuecfg" + "go.jetpack.io/devbox/internal/debug" ) func SyncLockfiles() error { - latestPackages, err := latestPackages() + lockfilePaths, err := collectLockfiles() if err != nil { return err } - // Step 2: Update the devbox.lock files - return filepath.Walk(".", func(path string, fi os.FileInfo, err error) error { - if err != nil { + latestPackages, err := latestPackages(lockfilePaths) + if err != nil { + return err + } + + for _, lockfilePath := range lockfilePaths { + var lockFile File + if err := cuecfg.ParseFile(lockfilePath, &lockFile); err != nil { return err } - if !fi.IsDir() && filepath.Base(path) == "devbox.lock" { - var lockFile File - if err := cuecfg.ParseFile(path, &lockFile); err != nil { - return err + changed := false + for key, latestPkg := range latestPackages { + if pkg, exists := lockFile.Packages[key]; exists { + if pkg.LastModified != latestPkg.LastModified { + lockFile.Packages[key].AllowInsecure = latestPkg.AllowInsecure + lockFile.Packages[key].LastModified = latestPkg.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 + changed = true + } } + } - changed := false - for key, latestPkg := range latestPackages { - if pkg, exists := lockFile.Packages[key]; exists { - if pkg.LastModified != latestPkg.LastModified { - lockFile.Packages[key].AllowInsecure = latestPkg.AllowInsecure - lockFile.Packages[key].LastModified = latestPkg.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 - changed = true - } - } + if changed { + if err = cuecfg.WriteFile(lockfilePath, lockFile); err != nil { + return err } + fmt.Printf("Updated: %s\n", lockfilePath) + } + } - if changed { - if err = cuecfg.WriteFile(path, lockFile); err != nil { - return err + return nil +} + +func latestPackages(lockfilePaths []string) (map[string]*Package, error) { + latestPackages := make(map[string]*Package) + + for _, lockFilePath := range lockfilePaths { + var lockFile File + if err := cuecfg.ParseFile(lockFilePath, &lockFile); err != nil { + return nil, err + } + for key, pkg := range lockFile.Packages { + if latestPkg, exists := latestPackages[key]; exists { + // Ignore error, which makes currentTime.After always false. + currentTime, _ := time.Parse(time.RFC3339, pkg.LastModified) + latestTime, err := time.Parse(time.RFC3339, latestPkg.LastModified) + if err != nil { + return nil, err + } + if currentTime.After(latestTime) { + latestPackages[key] = pkg } - fmt.Printf("Updated: %s\n", path) + } else if _, err := time.Parse(time.RFC3339, pkg.LastModified); err == nil { + latestPackages[key] = pkg } } - return nil - }) + } + + return latestPackages, nil } -func latestPackages() (map[string]*Package, error) { - latestPackages := make(map[string]*Package) +func collectLockfiles() ([]string, error) { + defer debug.FunctionTimer().End() - err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error { - if err != nil { - return err + type filterFuncStackEntry struct { + filterFunc func(string) bool + path string + } + + var filterFuncStack []filterFuncStackEntry + + var filterFunc = func(path string) bool { + // Start at the top of the stack which has most specific gitignore. + for i := len(filterFuncStack) - 1; i >= 0; i-- { + if strings.HasPrefix(path, filterFuncStack[i].path) { + // Any gitignore that is not a prefix can be popped off the stack + // because WalkDir is depth first which means we're already in a + // different branch + filterFuncStack = filterFuncStack[:i+1] + return filterFuncStack[i].filterFunc(path) + } } + return false + } - if !fi.IsDir() && filepath.Base(path) == "devbox.lock" { - var lockFile File - if err := cuecfg.ParseFile(path, &lockFile); err != nil { + var lockfiles []string + err := filepath.WalkDir( + ".", + func(path string, dirEntry fs.DirEntry, err error) error { + if err != nil { return err } - for key, pkg := range lockFile.Packages { - if latestPkg, exists := latestPackages[key]; exists { - currentTime, err := time.Parse(time.RFC3339, pkg.LastModified) - if err != nil { - return err - } - latestTime, err := time.Parse(time.RFC3339, latestPkg.LastModified) - if err != nil { - return err - } - if currentTime.After(latestTime) { - latestPackages[key] = pkg - } - } else { - latestPackages[key] = pkg + filename := filepath.Base(path) + + if filename == ".gitignore" { + ignoreMatcher, err := ignore.CompileIgnoreFile(path) + if err != nil { + return errors.WithStack(err) } + // push a new filterFunc onto the stack + filterFuncStack = append(filterFuncStack, filterFuncStackEntry{ + ignoreMatcher.MatchesPath, + filepath.Dir(path), + }) } - } - return nil - }) - return latestPackages, err + + if filterFunc(path) { + if dirEntry.IsDir() { + return filepath.SkipDir + } + } else if !dirEntry.IsDir() && filename == "devbox.lock" { + lockfiles = append(lockfiles, path) + } + + return nil + }, + ) + + return lockfiles, err } From f0a3e4331d540cdc68609dbf541f170c27b0ccc2 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Mon, 25 Sep 2023 15:21:47 -0700 Subject: [PATCH 7/8] Remove gitignore code --- go.mod | 1 - go.sum | 2 -- internal/lock/sync.go | 44 +------------------------------------------ 3 files changed, 1 insertion(+), 46 deletions(-) diff --git a/go.mod b/go.mod index 98c840e2948..25ae8062e48 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,6 @@ require ( github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect github.com/pkg/errors v0.9.1 github.com/rogpeppe/go-internal v1.11.0 - github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 github.com/samber/lo v1.38.1 github.com/segmentio/analytics-go v3.1.0+incompatible github.com/spf13/cobra v1.7.0 diff --git a/go.sum b/go.sum index fa6042c0a1d..b41f1f570a4 100644 --- a/go.sum +++ b/go.sum @@ -221,8 +221,6 @@ github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w= github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI= -github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/segmentio/analytics-go v3.1.0+incompatible h1:IyiOfUgQFVHvsykKKbdI7ZsH374uv3/DfZUo9+G0Z80= diff --git a/internal/lock/sync.go b/internal/lock/sync.go index 82ec73cf50b..60d10e6efd8 100644 --- a/internal/lock/sync.go +++ b/internal/lock/sync.go @@ -4,11 +4,8 @@ import ( "fmt" "io/fs" "path/filepath" - "strings" "time" - "github.com/pkg/errors" - ignore "github.com/sabhiram/go-gitignore" "go.jetpack.io/devbox/internal/cuecfg" "go.jetpack.io/devbox/internal/debug" ) @@ -88,27 +85,6 @@ func latestPackages(lockfilePaths []string) (map[string]*Package, error) { func collectLockfiles() ([]string, error) { defer debug.FunctionTimer().End() - type filterFuncStackEntry struct { - filterFunc func(string) bool - path string - } - - var filterFuncStack []filterFuncStackEntry - - var filterFunc = func(path string) bool { - // Start at the top of the stack which has most specific gitignore. - for i := len(filterFuncStack) - 1; i >= 0; i-- { - if strings.HasPrefix(path, filterFuncStack[i].path) { - // Any gitignore that is not a prefix can be popped off the stack - // because WalkDir is depth first which means we're already in a - // different branch - filterFuncStack = filterFuncStack[:i+1] - return filterFuncStack[i].filterFunc(path) - } - } - return false - } - var lockfiles []string err := filepath.WalkDir( ".", @@ -117,25 +93,7 @@ func collectLockfiles() ([]string, error) { return err } - filename := filepath.Base(path) - - if filename == ".gitignore" { - ignoreMatcher, err := ignore.CompileIgnoreFile(path) - if err != nil { - return errors.WithStack(err) - } - // push a new filterFunc onto the stack - filterFuncStack = append(filterFuncStack, filterFuncStackEntry{ - ignoreMatcher.MatchesPath, - filepath.Dir(path), - }) - } - - if filterFunc(path) { - if dirEntry.IsDir() { - return filepath.SkipDir - } - } else if !dirEntry.IsDir() && filename == "devbox.lock" { + if !dirEntry.IsDir() && filepath.Base(path) == "devbox.lock" { lockfiles = append(lockfiles, path) } From c770904d59c297374148e8457107353342591c85 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Mon, 25 Sep 2023 16:35:43 -0700 Subject: [PATCH 8/8] [CICD] Fix nix command --- .github/workflows/cli-tests.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cli-tests.yaml b/.github/workflows/cli-tests.yaml index 431fc6a0e04..b72f6933e27 100644 --- a/.github/workflows/cli-tests.yaml +++ b/.github/workflows/cli-tests.yaml @@ -143,7 +143,7 @@ jobs: cat /etc/nix/nix.conf || true echo "::endgroup::" echo "::group::Resolved Nix config" - nix show-config + nix show-config --extra-experimental-features nix-command echo "::endgroup::" go test -v -timeout $DEVBOX_GOLANG_TEST_TIMEOUT ./... @@ -202,7 +202,7 @@ jobs: cat /etc/nix/nix.conf || true echo "::endgroup::" echo "::group::Resolved Nix config" - nix show-config + nix show-config --extra-experimental-features nix-command echo "::endgroup::" devbox install devbox run -- echo "Hello from devbox!"