Skip to content

Commit d8c2220

Browse files
committed
add osx support
1 parent cfcbee8 commit d8c2220

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

darwin_dao.go

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,65 @@
33
package penv
44

55
import (
6+
"fmt"
7+
"io/ioutil"
68
"os"
9+
"os/exec"
710
"path/filepath"
811
"runtime"
12+
"strings"
913
)
1014

11-
var darwinPlist = `<?xml version="1.0" encoding="UTF-8"?>
15+
var (
16+
darwinPlist = `<?xml version="1.0" encoding="UTF-8"?>
1217
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
1318
<plist version="1.0">
1419
<dict>
1520
<key>Label</key>
1621
<string>com.github.badgerodon.penv</string>
17-
<key>Program</key>
18-
<string>` + filepath.Join(os.Getenv("HOME"), ".config", "penv.sh") + `</string>
22+
<key>ProgramArguments</key>
23+
<array>
24+
<string>bash</string>
25+
<string>` + filepath.Join(os.Getenv("HOME"), ".config", "penv.sh") + `</string>
26+
</array>
1927
<key>RunAtLoad</key>
2028
<true/>
2129
</dict>
2230
</plist>`
2331

32+
darwinShell = &shell{
33+
configFileName: filepath.Join(os.Getenv("HOME"), ".config", "penv.sh"),
34+
commentSigil: "#",
35+
quote: func(value string) string {
36+
r := strings.NewReplacer(
37+
"\\", "\\\\",
38+
"'", "\\'",
39+
"\n", `'"\n"'`,
40+
"\r", `'"\r"'`,
41+
)
42+
return "'" + r.Replace(value) + "'"
43+
},
44+
mkSet: func(sh *shell, nv NameValue) string {
45+
return fmt.Sprintf(
46+
"launchctl setenv %s %s",
47+
nv.Name, sh.quote(nv.Value),
48+
)
49+
},
50+
mkAppend: func(sh *shell, nv NameValue) string {
51+
return fmt.Sprintf(
52+
"launchctl setenv %s ${%s}${%s:+:}%s",
53+
nv.Name, nv.Name, nv.Name, sh.quote(nv.Value),
54+
)
55+
},
56+
mkUnset: func(sh *shell, nv NameValue) string {
57+
return fmt.Sprintf(
58+
"launchctl unsetenv %s",
59+
nv.Name,
60+
)
61+
},
62+
}
63+
)
64+
2465
// DarwinDAO is the data access object for OSX
2566
type DarwinDAO struct {
2667
}
@@ -33,10 +74,30 @@ func init() {
3374

3475
// Load loads the environment
3576
func (dao *DarwinDAO) Load() (*Environment, error) {
36-
panic("NOT IMPLEMENTED")
77+
return darwinShell.Load()
3778
}
3879

3980
// Save saves the environment
4081
func (dao *DarwinDAO) Save(env *Environment) error {
41-
panic("NOT IMPLEMENTED")
82+
err := darwinShell.Save(env)
83+
if err != nil {
84+
return err
85+
}
86+
87+
plistName := filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents", "penv.plist")
88+
89+
_, err = ioutil.WriteFile(
90+
plistName,
91+
darwinPlist,
92+
)
93+
if err != nil {
94+
return err
95+
}
96+
97+
err = exec.Command("launchctl", "load", plistName).Run()
98+
if err != nil {
99+
return err
100+
}
101+
102+
return nil
42103
}

0 commit comments

Comments
 (0)