Skip to content

Commit

Permalink
Add TF_LOG_PATH support
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Jul 18, 2020
1 parent 534499a commit cba46e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 10 additions & 1 deletion tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type Terraform struct {
workingDir string
execVersion string
env map[string]string
logger *log.Logger

logger *log.Logger
logPath string
}

// NewTerraform returns a Terraform struct with default values for all fields.
Expand Down Expand Up @@ -77,6 +79,13 @@ func (tf *Terraform) SetLogger(logger *log.Logger) {
tf.logger = logger
}

// SetLogPath sets the TF_LOG_PATH environment variable for Terraform CLI
// execution.
func (tf *Terraform) SetLogPath(path string) error {
tf.logPath = path
return nil
}

func (tf *Terraform) version() (string, error) {
versionCmd := tf.buildTerraformCmd(context.Background(), "version")

Expand Down
14 changes: 12 additions & 2 deletions tfexec/terraform_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tfexec

import (
"context"
"log"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -58,7 +59,16 @@ func (tf *Terraform) buildEnv() []string {
menv[checkpointDisableEnvVar] = os.Getenv(checkpointDisableEnvVar)
}

menv[logEnvVar] = "" // so logging can't pollute our stderr output
if tf.logPath == "" {
// so logging can't pollute our stderr output
menv[logEnvVar] = ""
menv[logPathEnvVar] = ""
} else {
menv[logPathEnvVar] = tf.logPath
// Log levels other than TRACE are currently unreliable, the CLI recommends using TRACE only.
menv[logEnvVar] = "TRACE"
}

menv[inputEnvVar] = "0"
menv[automationEnvVar] = "1"

Expand All @@ -77,7 +87,7 @@ func (tf *Terraform) buildTerraformCmd(ctx context.Context, args ...string) *exe
cmd.Env = env
cmd.Dir = tf.workingDir

tf.logger.Printf("Terraform command: %s", cmdString(cmd))
log.Printf("[INFO] running Terraform command: %s", cmdString(cmd))

return cmd
}

0 comments on commit cba46e0

Please sign in to comment.