Skip to content

Commit

Permalink
pass Stdout through, check for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Jones committed Aug 22, 2016
1 parent 91bfbaf commit bdd9285
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
5 changes: 5 additions & 0 deletions fixtures/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -eu

echo $VAR_FROM_YAML
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1
0.0.2
25 changes: 11 additions & 14 deletions yml2env.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package main

import (
"bytes"
"fmt"
"github.com/EngineerBetter/yml2env/env"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -18,7 +16,7 @@ var usage = "yml2env <YAML file> <command>"
func main() {
args := os.Args

if len(args) != 3 {
if len(args) < 3 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(1)
}
Expand All @@ -35,7 +33,11 @@ func main() {
envVars := os.Environ()
envVars = addUppercaseKeysToEnv(mapSlice, envVars)

run(envVars, args[1:])
err, _ := run(envVars, args[2:])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func fileExists(path string) bool {
Expand Down Expand Up @@ -75,8 +77,7 @@ func addUppercaseKeysToEnv(mapSlice yaml.MapSlice, envVars []string) []string {
if key, ok := item.Key.(string); ok {
key := strings.ToUpper(key)
if value, ok := item.Value.(string); ok {
env.Set(key, value, envVars)
fmt.Println(key, value)
envVars = env.Set(key, value, envVars)
} else {
fmt.Fprintln(os.Stderr, "YAML invalid")
os.Exit(1)
Expand All @@ -96,25 +97,21 @@ func commandWithEnv(envVars []string, args ...string) *exec.Cmd {
return cmd
}

func run(envVars []string, args []string) (error, int, string) {
func run(envVars []string, args []string) (error, int) {
cmd := commandWithEnv(envVars, args...)

buffer := bytes.NewBufferString("")
multiWriter := io.MultiWriter(os.Stdout, buffer)

cmd.Stdin = os.Stdin
cmd.Stdout = multiWriter
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := cmd.Start()

if err != nil {
return err, -1, ""
return err, -1
}

err = cmd.Wait()
output := buffer.String()
return nil, determineExitCode(cmd, err), output
return nil, determineExitCode(cmd, err)
}

func determineExitCode(cmd *exec.Cmd, err error) (exitCode int) {
Expand Down
2 changes: 1 addition & 1 deletion yml2env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var _ = Describe("yml2env", func() {
})

It("invokes the given command passing env vars from the YAML file", func() {
command := exec.Command(cliPath, "fixtures/vars.yml", "echo $VAR_FROM_YAML")
command := exec.Command(cliPath, "fixtures/vars.yml", "fixtures/script.sh")
session, err := Start(command, GinkgoWriter, GinkgoWriter)
Ω(err).ShouldNot(HaveOccurred())
Eventually(session).Should(Exit(0))
Expand Down

0 comments on commit bdd9285

Please sign in to comment.