Skip to content

Commit bd159a8

Browse files
authored
[add] Show better message if package is missing. Specify exact version after installing (#370)
## Summary Minor UX improvements: * Using devbox add without specifying a package shows a helpful message. * After installing a package we show the exact version that was installed. ## How was it tested? ```sh ➜ devbox add Usage: devbox add <pkg>... [flags] To search for packages use https://search.nixos.org/packages ``` ```sh ➜ devbox add curl Installing nix packages. This may take a while... done. curl (curl-7.86.0) is now installed. ```
1 parent 92a519a commit bd159a8

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

internal/boxcli/add.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package boxcli
55

66
import (
7+
"fmt"
78
"os"
89

910
"github.com/pkg/errors"
@@ -22,9 +23,16 @@ func AddCmd() *cobra.Command {
2223
command := &cobra.Command{
2324
Use: "add <pkg>...",
2425
Short: "Add a new package to your devbox",
25-
Args: cobra.MinimumNArgs(1),
2626
PersistentPreRunE: nix.EnsureInstalled,
2727
RunE: func(cmd *cobra.Command, args []string) error {
28+
if len(args) == 0 {
29+
fmt.Fprintf(
30+
cmd.OutOrStdout(),
31+
"Usage: %s\n\nTo search for packages use https://search.nixos.org/packages\n",
32+
cmd.UseLine(),
33+
)
34+
return nil
35+
}
2836
return addCmdFunc(cmd, args, flags)
2937
},
3038
}

internal/impl/devbox.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -528,30 +528,49 @@ func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
528528
return nil
529529
}
530530

531-
func (d *Devbox) printPackageUpdateMessage(mode installMode, pkgs []string) error {
532-
installedVerb := "installed"
531+
func (d *Devbox) printPackageUpdateMessage(
532+
mode installMode,
533+
pkgs []string,
534+
) error {
535+
verb := "installed"
536+
var infos []*nix.Info
537+
for _, pkg := range pkgs {
538+
info, _ := nix.PkgInfo(d.cfg.Nixpkgs.Commit, pkg)
539+
infos = append(infos, info)
540+
}
533541
if mode == uninstall {
534-
installedVerb = "removed"
542+
verb = "removed"
535543
}
536544

537545
if len(pkgs) > 0 {
538546

539-
successMsg := fmt.Sprintf("%s is now %s.", pkgs[0], installedVerb)
547+
successMsg := fmt.Sprintf("%s (%s) is now %s.", pkgs[0], infos[0], verb)
540548
if len(pkgs) > 1 {
541-
successMsg = fmt.Sprintf("%s are now %s.", strings.Join(pkgs, ", "), installedVerb)
549+
pkgsWithVersion := []string{}
550+
for idx, pkg := range pkgs {
551+
pkgsWithVersion = append(
552+
pkgsWithVersion,
553+
fmt.Sprintf("%s (%s)", pkg, infos[idx]),
554+
)
555+
}
556+
successMsg = fmt.Sprintf(
557+
"%s are now %s.",
558+
strings.Join(pkgsWithVersion, ", "),
559+
verb,
560+
)
542561
}
543562
fmt.Fprint(d.writer, successMsg)
544563

545-
// (Only when in devbox shell) Prompt the user to run `hash -r` to ensure their
546-
// shell can access the most recently installed binaries, or ensure their
547-
// recently uninstalled binaries are not accidentally still available.
564+
// (Only when in devbox shell) Prompt the user to run `hash -r` to ensure
565+
// their shell can access the most recently installed binaries, or ensure
566+
// their recently uninstalled binaries are not accidentally still available.
548567
if !IsDevboxShellEnabled() {
549568
fmt.Fprintln(d.writer)
550569
} else {
551570
fmt.Fprintln(d.writer, " Run `hash -r` to ensure your shell is updated.")
552571
}
553572
} else {
554-
fmt.Fprintf(d.writer, "No packages %s.\n", installedVerb)
573+
fmt.Fprintf(d.writer, "No packages %s.\n", verb)
555574
}
556575
return nil
557576
}

0 commit comments

Comments
 (0)