Skip to content

Commit caa944e

Browse files
authored
[UX] Improve post-install readme, improve message if package not found (#375)
## Summary UX improvements: * If user adds a package without a readme, don't show any readme. (previously we would show: "To show this information, run `devbox info %s") * If user tries to add non-existent package show better message. ## How was it tested? ```sh devbox add curl Installing nix packages. This may take a while... done. curl (curl-7.86.0) is now installed. ``` ```sh devbox add foobar Error: foobar: package not found To search for packages use https://search.nixos.org/packages ```
1 parent c2fb516 commit caa944e

File tree

7 files changed

+33
-9
lines changed

7 files changed

+33
-9
lines changed

internal/boxcli/add.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import (
1010
"github.com/pkg/errors"
1111
"github.com/spf13/cobra"
1212
"go.jetpack.io/devbox"
13+
"go.jetpack.io/devbox/internal/boxcli/usererr"
1314
"go.jetpack.io/devbox/internal/nix"
1415
)
1516

17+
const toSearchForPackages = "To search for packages use https://search.nixos.org/packages"
18+
1619
type addCmdFlags struct {
1720
config configFlags
1821
}
@@ -28,12 +31,17 @@ func AddCmd() *cobra.Command {
2831
if len(args) == 0 {
2932
fmt.Fprintf(
3033
cmd.OutOrStdout(),
31-
"Usage: %s\n\nTo search for packages use https://search.nixos.org/packages\n",
34+
"Usage: %s\n\n%s\n",
3235
cmd.UseLine(),
36+
toSearchForPackages,
3337
)
3438
return nil
3539
}
36-
return addCmdFunc(cmd, args, flags)
40+
err := addCmdFunc(cmd, args, flags)
41+
if errors.Is(err, nix.ErrPackageNotFound) {
42+
return usererr.New("%s\n\n%s", err, toSearchForPackages)
43+
}
44+
return err
3745
},
3846
}
3947

internal/impl/devbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (d *Devbox) Add(pkgs ...string) error {
9595
for _, pkg := range pkgs {
9696
ok := nix.PkgExists(d.cfg.Nixpkgs.Commit, pkg)
9797
if !ok {
98-
return errors.Errorf("package %s not found", pkg)
98+
return errors.WithMessage(nix.ErrPackageNotFound, pkg)
9999
}
100100
}
101101

internal/nix/nix.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
// Instead of using directory, prefer using the devbox.ProfilePath() function that ensures the directory exists.
1919
const ProfilePath = ".devbox/nix/profile/default"
2020

21+
var ErrPackageNotFound = errors.New("package not found")
22+
2123
func PkgExists(nixpkgsCommit, pkg string) bool {
2224
_, found := PkgInfo(nixpkgsCommit, pkg)
2325
return found

internal/pkgcfg/files.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const pkgCfgDir = "package-configuration"
1414
//go:embed package-configuration/*
1515
var packageConfiguration embed.FS
1616

17-
func getConfig(pkg, rootDir string) (*config, error) {
17+
func getConfigIfAny(pkg, rootDir string) (*config, error) {
1818
configFiles, err := packageConfiguration.ReadDir(pkgCfgDir)
1919
if err != nil {
2020
return nil, errors.WithStack(err)
@@ -43,7 +43,7 @@ func getConfig(pkg, rootDir string) (*config, error) {
4343
}
4444
return cfg, nil
4545
}
46-
return &config{}, nil
46+
return nil, nil
4747
}
4848

4949
func getFileContent(contentPath string) ([]byte, error) {

internal/pkgcfg/info.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ func PrintReadme(
1313
w io.Writer,
1414
showSourceEnv, markdown bool,
1515
) error {
16-
cfg, err := getConfig(pkg, rootDir)
16+
cfg, err := getConfigIfAny(pkg, rootDir)
1717

1818
if err != nil {
1919
return err
2020
}
2121

22+
if cfg == nil {
23+
return nil
24+
}
25+
2226
_, _ = fmt.Fprintln(w, "")
2327

2428
if err = printReadme(cfg, w, markdown); err != nil {

internal/pkgcfg/pkgcfg.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ type config struct {
3333
}
3434

3535
func (m *Manager) CreateFilesAndShowReadme(pkg, rootDir string) error {
36-
cfg, err := getConfig(pkg, rootDir)
36+
cfg, err := getConfigIfAny(pkg, rootDir)
3737
if err != nil {
3838
return err
3939
}
40+
if cfg == nil {
41+
return nil
42+
}
43+
4044
debug.Log("Creating files for package %q create files", pkg)
4145
for filePath, contentPath := range cfg.CreateFiles {
4246

@@ -95,10 +99,13 @@ func (m *Manager) CreateFilesAndShowReadme(pkg, rootDir string) error {
9599
func Env(pkgs []string, rootDir string) (map[string]string, error) {
96100
env := map[string]string{}
97101
for _, pkg := range pkgs {
98-
cfg, err := getConfig(pkg, rootDir)
102+
cfg, err := getConfigIfAny(pkg, rootDir)
99103
if err != nil {
100104
return nil, err
101105
}
106+
if cfg == nil {
107+
continue
108+
}
102109
for k, v := range cfg.Env {
103110
env[k] = v
104111
}

internal/pkgcfg/services.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ type service struct {
2020
func GetServices(pkgs []string, rootDir string) (Services, error) {
2121
services := map[string]service{}
2222
for _, pkg := range pkgs {
23-
c, err := getConfig(pkg, rootDir)
23+
c, err := getConfigIfAny(pkg, rootDir)
2424
if err != nil {
2525
return nil, err
2626
}
27+
if c == nil {
28+
continue
29+
}
2730
for name, svc := range c.Services {
2831
svc.Name = name
2932
services[name] = svc

0 commit comments

Comments
 (0)