Skip to content

Commit

Permalink
Starts go version
Browse files Browse the repository at this point in the history
  • Loading branch information
radiospiel committed Oct 15, 2019
1 parent 582c1f6 commit 90287e5
Show file tree
Hide file tree
Showing 21 changed files with 712 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
tmp/
bin/envy.*.bin
.roundup.*
26 changes: 25 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
.PHONY: doc

default: test-ruby install doc
default: build-golang
#default: test-ruby install doc

test: test-ruby

# -- go specific --------------------------------------------------------------

.PHONY: bin/envy.go.bin
build-golang: bin/envy.go.bin

bin/envy.go.bin:
go build -o $@ src/golang/main.go

test-golang:
ENVY=bin/envy.go spec/bin/roundup spec/*-test.sh

build-golang-all:
# $MACHTYPE | $GOOS | $GOARCH
#
# x86_64-apple-darwin18 | darwin | amd64
# x86_64-pc-linux-gnu | linux | amd64
GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o bin/envy.x86_64-apple-darwin18.bin src/golang/main.go
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o bin/envy.x86_64-pc-linux-gnu.bin src/golang/main.go

# --- ruby specific -----------------------------------------------------------

test-ruby:
ENVY=bin/envy spec/bin/roundup spec/*-test.sh

# --- generic -----------------------------------------------------------------

install: install_bin install_doc

install_bin:
Expand Down
12 changes: 12 additions & 0 deletions bin/envy
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
12 changes: 12 additions & 0 deletions bin/envy.go
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 "$@"
3 changes: 3 additions & 0 deletions scripts/watch
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -eu -o pipefail
watchr src "$@"
9 changes: 9 additions & 0 deletions src/bash-completion
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
33 changes: 33 additions & 0 deletions src/golang/cli/cli.go
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
}
104 changes: 104 additions & 0 deletions src/golang/cli/edit.go
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)
}
17 changes: 17 additions & 0 deletions src/golang/cli/generate.go
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)
}
49 changes: 49 additions & 0 deletions src/golang/cli/load.go
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)
}
46 changes: 46 additions & 0 deletions src/golang/cli/run.go
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)
}
18 changes: 18 additions & 0 deletions src/golang/cli/secret_backup.go
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)
}
18 changes: 18 additions & 0 deletions src/golang/cli/secret_generate.go
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)
}
18 changes: 18 additions & 0 deletions src/golang/cli/secret_restore.go
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)
}
Loading

0 comments on commit 90287e5

Please sign in to comment.