From c4b8659dcc0d457f1345d632c11c40b21c0270ee Mon Sep 17 00:00:00 2001 From: pratishshr Date: Tue, 31 Dec 2019 14:08:56 +0545 Subject: [PATCH] Add ability to run from .env files --- cli/cli.go | 10 ++++++++-- cli/list.go | 4 ++-- cli/run.go | 4 ++-- envault.go | 2 +- go.mod | 1 + go.sum | 2 ++ internal/secrets/secrets.go | 13 ++++++++++++- 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index e4d9cc7..f615e3d 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -21,6 +21,7 @@ func Initialize(info *Info) error { var env string var region string var profile string + var envFile string app := cli.NewApp() app.Name = info.Name @@ -54,6 +55,11 @@ func Initialize(info *Info) error { Usage: "Profile", Destination: &profile, }, + cli.StringFlag{ + Name: "envfile, ef", + Usage: "Use .env file", + Destination: &envFile, + }, } app.Commands = []cli.Command{ @@ -71,7 +77,7 @@ func Initialize(info *Info) error { Usage: "List environment variables stored in Secrets Manager", Flags: flags, Action: func(ctx *cli.Context) error { - List(secretName, env, region, profile) + List(secretName, env, region, profile, envFile) return nil }, @@ -82,7 +88,7 @@ func Initialize(info *Info) error { ArgsUsage: "[command]", Flags: flags, Action: func(ctx *cli.Context) error { - Run(secretName, ctx.Args().Get(0), env, region, profile) + Run(secretName, ctx.Args().Get(0), env, region, profile, envFile) return nil }, diff --git a/cli/list.go b/cli/list.go index b630165..a92f832 100644 --- a/cli/list.go +++ b/cli/list.go @@ -7,8 +7,8 @@ import ( ) // List all environment from Secrets Manager -func List(secretName string, env string, region string, profile string) { - for key, value := range secrets.GetSecrets(secretName, env, region, profile) { +func List(secretName string, env string, region string, profile string, envFile string) { + for key, value := range secrets.GetSecrets(secretName, env, region, profile, envFile) { fmt.Println(key + "=" + value) } } diff --git a/cli/run.go b/cli/run.go index e8abc6d..7e160b4 100644 --- a/cli/run.go +++ b/cli/run.go @@ -7,10 +7,10 @@ import ( ) // Run given command with the secrets from given Secret Manager. -func Run(secretName string, command string, env string, region string, profile string) { +func Run(secretName string, command string, env string, region string, profile string, envFile string) { if command == "" { exit.Error("Command to run is not specified. Add command as 'envault run [command]'") } - shell.Execute(command, secrets.GetSecrets(secretName, env, region, profile)) + shell.Execute(command, secrets.GetSecrets(secretName, env, region, profile, envFile)) } diff --git a/envault.go b/envault.go index 8e59a11..ff126c6 100644 --- a/envault.go +++ b/envault.go @@ -9,7 +9,7 @@ import ( func main() { info := &cli.Info{ Name: "Envault", - Version: "1.1.5", + Version: "1.1.6", Description: "Envault is a simple CLI tool which runs a process with secrets from AWS Secrets Manager.", AuthorName: "Pratish Shrestha", AuthorEmail: "pratishshr@gmail.com", diff --git a/go.mod b/go.mod index 25576a5..a4a05bf 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.12 require ( github.com/AlecAivazis/survey/v2 v2.0.1 github.com/aws/aws-sdk-go v1.20.3 + github.com/joho/godotenv v1.3.0 github.com/stretchr/testify v1.3.0 // indirect github.com/urfave/cli v1.20.0 golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect diff --git a/go.sum b/go.sum index 23446af..9949166 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDG github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ= diff --git a/internal/secrets/secrets.go b/internal/secrets/secrets.go index e54e88f..48ed618 100644 --- a/internal/secrets/secrets.go +++ b/internal/secrets/secrets.go @@ -1,13 +1,24 @@ package secrets import ( + "github.com/joho/godotenv" "github.com/pratishshr/envault/config" "github.com/pratishshr/envault/platform/aws" "github.com/pratishshr/envault/util/system/exit" ) // GetSecrets sets appropriate config and fetches secrets from aws. -func GetSecrets(secretName string, env string, region string, profile string) map[string]string { +func GetSecrets(secretName string, env string, region string, profile string, envFile string) map[string]string { + if envFile != "" { + secrets, err := godotenv.Read(envFile) + + if err != nil { + exit.Error("Could not read env file " + envFile) + } + + return secrets + } + conf := config.GetConfig() if env == "" {