Skip to content

Commit

Permalink
Merge pull request #29 from patoarvizu/log-terraform-output
Browse files Browse the repository at this point in the history
Log output of Terraform commands
  • Loading branch information
patoarvizu authored Feb 20, 2022
2 parents ec2128c + 35a8777 commit 3d5622e
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions controllers/terraformstate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,26 @@ func (r *TerraformStateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err

cmd := exec.Command("/terraform-bin/terraform", "apply", "-auto-approve")
cmd.Dir = stateDir
err = cmd.Run()
output, err := cmd.CombinedOutput()
r.Log.Info(string(output))
if err != nil {
return ctrl.Result{}, err
}

cmd = exec.Command("/terraform-bin/terraform", "output", "-json")
cmd.Dir = stateDir
var out bytes.Buffer
cmd.Stdout = &out
var stdout bytes.Buffer
cmd.Stdout = &stdout
var stderr bytes.Buffer
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
r.Log.Info(stderr.String())
return ctrl.Result{}, err
}

var tfOutputs terraformOutputs
err = json.Unmarshal(out.Bytes(), &tfOutputs)
err = json.Unmarshal(stdout.Bytes(), &tfOutputs)
if err != nil {
return ctrl.Result{}, err
}
Expand Down

0 comments on commit 3d5622e

Please sign in to comment.