Skip to content

Commit 751628e

Browse files
committed
Add more shells and customize hooks for devbox
1 parent 18fbefd commit 751628e

File tree

8 files changed

+119
-66
lines changed

8 files changed

+119
-66
lines changed
File renamed without changes.

internal/shell/shell_bash.go renamed to internal/shenv/shell_bash.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package shell
1+
package shenv
22

33
import "fmt"
44

@@ -8,15 +8,15 @@ type bash struct{}
88
var Bash Shell = bash{}
99

1010
const bashHook = `
11-
_direnv_hook() {
11+
_devbox_hook() {
1212
local previous_exit_status=$?;
1313
trap -- '' SIGINT;
14-
eval "$("{{.SelfPath}}" export bash)";
14+
eval "$(devbox shellenv --config {{ .ProjectDir }})";
1515
trap - SIGINT;
1616
return $previous_exit_status;
1717
};
18-
if ! [[ "${PROMPT_COMMAND:-}" =~ _direnv_hook ]]; then
19-
PROMPT_COMMAND="_direnv_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
18+
if ! [[ "${PROMPT_COMMAND:-}" =~ _devbox_hook ]]; then
19+
PROMPT_COMMAND="_devbox_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
2020
fi
2121
`
2222

@@ -94,9 +94,13 @@ func BashEscape(str string) string {
9494
if str == "" {
9595
return "''"
9696
}
97+
// var too short
98+
//nolint:varnamelen
9799
in := []byte(str)
98100
out := ""
99101
i := 0
102+
// var too short
103+
//nolint:varnamelen
100104
l := len(in)
101105
escape := false
102106

internal/shell/shell_fish.go renamed to internal/shenv/shell_fish.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package shell
1+
package shenv
22

33
import (
44
"fmt"
@@ -11,29 +11,9 @@ type fish struct{}
1111
var Fish Shell = fish{}
1212

1313
const fishHook = `
14-
function __direnv_export_eval --on-event fish_prompt;
15-
"{{.SelfPath}}" export fish | source;
16-
17-
if test "$direnv_fish_mode" != "disable_arrow";
18-
function __direnv_cd_hook --on-variable PWD;
19-
if test "$direnv_fish_mode" = "eval_after_arrow";
20-
set -g __direnv_export_again 0;
21-
else;
22-
"{{.SelfPath}}" export fish | source;
23-
end;
24-
end;
25-
end;
26-
end;
27-
28-
function __direnv_export_eval_2 --on-event fish_preexec;
29-
if set -q __direnv_export_again;
30-
set -e __direnv_export_again;
31-
"{{.SelfPath}}" export fish | source;
32-
echo;
33-
end;
34-
35-
functions --erase __direnv_cd_hook;
36-
end;
14+
function __devbox_shellenv_eval --on-event fish_prompt;
15+
devbox shellenv --config {{ .ProjectDir }} | source;
16+
end;
3717
`
3818

3919
func (sh fish) Hook() (string, error) {
@@ -74,9 +54,13 @@ func (sh fish) unset(key string) string {
7454
}
7555

7656
func (sh fish) escape(str string) string {
57+
// var too short
58+
//nolint:varnamelen
7759
in := []byte(str)
7860
out := "'"
7961
i := 0
62+
// var too short
63+
//nolint:varnamelen
8064
l := len(in)
8165

8266
hex := func(char byte) {

internal/shenv/shell_ksh.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package shenv
2+
3+
type ksh struct{}
4+
5+
// Ksh adds support the korn shell
6+
var Ksh Shell = ksh{}
7+
8+
// um, this is ChatGPT writing it. I need to verify and test
9+
const kshHook = `
10+
_devbox_hook() {
11+
eval "$(devbox shellenv --config {{ .ProjectDir }})";
12+
}
13+
if [[ "$(typeset -f precmd)" != *"_devbox_hook"* ]]; then
14+
function precmd {
15+
devbox_hook
16+
}
17+
fi
18+
`
19+
20+
func (sh ksh) Hook() (string, error) {
21+
return kshHook, nil
22+
}
23+
24+
func (sh ksh) Export(e ShellExport) (out string) {
25+
panic("not implemented")
26+
}
27+
28+
func (sh ksh) Dump(env Env) (out string) {
29+
panic("not implemented")
30+
}

internal/shenv/shell_posix.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package shenv
2+
3+
type posix struct{}
4+
5+
// Posix adds support for posix-compatible shells
6+
// Specifically, in the context of devbox, this includes
7+
// `dash`, `ash`, and `shell`
8+
var Posix Shell = posix{}
9+
10+
// um, this is ChatGPT writing it. I need to verify and test
11+
const posixHook = `
12+
_devbox_hook() {
13+
local previous_exit_status=$?
14+
trap : INT
15+
eval "$(devbox shellenv --config {{ .ProjectDir }})"
16+
trap - INT
17+
return $previous_exit_status
18+
}
19+
if [ -z "$PROMPT_COMMAND" ] || ! printf "%s" "$PROMPT_COMMAND" | grep -q "_devbox_hook"; then
20+
PROMPT_COMMAND="_devbox_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
21+
fi
22+
`
23+
24+
func (sh posix) Hook() (string, error) {
25+
return posixHook, nil
26+
}
27+
28+
func (sh posix) Export(e ShellExport) (out string) {
29+
panic("not implemented")
30+
}
31+
32+
func (sh posix) Dump(env Env) (out string) {
33+
panic("not implemented")
34+
}

internal/shenv/shell_unknown.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package shenv
2+
3+
type unknown struct{}
4+
5+
// UnknownSh adds support the unknown shell. This serves
6+
// as a fallback alternative to outright failure.
7+
var UnknownSh Shell = unknown{}
8+
9+
const unknownHook = `
10+
echo "Warning: this shell will not update its environment.
11+
Please exit and re-enter shell after making any changes that may affect the devbox generated environment.\n"
12+
`
13+
14+
func (sh unknown) Hook() (string, error) {
15+
return unknownHook, nil
16+
}
17+
18+
func (sh unknown) Export(e ShellExport) (out string) {
19+
panic("not implemented")
20+
}
21+
22+
func (sh unknown) Dump(env Env) (out string) {
23+
panic("not implemented")
24+
}

internal/shell/shell_zsh.go renamed to internal/shenv/shell_zsh.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package shell
1+
package shenv
22

33
// ZSH is a singleton instance of ZSH_T
44
type zsh struct{}
@@ -7,18 +7,14 @@ type zsh struct{}
77
var Zsh Shell = zsh{}
88

99
const zshHook = `
10-
_direnv_hook() {
10+
_devbox_hook() {
1111
trap -- '' SIGINT;
12-
eval "$("{{.SelfPath}}" export zsh)";
12+
eval "$(devbox shellenv --config {{ .ProjectDir }})";
1313
trap - SIGINT;
1414
}
1515
typeset -ag precmd_functions;
16-
if [[ -z "${precmd_functions[(r)_direnv_hook]+1}" ]]; then
17-
precmd_functions=( _direnv_hook ${precmd_functions[@]} )
18-
fi
19-
typeset -ag chpwd_functions;
20-
if [[ -z "${chpwd_functions[(r)_direnv_hook]+1}" ]]; then
21-
chpwd_functions=( _direnv_hook ${chpwd_functions[@]} )
16+
if [[ -z "${precmd_functions[(r)_devbox_hook]+1}" ]]; then
17+
precmd_functions=( _devbox_hook ${precmd_functions[@]} )
2218
fi
2319
`
2420

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
package shell
2-
3-
import (
4-
"path/filepath"
5-
)
1+
package shenv
62

73
type Env map[string]string
84

@@ -33,36 +29,21 @@ func (e ShellExport) Remove(key string) {
3329
e[key] = nil
3430
}
3531

36-
// DetectShell returns a Shell instance from the given target.
37-
//
38-
// target is usually $0 and can also be prefixed by `-`
32+
// DetectShell returns a Shell instance from the given shell name
33+
// TODO: use a single common "enum" for both shenv and DevboxShell
3934
func DetectShell(target string) Shell {
40-
target = filepath.Base(target)
41-
// $0 starts with "-"
42-
if target[0:1] == "-" {
43-
target = target[1:]
44-
}
45-
4635
switch target {
4736
case "bash":
4837
return Bash
49-
//case "elvish":
50-
// return Elvish
5138
case "fish":
5239
return Fish
53-
//case "gha":
54-
// return GitHubActions
55-
//case "gzenv":
56-
// return GzEnv
57-
//case "json":
58-
// return JSON
59-
//case "tcsh":
60-
// return Tcsh
61-
//case "vim":
62-
// return Vim
40+
case "ksh":
41+
return Ksh
42+
case "posix":
43+
return Posix
6344
case "zsh":
6445
return Zsh
46+
default:
47+
return UnknownSh
6548
}
66-
67-
return nil
6849
}

0 commit comments

Comments
 (0)