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 20, 2020
1 parent 5c52c98 commit 2e5e74b
Show file tree
Hide file tree
Showing 2 changed files with 21 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
13 changes: 11 additions & 2 deletions tfexec/terraform_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,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[automationEnvVar] = "1"

env := []string{}
Expand All @@ -83,7 +92,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))
tf.logger.Printf("[INFO] running Terraform command: %s", cmdString(cmd))

return cmd
}

0 comments on commit 2e5e74b

Please sign in to comment.