-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shellenv] Remove nix profile from path, add shellenv command (#667)
## Summary This removes nix profile from path when using unified environment. It adds a new shellenv command and prints instructions when package is added inside devbox shell. @gcurtis I chose `eval $(devbox shellenv)` over `. <(devbox shellenv)` to be consistent with the global instructions and also because it's easy to miss the period when we print it. Not sure if there's a difference. Also is this fish compatible or do we need different instructions for fish? Lastly, you'll notice `hash -r` is not there anymore. In my experiments running `eval $(devbox shellenv)` causes the hash table to clear. I believe this happens because `PATH` has changed, but I'm not 100% sure. ## How was it tested? <img width="762" alt="image" src="https://user-images.githubusercontent.com/544948/220529645-42a6578f-e733-4943-9478-bb415a904a40.png"> ```bash ➜ devbox shell ➜ devbox add php82 php82Extensions.memcached ➜ which php php not found ➜ eval $(devbox shellenv) ➜ which php /nix/store/5yylkc71wmlzii2vl40021vgbfwqmajb-php-with-extensions-8.2.1/bin/php ➜ php -m | grep memcached memcached ```
- Loading branch information
1 parent
0fdee09
commit 0eda044
Showing
13 changed files
with
114 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. | ||
// Use of this source code is governed by the license in the LICENSE file. | ||
|
||
package boxcli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"go.jetpack.io/devbox" | ||
) | ||
|
||
type shellEnvCmdFlags struct { | ||
config configFlags | ||
} | ||
|
||
func shellEnvCmd() *cobra.Command { | ||
flags := shellEnvCmdFlags{} | ||
command := &cobra.Command{ | ||
Use: "shellenv", | ||
Short: "Print shell commands that add Devbox packages to your PATH", | ||
Args: cobra.ExactArgs(0), | ||
PreRunE: ensureNixInstalled, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
s, err := shellEnvFunc(cmd, flags) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintln(cmd.OutOrStdout(), s) | ||
return nil | ||
}, | ||
} | ||
|
||
flags.config.register(command) | ||
return command | ||
} | ||
|
||
func shellEnvFunc(cmd *cobra.Command, flags shellEnvCmdFlags) (string, error) { | ||
box, err := devbox.Open(flags.config.path, cmd.ErrOrStderr()) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return box.PrintEnv(true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
exec devbox init | ||
|
||
# test adding and running hello | ||
exec devbox add hello | ||
! exec hello | ||
! stdout . | ||
|
||
# source shellenv and test again | ||
exec devbox shellenv | ||
source.path | ||
exec hello | ||
stdout 'Hello, world!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package testrunner | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/rogpeppe/go-internal/testscript" | ||
) | ||
|
||
// Sources whatever path is exported in stdout. Ignored everything else | ||
// Usage: | ||
// exec devbox shellenv | ||
// source.path | ||
func sourcePath(script *testscript.TestScript, neg bool, args []string) { | ||
if len(args) != 0 { | ||
script.Fatalf("usage: source.path") | ||
} | ||
if neg { | ||
script.Fatalf("source.path does not support negation") | ||
} | ||
sourcedScript := script.ReadFile("stdout") | ||
for _, line := range strings.Split(sourcedScript, "\n") { | ||
if strings.HasPrefix(line, "export PATH=") { | ||
path := strings.TrimPrefix(line, "export PATH=") | ||
path = strings.Trim(path, "\"") | ||
script.Setenv("PATH", path) | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters