Skip to content

Commit

Permalink
Merge pull request #3 from sachaos/feature/show-only-defined-in-s3
Browse files Browse the repository at this point in the history
Show only env vars defined in s3
  • Loading branch information
sachaos authored Oct 17, 2019
2 parents 0c256b0 + 8514054 commit a682f2e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
14 changes: 3 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -16,7 +15,7 @@ import (
"github.com/urfave/cli"
)

func LoadS3() (err error) {
func LoadS3() (map[string]string, error) {
sess := session.Must(session.NewSession())
svc := s3.New(sess)

Expand All @@ -27,23 +26,16 @@ func LoadS3() (err error) {
Key: aws.String(os.Getenv("S3ENV_KEY_NAME")),
})
if err != nil {
return
return nil, err
}

f, err := ioutil.TempFile("", "dotenv")

var r io.Reader
r = result.Body
if os.Getenv("S3ENV_BASE64ENCODE") == "y" {
r = base64.NewDecoder(base64.StdEncoding, result.Body)
}
io.Copy(f, r)

if err = godotenv.Load(f.Name()); err != nil {
return
}

return
return godotenv.Parse(r)
}

func runCmdStartPosition(args []string) (int, error) {
Expand Down
7 changes: 6 additions & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import (
)

func CmdRun(args []string) (err error) {
if err = LoadS3(); err != nil {
envMap, err := LoadS3()
if err != nil {
return
}

for k, v := range envMap {
os.Setenv(k, v)
}

path, err := exec.LookPath(args[0])
if err != nil {
return err
Expand Down
11 changes: 5 additions & 6 deletions show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"os"
"strings"

"github.com/urfave/cli"
Expand All @@ -13,22 +12,22 @@ func refineEnvVar(envVar string) string {
}

func CmdShow(c *cli.Context) (err error) {
if err = LoadS3(); err != nil {
envMap, err := LoadS3()
if err != nil {
return
}

envVarName := c.Args().Get(0)
if envVarName != "" {
fmt.Print(refineEnvVar(os.Getenv(envVarName)))
fmt.Print(refineEnvVar(envMap[envVarName]))
return
}

for _, env := range os.Environ() {
s := strings.SplitN(env, "=", 2)
for k, v := range envMap {
if c.Bool("export") {
fmt.Print("export ")
}
fmt.Printf("%s=\"%s\"\n", s[0], strings.Replace(refineEnvVar(s[1]), "\"", "\\\"", -1))
fmt.Printf("%s=\"%s\"\n", k, strings.Replace(refineEnvVar(v), "\"", "\\\"", -1))
}
return
}

0 comments on commit a682f2e

Please sign in to comment.