Skip to content

Commit 9672a6d

Browse files
committed
feat: implement outdated command
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.
1 parent 15e2fb1 commit 9672a6d

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

internal/boxcli/outdated.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package boxcli
2+
3+
import (
4+
"github.com/pkg/errors"
5+
"github.com/spf13/cobra"
6+
"go.jetpack.io/devbox/internal/devbox"
7+
"go.jetpack.io/devbox/internal/devbox/devopt"
8+
)
9+
10+
func outdatedCmd() *cobra.Command {
11+
flags := listCmdFlags{}
12+
command := &cobra.Command{
13+
Use: "outdated",
14+
Short: "Show all outdated packages",
15+
Args: cobra.MaximumNArgs(0),
16+
PreRunE: ensureNixInstalled,
17+
RunE: func(cmd *cobra.Command, _ []string) error {
18+
box, err := devbox.Open(&devopt.Opts{
19+
Dir: flags.config.path,
20+
Stderr: cmd.ErrOrStderr(),
21+
})
22+
if err != nil {
23+
return errors.WithStack(err)
24+
}
25+
26+
resutls, err := box.Outdated(cmd.Context())
27+
if err != nil {
28+
return errors.WithStack(err)
29+
}
30+
31+
if len(resutls) == 0 {
32+
cmd.Println("Your packages are up to date!")
33+
return nil
34+
}
35+
36+
cmd.Println("The following packages can be updated:")
37+
for pkg, version := range resutls {
38+
cmd.Printf(" * %-30s %s -> %s\n", pkg, version.Current, version.Latest)
39+
}
40+
return nil
41+
},
42+
}
43+
44+
return command
45+
}

internal/boxcli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func RootCmd() *cobra.Command {
6666
command.AddCommand(infoCmd())
6767
command.AddCommand(initCmd())
6868
command.AddCommand(installCmd())
69+
command.AddCommand(outdatedCmd())
6970
command.AddCommand(integrateCmd())
7071
command.AddCommand(listCmd())
7172
command.AddCommand(logCmd())

internal/devbox/packages.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"go.jetpack.io/devbox/internal/devpkg"
2626
"go.jetpack.io/devbox/internal/devpkg/pkgtype"
2727
"go.jetpack.io/devbox/internal/lock"
28+
"go.jetpack.io/devbox/internal/searcher"
2829
"go.jetpack.io/devbox/internal/setup"
2930
"go.jetpack.io/devbox/internal/shellgen"
3031
"go.jetpack.io/devbox/internal/telemetry"
@@ -43,6 +44,48 @@ const StateOutOfDateMessage = "Your devbox environment may be out of date. Run %
4344
// packages.go has functions for adding, removing and getting info about nix
4445
// packages
4546

47+
type UpdateVersion struct {
48+
Current string
49+
Latest string
50+
}
51+
52+
// Outdated returns a map of package names to their available latest version.
53+
func (d *Devbox) Outdated(ctx context.Context) (map[string]UpdateVersion, error) {
54+
ctx, task := trace.NewTask(ctx, "devboxOutdated")
55+
defer task.End()
56+
57+
outdatedPackages := map[string]UpdateVersion{}
58+
59+
for _, pkg := range d.AllPackages() {
60+
if strings.HasSuffix(pkg.Versioned(), "latest") {
61+
continue
62+
}
63+
64+
result, err := searcher.Client().Search(ctx, pkg.CanonicalName())
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
for _, p := range result.Packages {
70+
if p.Name == pkg.CanonicalName() {
71+
for _, v := range p.Versions {
72+
vv, err := pkg.ResolvedVersion()
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
if v.Version > vv {
78+
outdatedPackages[p.Name] = UpdateVersion{Current: vv, Latest: v.Version}
79+
break
80+
}
81+
}
82+
}
83+
}
84+
}
85+
86+
return outdatedPackages, nil
87+
}
88+
4689
// Add adds the `pkgs` to the config (i.e. devbox.json) and nix profile for this
4790
// devbox project
4891
func (d *Devbox) Add(ctx context.Context, pkgsNames []string, opts devopt.AddOpts) error {

0 commit comments

Comments
 (0)