forked from badgerodon/penv
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fish_dao.go
77 lines (71 loc) · 1.37 KB
/
fish_dao.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package penv
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/mitchellh/go-ps"
)
var (
fishShell = &shell{
configFileName: filepath.Join(os.Getenv("HOME"), ".config", "fish", "config.fish"),
commentSigil: " #",
quote: func(value string) string {
r := strings.NewReplacer(
"\\", "\\\\",
"'", "\\'",
"\n", `'"\n"'`,
"\r", `'"\r"'`,
)
return "'" + r.Replace(value) + "'"
},
mkSet: func(sh *shell, nv NameValue) string {
return fmt.Sprintf(
"set -Ux %s %s",
nv.Name, sh.quote(nv.Value),
)
},
mkAppend: func(sh *shell, nv NameValue) string {
return fmt.Sprintf(
"set -Ux %s $%s %s",
nv.Name, nv.Name, sh.quote(nv.Value),
)
},
mkUnset: func(sh *shell, nv NameValue) string {
return fmt.Sprintf(
"set -Ue %s",
nv.Name,
)
},
}
)
type fishReloader struct{ DAO }
func (fw fishReloader) Save(env *Environment) error {
err := fw.DAO.Save(env)
if err != nil {
return err
}
return exec.Command("fish", "-c", "echo hi").Run()
}
func init() {
RegisterDAO(1000, func() bool {
pid := os.Getpid()
for pid > 0 {
p, err := ps.FindProcess(pid)
if err != nil || p == nil {
break
}
switch p.Executable() {
case "fish":
return true
case "bash":
return false
case "zsh":
return false
}
pid = p.PPid()
}
return false
}, fishReloader{fishShell})
}