Skip to content

Commit

Permalink
Merge pull request #19 from pratishshr/dotenv
Browse files Browse the repository at this point in the history
Add ability to run from .env files
  • Loading branch information
pratishshr authored Dec 31, 2019
2 parents 7a09229 + c4b8659 commit 31366db
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 8 deletions.
10 changes: 8 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -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
},
Expand All @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion envault.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 12 additions & 1 deletion internal/secrets/secrets.go
Original file line number Diff line number Diff line change
@@ -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 == "" {
Expand Down

0 comments on commit 31366db

Please sign in to comment.