Skip to content

Commit

Permalink
feat: implement outdated command (#2497)
Browse files Browse the repository at this point in the history
## Summary
This allow to present all new versions available for the pkg installed.
It is a lot useful because otherwise you need to check this manually.

## How was it tested?

In a different repository, this is the output.

<img width="383" alt="Screenshot 2025-01-31 at 17 54 25"
src="https://github.com/user-attachments/assets/6041e24a-8c1c-4d74-8213-99738959cc17"
/>

---------

Co-authored-by: John Lago <[email protected]>
  • Loading branch information
guerinoni and Lagoja authored Feb 5, 2025
1 parent 3f7706d commit ff381aa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
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{}

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
}

// 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

0 comments on commit ff381aa

Please sign in to comment.