forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_zsh.go
43 lines (35 loc) · 789 Bytes
/
shell_zsh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
// ZSH is a singleton instance of ZSH_T
type zsh int
var ZSH zsh
const ZSH_HOOK = `
_direnv_hook() {
eval "$(direnv export zsh)";
}
typeset -ag precmd_functions;
if [[ -z ${precmd_functions[(r)_direnv_hook]} ]]; then
precmd_functions+=_direnv_hook;
fi
`
func (z zsh) Hook() (string, error) {
return ZSH_HOOK, nil
}
func (z zsh) Export(e ShellExport) (out string) {
for key, value := range e {
if value == nil {
out += z.unset(key)
} else {
out += z.export(key, *value)
}
}
return out
}
func (z zsh) export(key, value string) string {
return "export " + z.escape(key) + "=" + z.escape(value) + ";"
}
func (z zsh) unset(key string) string {
return "unset " + z.escape(key) + ";"
}
func (z zsh) escape(str string) string {
return BashEscape(str)
}