Skip to content

Commit 328ec6b

Browse files
committed
initial
0 parents  commit 328ec6b

File tree

8 files changed

+716
-0
lines changed

8 files changed

+716
-0
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2015 Caleb Doxsey
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# penv
2+
`penv` permanently sets environment variables. It supports the following:
3+
4+
* bash - entries are added to `~/.bashrc`
5+
* fish - entries are added to `~/.config/fish/config.fish`
6+
* windows - entries are added to the registry for the current user
7+
8+
## Installation
9+
`penv` is both a library and a command. To use the library in your own code see
10+
(godoc.org/github.com/badgerodon/penv). For the command:
11+
12+
go get github.com/badgerodon/penv/...
13+
14+
This creates a `penv` command. Here's its usage:
15+
16+
usage: penv [<flags>] <command> [<args> ...]
17+
18+
Permanently set/unset environment variables
19+
20+
Flags:
21+
--help Show help.
22+
--version Show application version.
23+
24+
Commands:
25+
help [<command>...]
26+
Show help.
27+
28+
set <name> <value>
29+
Permanently NAME to VALUE in the environment
30+
31+
unset <name>
32+
Permanently unset NAME in the environment
33+
34+
append <name> <value>
35+
Permanently append VALUE to NAME in the environment
36+
37+
## License
38+
MIT

bash_dao.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package penv
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/mitchellh/go-ps"
10+
)
11+
12+
var (
13+
bashShell = &shell{
14+
configFileName: filepath.Join(os.Getenv("HOME"), ".bashrc"),
15+
commentSigil: "#",
16+
quote: func(value string) string {
17+
r := strings.NewReplacer(
18+
"\\", "\\\\",
19+
"'", "\\'",
20+
"\n", `'"\n"'`,
21+
"\r", `'"\r"'`,
22+
)
23+
return "'" + r.Replace(value) + "'"
24+
},
25+
mkSet: func(sh *shell, nv NameValue) string {
26+
return fmt.Sprintf(
27+
"export %s=%s",
28+
nv.Name, sh.quote(nv.Value),
29+
)
30+
},
31+
mkAppend: func(sh *shell, nv NameValue) string {
32+
return fmt.Sprintf(
33+
"export %s=${%s}${%s:+:}%s",
34+
nv.Name, nv.Name, nv.Name, sh.quote(nv.Value),
35+
)
36+
},
37+
mkUnset: func(sh *shell, nv NameValue) string {
38+
return fmt.Sprintf(
39+
"unset %s",
40+
nv.Name,
41+
)
42+
},
43+
}
44+
)
45+
46+
type (
47+
bashOp struct {
48+
op string
49+
nameValue NameValue
50+
}
51+
// BashDAO is a data access object for bash
52+
BashDAO struct{}
53+
)
54+
55+
func init() {
56+
RegisterDAO(1000, func() bool {
57+
pid := os.Getpid()
58+
for pid > 0 {
59+
p, err := ps.FindProcess(pid)
60+
if err != nil || p == nil {
61+
break
62+
}
63+
if p.Executable() == "fish" {
64+
return false
65+
}
66+
if p.Executable() == "bash" {
67+
return true
68+
}
69+
pid = p.PPid()
70+
}
71+
return false
72+
}, bashShell)
73+
}

cmd/penv/main.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
7+
"github.com/golang-book/penv"
8+
9+
"gopkg.in/alecthomas/kingpin.v2"
10+
)
11+
12+
var help = `usage: penv command
13+
14+
Where command is:
15+
set Set an environmental variable
16+
unset Unset an environmental variable
17+
`
18+
var helpSet = `usage: penv set [NAME=VALUE]...
19+
Permanently set each NAME to VALUE in the environment.
20+
`
21+
var helpUnset = `usage: penv unset [NAME]...
22+
Permanently unset each NAME in the environment. Only variables added by
23+
'set' can be 'unset'.
24+
`
25+
26+
func main() {
27+
log.SetFlags(0)
28+
29+
app := kingpin.New("penv", "Permanently set/unset environment variables")
30+
app.Version("1.0")
31+
32+
setcmd := app.Command("set", "Permanently NAME to VALUE in the environment")
33+
setcmdName := setcmd.Arg("name", "environment variable name").Required().String()
34+
setcmdValue := setcmd.Arg("value", "environment variable value").Required().String()
35+
36+
unsetcmd := app.Command("unset", "Permanently unset NAME in the environment")
37+
unsetcmdName := unsetcmd.Arg("name", "environment variable name").Required().String()
38+
39+
appendcmd := app.Command("append", "Permanently append VALUE to NAME in the environment")
40+
appendcmdName := appendcmd.Arg("name", "environment variable name").Required().String()
41+
appendcmdValue := appendcmd.Arg("value", "environment vairable value").Required().String()
42+
43+
var err error
44+
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
45+
case setcmd.FullCommand():
46+
err = penv.SetEnv(*setcmdName, *setcmdValue)
47+
case unsetcmd.FullCommand():
48+
err = penv.UnsetEnv(*unsetcmdName)
49+
case appendcmd.FullCommand():
50+
err = penv.AppendEnv(*appendcmdName, *appendcmdValue)
51+
}
52+
if err != nil {
53+
log.Fatalln(err)
54+
}
55+
}

fish_dao.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package penv
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/mitchellh/go-ps"
11+
)
12+
13+
var (
14+
fishShell = &shell{
15+
configFileName: filepath.Join(os.Getenv("HOME"), ".config", "fish", "config.fish"),
16+
commentSigil: "#",
17+
quote: func(value string) string {
18+
r := strings.NewReplacer(
19+
"\\", "\\\\",
20+
"'", "\\'",
21+
"\n", `'"\n"'`,
22+
"\r", `'"\r"'`,
23+
)
24+
return "'" + r.Replace(value) + "'"
25+
},
26+
mkSet: func(sh *shell, nv NameValue) string {
27+
return fmt.Sprintf(
28+
"set -Ux %s %s",
29+
nv.Name, sh.quote(nv.Value),
30+
)
31+
},
32+
mkAppend: func(sh *shell, nv NameValue) string {
33+
return fmt.Sprintf(
34+
"set -Ux %s $%s %s",
35+
nv.Name, nv.Name, sh.quote(nv.Value),
36+
)
37+
},
38+
mkUnset: func(sh *shell, nv NameValue) string {
39+
return fmt.Sprintf(
40+
"set -Ue %s",
41+
nv.Name,
42+
)
43+
},
44+
}
45+
)
46+
47+
type fishReloader struct{ DAO }
48+
49+
func (fw fishReloader) Save(env *Environment) error {
50+
err := fw.DAO.Save(env)
51+
if err != nil {
52+
return err
53+
}
54+
return exec.Command("fish", "-c", "echo hi").Run()
55+
}
56+
57+
func init() {
58+
RegisterDAO(1000, func() bool {
59+
pid := os.Getpid()
60+
for pid > 0 {
61+
p, err := ps.FindProcess(pid)
62+
if err != nil || p == nil {
63+
break
64+
}
65+
switch p.Executable() {
66+
case "fish":
67+
return true
68+
case "bash":
69+
return false
70+
case "zsh":
71+
return false
72+
}
73+
pid = p.PPid()
74+
}
75+
return false
76+
}, fishReloader{fishShell})
77+
}

0 commit comments

Comments
 (0)