-
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
1 parent
582c1f6
commit 90287e5
Showing
21 changed files
with
712 additions
and
1 deletion.
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
tmp/ | ||
bin/envy.*.bin | ||
.roundup.* |
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
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,12 @@ | ||
#!/bin/bash | ||
set -eu -o pipefail | ||
|
||
BINARY=$0.$MACHTYPE.bin | ||
|
||
[ -x $BINARY ] && exec $BINARY $@ | ||
|
||
cat <<MSG | ||
It looks like there is no envy binary for the machine type $MACHTYPE. | ||
You probably have to build one yourself. Go to https://github.com:radiospiel/envy-rb.git | ||
MSG |
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,12 @@ | ||
#!/bin/bash | ||
set -eu -o pipefail | ||
|
||
newest_file=$(find src/golang/ -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" ") | ||
|
||
if [ $newest_file -nt $0.bin ]; then | ||
echo "> building $0.bin..." >&2 | ||
# go build -ldflags="-s -w" -o $0.bin src/envy/main.go | ||
go build -o $0.bin src/golang/main.go | ||
fi | ||
|
||
exec $0.bin "$@" |
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,3 @@ | ||
#!/bin/bash | ||
set -eu -o pipefail | ||
watchr src "$@" |
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,9 @@ | ||
_examples() { | ||
args=("${COMP_WORDS[@]:1:$COMP_CWORD}") | ||
|
||
local IFS=$'\n' | ||
COMPREPLY=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}")) | ||
return 1 | ||
} | ||
|
||
complete -F _examples examples |
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,33 @@ | ||
package cli | ||
|
||
import ( | ||
"../envy" | ||
"github.com/spf13/cobra" | ||
"log" | ||
) | ||
|
||
var ( | ||
rootCmd = &cobra.Command{ | ||
Use: "envy", | ||
Short: "envy handles secure encvironments", | ||
Long: `envy handles secure encvironments`, | ||
} | ||
) | ||
|
||
// Execute executes the root command. | ||
func Execute() error { | ||
return rootCmd.Execute() | ||
} | ||
|
||
func check(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func mustLoadConfig(path string) map[string]string { | ||
config, err := envy.LoadConfig(path) | ||
check(err) | ||
|
||
return config | ||
} |
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,104 @@ | ||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
import ( | ||
"../envy" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func copyEnvyFile(srcPath string, dest *os.File, do_encrypt bool) error { | ||
return envy.ParseFile(srcPath, func(mode envy.Mode, pt1 string, pt2 string) { | ||
switch mode { | ||
case envy.Mode_Value: | ||
fmt.Fprintf(dest, "%s=%s\n", pt1, pt2) | ||
case envy.Mode_Secured_Value: | ||
var value string | ||
if do_encrypt { | ||
value = envy.EncryptSecuredValue(pt2) | ||
} else { | ||
value = envy.DecryptSecuredValue(pt2) | ||
} | ||
|
||
fmt.Fprintf(dest, "%s=%s\n", pt1, value) | ||
case envy.Mode_Line: | ||
fmt.Fprintf(dest, "%s\n", pt1) | ||
} | ||
}) | ||
} | ||
|
||
func copyAndDecryptEnvyFile(srcPath string, dest *os.File) error { | ||
return copyEnvyFile(srcPath, dest, false) | ||
} | ||
|
||
func copyAndEncryptEnvyFile(srcPath string, dest *os.File) error { | ||
return copyEnvyFile(srcPath, dest, true) | ||
} | ||
|
||
func shell(str string) error { | ||
cmd := exec.Command("/bin/sh", "-c", str) // editor, tmpFile.Name()) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
return cmd.Run() | ||
} | ||
|
||
func do_edit(path string) { | ||
/* | ||
* create a temp file, and decrypt src into the temp file. | ||
*/ | ||
tmpFile, err := ioutil.TempFile("", "envy") | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
check(err) | ||
|
||
err = copyAndDecryptEnvyFile(path, tmpFile) | ||
check(err) | ||
|
||
tmpFile.Close() | ||
|
||
/* | ||
* make sure we have an EDITOR env variable | ||
*/ | ||
_, ok := os.LookupEnv("EDITOR") | ||
if !ok { | ||
os.Setenv("EDITOR", "vi") | ||
} | ||
|
||
/* | ||
* run editor on tmpFile. The editor is determined by $EDITOR, and | ||
*/ | ||
err = shell("$EDITOR " + tmpFile.Name()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
/* | ||
* encrypt temp file into the original path. | ||
*/ | ||
|
||
dest, err := os.Create(path) | ||
check(err) | ||
defer dest.Close() | ||
|
||
copyAndEncryptEnvyFile(tmpFile.Name(), dest) | ||
check(err) | ||
} | ||
|
||
func init() { | ||
var cmd = &cobra.Command{ | ||
Use: "edit", | ||
Short: "Edit an envy file", | ||
Long: `Edit an envy file in your $EDITOR`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
do_edit(args[0]) | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
} |
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,17 @@ | ||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func init() { | ||
var cmd = &cobra.Command{ | ||
Use: "generate", | ||
Short: "Generate an envy file", | ||
Long: `Generate and edit an envy file`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// edit(args[0]) | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
} |
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,49 @@ | ||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// go get github.com/Wing924/shellwords | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/Wing924/shellwords" | ||
) | ||
|
||
func init() { | ||
var opts_json bool | ||
var opts_export bool | ||
|
||
var cmd = &cobra.Command{ | ||
Use: "load", | ||
Short: "Load an envy file", | ||
Long: `Load an envy file`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
path := args[0] | ||
|
||
config := mustLoadConfig(path) | ||
|
||
if opts_json { | ||
jsonData, err := json.MarshalIndent(config, "", " ") | ||
check(err) | ||
|
||
fmt.Printf("%s\n", jsonData) | ||
} else { | ||
prefix := "" | ||
if opts_export { | ||
prefix = "export " | ||
} | ||
|
||
for k, v := range config { | ||
fmt.Printf("%s%s=%s\n", prefix, k, shellwords.Escape(v)) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
cmd.Flags().BoolVarP(&opts_json, "json", "j", false, "Print as JSON") | ||
cmd.Flags().BoolVarP(&opts_export, "export", "e", false, "Print as sh(1) export") | ||
|
||
rootCmd.AddCommand(cmd) | ||
} |
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,46 @@ | ||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func run(path string, args []string) { | ||
/* | ||
* fill in config into environment | ||
*/ | ||
config := mustLoadConfig(path) | ||
|
||
for k, v := range config { | ||
os.Setenv(k, v) | ||
} | ||
|
||
/* | ||
* create command and set arguments | ||
*/ | ||
cmd := exec.Command(args[0], args[1:]...) | ||
|
||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
err := cmd.Run() | ||
check(err) | ||
} | ||
|
||
func init() { | ||
var cmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Load an envy file and run command", | ||
Long: `Load an envy file and run command`, | ||
Args: cobra.MinimumNArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
path := args[0] | ||
run(path, args[1:]) | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
} |
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,18 @@ | ||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
import () | ||
|
||
func init() { | ||
var cmd = &cobra.Command{ | ||
Use: "secret:backup", | ||
Short: "Backup the envy secret", | ||
Long: `Backup the envy secret`, | ||
Args: cobra.ExactArgs(0), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
} |
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,18 @@ | ||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
import () | ||
|
||
func init() { | ||
var cmd = &cobra.Command{ | ||
Use: "secret:generate", | ||
Short: "Generate the envy secret", | ||
Long: `Generate the envy secret`, | ||
Args: cobra.ExactArgs(0), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
} |
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,18 @@ | ||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
import () | ||
|
||
func init() { | ||
var cmd = &cobra.Command{ | ||
Use: "secret:restore", | ||
Short: "Restore the envy secret", | ||
Long: `Restore the envy secret`, | ||
Args: cobra.ExactArgs(0), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(cmd) | ||
} |
Oops, something went wrong.