Skip to content

Commit 15dff68

Browse files
authored
[plugins] Join hash -r and source message and show in yellow (#452)
## Summary This improves the message we show when a package that has env vars is installed. It shows it in yellow at the very end always. It also joins the `hash -r` command so that it's a single command. This is a stopgap until we have a better way to use update env variables while in shell. Once that is done, some of this change can be reverted. (we might also be able to remove the need for `hash -r` command by running it directly from devbox and/or only showing it when it actually needs to be run) I also considered creating a global env file that sources all the env files but this would cause potential overwriting I didn't want. ## How was it tested? <img width="595" alt="image" src="https://user-images.githubusercontent.com/544948/211695932-63e628e4-3cb9-42a2-aab3-aed933930597.png">
1 parent 3c3e71b commit 15dff68

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

internal/impl/devbox.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ func (d *Devbox) Add(pkgs ...string) error {
164164
pkg,
165165
d.projectDir,
166166
d.writer,
167-
IsDevboxShellEnabled(),
168167
false, /*markdown*/
169168
); err != nil {
170169
return err
@@ -423,7 +422,6 @@ func (d *Devbox) Info(pkg string, markdown bool) error {
423422
pkg,
424423
d.projectDir,
425424
d.writer,
426-
false, /*showSourceEnv*/
427425
markdown,
428426
)
429427
}
@@ -605,7 +603,7 @@ func (d *Devbox) printPackageUpdateMessage(
605603

606604
if len(pkgs) > 0 {
607605

608-
successMsg := fmt.Sprintf("%s (%s) is now %s.", pkgs[0], infos[0], verb)
606+
successMsg := fmt.Sprintf("%s (%s) is now %s.\n", pkgs[0], infos[0], verb)
609607
if len(pkgs) > 1 {
610608
pkgsWithVersion := []string{}
611609
for idx, pkg := range pkgs {
@@ -622,13 +620,18 @@ func (d *Devbox) printPackageUpdateMessage(
622620
}
623621
fmt.Fprint(d.writer, successMsg)
624622

625-
// (Only when in devbox shell) Prompt the user to run `hash -r` to ensure
626-
// their shell can access the most recently installed binaries, or ensure
627-
// their recently uninstalled binaries are not accidentally still available.
628-
if !IsDevboxShellEnabled() {
629-
fmt.Fprintln(d.writer)
630-
} else {
631-
fmt.Fprintln(d.writer, " Run `hash -r` to ensure your shell is updated.")
623+
fmt.Fprintln(d.writer)
624+
625+
// (Only when in devbox shell) Prompt the user to run hash -r
626+
// to ensure we refresh the shell hash and load the proper environment.
627+
if IsDevboxShellEnabled() {
628+
if err := plugin.PrintEnvUpdateMessage(
629+
lo.Ternary(mode == install, pkgs, []string{}),
630+
d.projectDir,
631+
d.writer,
632+
); err != nil {
633+
return err
634+
}
632635
}
633636
} else {
634637
fmt.Fprintf(d.writer, "No packages %s.\n", verb)

internal/plugin/info.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ package plugin
33
import (
44
"fmt"
55
"io"
6+
"os"
7+
"path/filepath"
8+
"strings"
69

10+
"github.com/fatih/color"
711
"github.com/pkg/errors"
812
"github.com/samber/lo"
913
)
1014

1115
func PrintReadme(
1216
pkg, projectDir string,
1317
w io.Writer,
14-
showSourceEnv, markdown bool,
18+
markdown bool,
1519
) error {
1620
cfg, err := getConfigIfAny(pkg, projectDir)
1721

@@ -45,9 +49,6 @@ func PrintReadme(
4549
return err
4650
}
4751

48-
if showSourceEnv {
49-
err = printSourceEnvMessage(pkg, projectDir, w)
50-
}
5152
return err
5253
}
5354

@@ -132,18 +133,26 @@ func printInfoInstructions(pkg string, w io.Writer) error {
132133
return errors.WithStack(err)
133134
}
134135

135-
func printSourceEnvMessage(pkg, projectDir string, w io.Writer) error {
136-
env, err := Env([]string{pkg}, projectDir)
137-
if err != nil {
138-
return err
136+
func PrintEnvUpdateMessage(pkgs []string, projectDir string, w io.Writer) error {
137+
commands := []string{"hash -r"}
138+
for _, pkg := range pkgs {
139+
if path := getEnvFilePathIfExist(pkg, projectDir); path != "" {
140+
wd, err := os.Getwd()
141+
if err != nil {
142+
return errors.WithStack(err)
143+
}
144+
relPath, err := filepath.Rel(wd, path)
145+
if err != nil {
146+
return errors.WithStack(err)
147+
}
148+
commands = append(commands, fmt.Sprintf("source %s", relPath))
149+
}
139150
}
140-
if len(env) > 0 {
141-
_, err = fmt.Fprintf(
151+
color.New(color.FgYellow).
152+
Fprintf(
142153
w,
143-
"To ensure environment is set, run `source %s/%s/env`\n\n",
144-
VirtenvPath,
145-
pkg,
154+
"Run `%s` to ensure your shell is updated.\n\n",
155+
strings.Join(commands, " && "),
146156
)
147-
}
148-
return errors.WithStack(err)
157+
return nil
149158
}

internal/plugin/pkgcfg.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ func Env(pkgs []string, projectDir string) (map[string]string, error) {
120120
return env, nil
121121
}
122122

123+
func getEnvFilePathIfExist(pkg, projectDir string) string {
124+
filePath := filepath.Join(projectDir, VirtenvPath, pkg, "/env")
125+
if _, err := os.Stat(filePath); err != nil {
126+
return ""
127+
}
128+
return filePath
129+
}
130+
123131
func createEnvFile(pkg, projectDir string) error {
124132
envVars, err := Env([]string{pkg}, projectDir)
125133
if err != nil {

0 commit comments

Comments
 (0)