-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eea83de
Showing
7 changed files
with
460 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
envdo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Anton Lindström | ||
|
||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# envdo | ||
|
||
Manage environment variables with a command. | ||
|
||
envdo came into existance when I realized I had multiple environment | ||
variables set for different work, for home and for side projects. This meant | ||
having one default and then use aliases or manually try to switch the | ||
environment variables. Since this is a hassle I wanted to make it easy to | ||
switch while still maintain some security. | ||
|
||
The goal is to have all environment variables in gpg encrypted files and | ||
import them when needed. | ||
|
||
### Examples | ||
|
||
List profiles: | ||
|
||
envdo ls | ||
|
||
Use project1 environment variables with command `env`: | ||
|
||
envdo project1 env | ||
|
||
Add new environment variable file: | ||
|
||
envdo -r GPGID add profile | ||
|
||
### Installation instructions | ||
|
||
The command can be installed with `go get`: | ||
|
||
go get -v github.com/antonlindstrom/envdo | ||
|
||
## Bugs | ||
|
||
I'm sure there are a lot of bugs or surprises, please file anything you find | ||
and I will try to do my best to solve them. | ||
|
||
Currently the code is very hacky and there are no tests, this was made in an | ||
evening and works for my uses. | ||
|
||
## Acknowledgements | ||
|
||
This program has borrowed most of its ideas from | ||
[Pass](https://www.passwordstore.org/). This software can be managed by Pass | ||
and I suggest linking `~/.password-store/envvars` to `.envdo`. | ||
|
||
## Author | ||
|
||
This is maintained by [Anton Lindstrom](https://www.antonlindstrom.com). | ||
|
||
## License | ||
|
||
See [LICENSE](LICENSE) file in the current directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
_envdo() { | ||
COMPREPLY=() | ||
local word="${COMP_WORDS[COMP_CWORD]}" | ||
|
||
if [ "$COMP_CWORD" -eq 1 ]; then | ||
COMPREPLY=( $(compgen -W "$(envdo ls --plain) help add ls --gpg-recipient --version --directory" -- "$word") ) | ||
elif [[ $COMP_CWORD -eq 2 ]]; then | ||
local lastarg="${COMP_WORDS[$COMP_CWORD-1]}" | ||
case "${COMP_WORDS[1]}" in | ||
add) | ||
if [[ $COMP_CWORD -le 2 ]]; then | ||
COMPREPLY=($(compgen -W "$(envdo ls --plain)" -- ${word})); | ||
fi | ||
;; | ||
esac | ||
else | ||
local command="${COMP_WORDS[1]}" | ||
# TODO: load autocompletion for all other commands. | ||
#local completions="$("$command")" | ||
#COMPREPLY=( $(compgen -W "$completions" -- "$word") ) | ||
fi | ||
} | ||
|
||
complete -o default -F _envdo envdo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var passhroughEnvVars = []string{ | ||
"LANG", | ||
"LC_CTYPE", | ||
"TERM", | ||
"SHELL", | ||
"PATH", | ||
"PWD", | ||
"HOME", | ||
"USER", | ||
"LOGNAME", | ||
} | ||
|
||
func readFiles(path string) ([]string, error) { | ||
files, err := ioutil.ReadDir(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var allFiles []string | ||
|
||
for _, f := range files { | ||
if f.Mode().IsDir() { | ||
if strings.HasPrefix(f.Name(), ".") { | ||
continue | ||
} | ||
|
||
subdirFiles, err := readFiles(path + "/" + f.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
allFiles = append(allFiles, subdirFiles...) | ||
|
||
continue | ||
} | ||
|
||
allFiles = append(allFiles, path+"/"+f.Name()) | ||
} | ||
|
||
return allFiles, nil | ||
} | ||
|
||
func profileName(base, path string) string { | ||
return strings.TrimPrefix(path, base+"/") | ||
} | ||
|
||
func absProfilePath(path, profileName string) (string, error) { | ||
stat, err := os.Lstat(path) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if stat.Mode() == os.ModeSymlink { | ||
path, err = os.Readlink(path) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
return fmt.Sprintf("%s/%s", path, profileName), nil | ||
} | ||
|
||
func passtroughEnvWithValues() []string { | ||
var evs []string | ||
|
||
for _, name := range passhroughEnvVars { | ||
evs = append(evs, fmt.Sprintf("%s=%s", name, os.Getenv(name))) | ||
} | ||
|
||
return evs | ||
} | ||
|
||
func fetchEnv(path, profile string, recipient *string) ([]string, error) { | ||
absPath, err := absProfilePath(path, profile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var reader io.Reader | ||
|
||
ext := filepath.Ext(absPath) | ||
if ext == ".gpg" { | ||
var b []byte | ||
var gpgArgs []string | ||
|
||
if recipient != nil { | ||
gpgArgs = append(gpgArgs, []string{"-r", *recipient}...) | ||
} | ||
|
||
gpgArgs = append(gpgArgs, []string{"-d", "--quiet", absPath}...) | ||
|
||
buf := bytes.NewBuffer(b) | ||
|
||
err := executeCmdWithWriter("gpg2", gpgArgs, true, nil, buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reader = buf | ||
} else { | ||
f, err := os.Open(absPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reader = f | ||
|
||
defer f.Close() | ||
} | ||
|
||
var lines []string | ||
|
||
scanner := bufio.NewScanner(reader) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
|
||
if strings.HasPrefix("#", line) { | ||
continue | ||
} | ||
|
||
lines = append(lines, strings.Replace(line, `"`, ``, -1)) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return lines, nil | ||
} | ||
|
||
func executeCmd(command string, args []string, preserveEnv bool, env []string) error { | ||
return executeCmdWithWriter(command, args, preserveEnv, env, os.Stdout) | ||
} | ||
|
||
func executeCmdWithWriter(command string, args []string, preserveEnv bool, env []string, writer io.Writer) error { | ||
cmd := exec.Command(command, args...) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stderr = os.Stderr | ||
cmd.Stdout = writer | ||
|
||
// Default variables to pass through. | ||
cmd.Env = passtroughEnvWithValues() | ||
|
||
if preserveEnv { | ||
cmd.Env = os.Environ() | ||
} | ||
|
||
cmd.Env = append(cmd.Env, env...) | ||
|
||
return cmd.Run() | ||
} |
Oops, something went wrong.