Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement outdated command #2497

Merged
merged 2 commits into from
Feb 5, 2025
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: 29 additions & 2 deletions internal/boxcli/list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
// Copyright 2025 Jetify Inc. and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.

package boxcli
Expand All @@ -15,7 +15,8 @@ import (
)

type listCmdFlags struct {
config configFlags
config configFlags
outdated bool
}

func listCmd() *cobra.Command {
Expand All @@ -34,6 +35,10 @@ func listCmd() *cobra.Command {
return errors.WithStack(err)
}

if flags.outdated {
return printOutdatedPackages(cmd, box)
}

for _, pkg := range box.AllPackagesIncludingRemovedTriggerPackages() {
resolvedVersion, err := pkg.ResolvedVersion()
if err != nil {
Expand All @@ -57,6 +62,28 @@ func listCmd() *cobra.Command {
return nil
},
}

cmd.Flags().BoolVar(&flags.outdated, "outdated", false, "List outdated packages")
flags.config.register(cmd)
return cmd
}

// printOutdatedPackages prints a list of outdated packages.
func printOutdatedPackages(cmd *cobra.Command, box *devbox.Devbox) error {
results, err := box.Outdated(cmd.Context())
if err != nil {
return errors.WithStack(err)
}

if len(results) == 0 {
cmd.Println("Your packages are up to date!")
return nil
}

cmd.Println("The following packages can be updated:")
for pkg, version := range results {
cmd.Printf(" * %-30s %s -> %s\n", pkg, version.Current, version.Latest)
}

return nil
}
31 changes: 31 additions & 0 deletions internal/devbox/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,37 @@ const StateOutOfDateMessage = "Your devbox environment may be out of date. Run %
// packages.go has functions for adding, removing and getting info about nix
// packages

type UpdateVersion struct {
Current string
Latest string
}

// Outdated returns a map of package names to their available latest version.
func (d *Devbox) Outdated(ctx context.Context) (map[string]UpdateVersion, error) {
lockfile := d.Lockfile()
outdatedPackages := map[string]UpdateVersion{}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, would prefer to use a slice over map. We don't need random access or deduplication.

Copy link
Contributor

Choose a reason for hiding this comment

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

@gcurtis may not like this but we could just return []lo.Tuple2[*lockfile.Package, *lockfile.Package]

that way we don't have to create a new struct 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.

hm I don't mind the struct. Its nice to have the names

Copy link
Collaborator

Choose a reason for hiding this comment

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

The map is just a convenience for the name. Otherwise you'd have to add a Name field to the struct. That works too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How you prefer guys, here map doesn't create any performance problems, it is just a matter of some seconds to run the command, I chose readability over little optimization


for _, pkg := range d.AllPackages() {
// For non-devbox packages, like flakes or runx, we can skip for now
if !pkg.IsDevboxPackage {
continue
}

lockPackage, err := lockfile.FetchResolvedPackage(pkg.Versioned())
if err != nil {
return nil, errors.Wrap(err, "failed to fetch resolved package")
}
existingLockPackage := lockfile.Packages[pkg.Raw]
if lockPackage.Version == existingLockPackage.Version {
continue
}

outdatedPackages[pkg.Versioned()] = UpdateVersion{Current: existingLockPackage.Version, Latest: lockPackage.Version}
}

return outdatedPackages, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

@guerinoni I think we need this function to be rewritten as so. I created this patch via git diff --patch, so you could try applying it: https://gist.github.com/savil/7a55e959c5eb5c5e240f57dedd5fd7ad

copying here for clarity:

type UpdateVersion struct {
	Current string
	Latest  string
}

// Outdated returns a map of package names to their available latest version.
func (d *Devbox) Outdated(ctx context.Context) (map[string]UpdateVersion, error) {
	ctx, task := trace.NewTask(ctx, "devboxOutdated")
	defer task.End()

	lockfile := d.Lockfile()
	outdatedPackages := map[string]UpdateVersion{}

	for _, pkg := range d.AllPackages() {
		// For non-devbox packages, like flakes or runx, we can skip for now
		if !pkg.IsDevboxPackage {
			continue
		}

		lockPackage, err := lockfile.FetchResolvedPackage(pkg.Versioned())
		if err != nil {
			return nil, errors.Wrap(err, "failed to fetch resolved package")
		}
		existingLockPackage := lockfile.Packages[pkg.Raw]
		if lockPackage.Version == existingLockPackage.Version {
			continue
		}

		outdatedPackages[pkg.Versioned()] = UpdateVersion{Current: existingLockPackage.Version, Latest: lockPackage.Version}
	}

	return outdatedPackages, nil
}

wdyt @gcurtis @mikeland73 ?

The benefit of this is that it also works for @latest. For example, when I run this on the devbox repo itself, I see:

$ devbox run build
$  dist/devbox ls --outdated
The following packages can be updated:
 * go@latest                      1.23.0 -> 1.23.4
 * runx:golangci/golangci-lint@latest v1.60.2 -> v1.63.4

I don't think the runx one should be getting printed, but its cause we return true for IsDevboxPackage for runx packages!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I aligned with this patch :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not runx? The versions are correct. Ideally we could also do it for flakes as well but our current flake update mechanism is a bit broken.

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh good point, I forgot runx updating works properly. So all good

}

// Add adds the `pkgs` to the config (i.e. devbox.json) and nix profile for this
// devbox project
func (d *Devbox) Add(ctx context.Context, pkgsNames []string, opts devopt.AddOpts) error {
Expand Down
Loading