Skip to content

Commit 5cc29c4

Browse files
committed
When saving a command, only include a few selected environment variables
1 parent 76271ef commit 5cc29c4

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Diff for: v2/lastcmd.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ func saveCommand(cmd *exec.Cmd) error {
5757
// Strip the leading /usr/bin/sh -c command, if present
5858
commandString := getCommand(cmd)
5959

60-
// Add environment variables
60+
// Add environment variables, but only some
61+
selectedEnvVars := []string{"NO_COLOR", "PYTHON"}
6162
for _, assignment := range cmd.Env {
62-
commandString = assignment + " " + commandString
63+
if strings.Contains(assignment, "=") && containsSubstring(assignment, selectedEnvVars) {
64+
commandString = strings.Replace(assignment, "=", "=\"", 1) + "\" \\\n" + commandString
65+
}
6366
}
6467

6568
// Write the contents, ignore the number of written bytes

Diff for: v2/strings.go

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ func hasWords(s string) bool {
2929
return false
3030
}
3131

32+
// containsSubstring checks if the given string contains one of the given substrings
33+
func containsSubstring(haystack string, substrings []string) bool {
34+
for _, ss := range substrings {
35+
if strings.Contains(haystack, ss) {
36+
return true
37+
}
38+
}
39+
return false
40+
}
41+
3242
// allUpper checks if all letters in a string are uppercase
3343
func allUpper(s string) bool {
3444
for _, r := range s {

0 commit comments

Comments
 (0)