-
Notifications
You must be signed in to change notification settings - Fork 16
/
bash_dao.go
79 lines (73 loc) · 1.52 KB
/
bash_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
78
79
package penv
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/mitchellh/go-ps"
)
var (
bashShell = &shell{
configFileName: filepath.Join(os.Getenv("HOME"), ".bashrc"),
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(
"export %s=%s",
nv.Name, sh.quote(nv.Value),
)
},
mkAppend: func(sh *shell, nv NameValue) string {
return fmt.Sprintf(
"export %s=${%s}${%s:+:}%s",
nv.Name, nv.Name, nv.Name, sh.quote(nv.Value),
)
},
mkUnset: func(sh *shell, nv NameValue) string {
return fmt.Sprintf(
"unset %s",
nv.Name,
)
},
}
)
type (
bashOp struct {
op string
nameValue NameValue
}
// BashDAO is a data access object for bash
BashDAO struct{}
)
func init() {
// For nitrous store the settings in .bash_profile since autoparts overrides
// the settings
if _, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".nitrousboxrc.sample")); err == nil {
bashShell.configFileName = filepath.Join(os.Getenv("HOME"), ".bash_profile")
}
RegisterDAO(1000, func() bool {
pid := os.Getpid()
for pid > 0 {
p, err := ps.FindProcess(pid)
if err != nil || p == nil {
break
}
if p.Executable() == "fish" {
return false
}
if p.Executable() == "bash" {
return true
}
pid = p.PPid()
}
return false
}, bashShell)
}