44 "errors"
55 "fmt"
66 "io"
7+ "io/ioutil"
78 "log"
89 "os"
910 "os/exec"
@@ -46,13 +47,14 @@ func Execute(config *config.Commit0Config, pathPrefix string) {
4647 writeSecrets (awsSecrets )
4748 }
4849
50+ envars := getAwsEnvars (readSecrets ())
4951 log .Println ("Planning infrastructure..." )
50- execute (exec .Command ("terraform" , "init" ), pathPrefix )
51- execute (exec .Command ("terraform" , "plan" ), pathPrefix )
52+ execute (exec .Command ("terraform" , "init" ), pathPrefix , envars )
53+ execute (exec .Command ("terraform" , "plan" ), pathPrefix , envars )
5254 }
5355}
5456
55- func execute (cmd * exec.Cmd , pathPrefix string ) {
57+ func execute (cmd * exec.Cmd , pathPrefix string , envars [] string ) {
5658 dir , err := os .Getwd ()
5759
5860 if err != nil {
@@ -65,6 +67,12 @@ func execute(cmd *exec.Cmd, pathPrefix string) {
6567 stderrPipe , _ := cmd .StderrPipe ()
6668
6769 var errStdout , errStderr error
70+
71+ if envars != nil {
72+ log .Println ("Setting envars to cmd ..." )
73+ cmd .Env = envars
74+ }
75+
6876 err = cmd .Start ()
6977 if err != nil {
7078 log .Fatalf ("Starting terraform command failed: %v\n " , err )
@@ -91,6 +99,41 @@ func execute(cmd *exec.Cmd, pathPrefix string) {
9199 }
92100}
93101
102+ func getAwsEnvars (awsSecrets Secrets ) []string {
103+ env := os .Environ ()
104+ env = append (env , fmt .Sprintf ("AWS_ACCESS_KEY_ID=%s" , awsSecrets .Aws .AwsAccessKeyID ))
105+ env = append (env , fmt .Sprintf ("AWS_SECRET_ACCESS_KEY=%s" , awsSecrets .Aws .AwsSecretAccessKey ))
106+ env = append (env , fmt .Sprintf ("AWS_DEFAULT_REGION=%s" , awsSecrets .Aws .Region ))
107+
108+ return env
109+ }
110+
111+ func readSecrets () Secrets {
112+
113+ dir , err := os .Getwd ()
114+
115+ if err != nil {
116+ log .Fatalf ("Getting working directory failed: %v\n " , err )
117+ panic (err )
118+ }
119+
120+ secretsFile := fmt .Sprintf ("%s/secrets.yaml" , dir )
121+
122+ data , err := ioutil .ReadFile (secretsFile )
123+ if err != nil {
124+ log .Fatalln (err )
125+ }
126+
127+ awsSecrets := Secrets {}
128+
129+ err = yaml .Unmarshal (data , & awsSecrets )
130+ if err != nil {
131+ log .Fatalln (err )
132+ }
133+
134+ return awsSecrets
135+ }
136+
94137func writeSecrets (s Secrets ) {
95138 secretsYaml , err := yaml .Marshal (& s )
96139
@@ -120,7 +163,7 @@ func writeSecrets(s Secrets) {
120163
121164 n3 , err := f .WriteString (string (secretsYaml ))
122165 f .Sync ()
123- log .Printf ("wrote %d bytes to %v" , n3 , secretsFile )
166+ log .Printf ("Wrote %d bytes to %v" , n3 , secretsFile )
124167
125168}
126169
0 commit comments