Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tfexec: Add (Terraform).SetLogCore() and (Terraform).SetLogProvider() methods #324

Merged
merged 1 commit into from Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions tfexec/cmd.go
Expand Up @@ -19,10 +19,12 @@ import (
const (
checkpointDisableEnvVar = "CHECKPOINT_DISABLE"
cliArgsEnvVar = "TF_CLI_ARGS"
logEnvVar = "TF_LOG"
inputEnvVar = "TF_INPUT"
automationEnvVar = "TF_IN_AUTOMATION"
logEnvVar = "TF_LOG"
logCoreEnvVar = "TF_LOG_CORE"
logPathEnvVar = "TF_LOG_PATH"
logProviderEnvVar = "TF_LOG_PROVIDER"
reattachEnvVar = "TF_REATTACH_PROVIDERS"
appendUserAgentEnvVar = "TF_APPEND_USER_AGENT"
workspaceEnvVar = "TF_WORKSPACE"
Expand All @@ -37,8 +39,10 @@ var prohibitedEnvVars = []string{
cliArgsEnvVar,
inputEnvVar,
automationEnvVar,
logPathEnvVar,
logEnvVar,
logCoreEnvVar,
logPathEnvVar,
logProviderEnvVar,
reattachEnvVar,
appendUserAgentEnvVar,
workspaceEnvVar,
Expand Down Expand Up @@ -148,10 +152,14 @@ func (tf *Terraform) buildEnv(mergeEnv map[string]string) []string {
if tf.logPath == "" {
// so logging can't pollute our stderr output
env[logEnvVar] = ""
env[logCoreEnvVar] = ""
env[logPathEnvVar] = ""
env[logProviderEnvVar] = ""
} else {
env[logPathEnvVar] = tf.logPath
env[logEnvVar] = tf.log
env[logCoreEnvVar] = tf.logCore
env[logPathEnvVar] = tf.logPath
env[logProviderEnvVar] = tf.logProvider
}

// constant automation override env vars
Expand Down
4 changes: 3 additions & 1 deletion tfexec/cmd_test.go
Expand Up @@ -39,8 +39,10 @@ func defaultEnv() []string {
"CHECKPOINT_DISABLE=",
"TF_APPEND_USER_AGENT=HashiCorp-terraform-exec/" + version.ModuleVersion(),
"TF_IN_AUTOMATION=1",
"TF_LOG_PATH=",
"TF_LOG=",
"TF_LOG_CORE=",
"TF_LOG_PATH=",
"TF_LOG_PROVIDER=",
"TF_WORKSPACE=",
}
}
Expand Down
41 changes: 38 additions & 3 deletions tfexec/terraform.go
Expand Up @@ -55,9 +55,15 @@ type Terraform struct {
// TF_LOG environment variable, defaults to TRACE if logPath is set.
log string

// TF_LOG_CORE environment variable
logCore string

// TF_LOG_PATH environment variable
logPath string

// TF_LOG_PROVIDER environment variable
logProvider string

versionLock sync.Mutex
execVersion *version.Version
provVersions map[string]*version.Version
Expand Down Expand Up @@ -131,8 +137,10 @@ func (tf *Terraform) SetStderr(w io.Writer) {
// This must be combined with a call to SetLogPath to take effect.
//
// This is only compatible with Terraform CLI 0.15.0 or later as setting the
// log level was unreliable in earlier versions. It will default to TRACE for
// those earlier versions when SetLogPath is called.
// log level was unreliable in earlier versions. It will default to TRACE when
// SetLogPath is called on versions 0.14.11 and earlier, or if SetLogCore and
// SetLogProvider have not been called before SetLogPath on versions 0.15.0 and
// later.
func (tf *Terraform) SetLog(log string) error {
err := tf.compatible(context.Background(), tf0_15_0, nil)
if err != nil {
Expand All @@ -142,17 +150,44 @@ func (tf *Terraform) SetLog(log string) error {
return nil
}

// SetLogCore sets the TF_LOG_CORE environment variable for Terraform CLI
// execution. This must be combined with a call to SetLogPath to take effect.
//
// This is only compatible with Terraform CLI 0.15.0 or later.
func (tf *Terraform) SetLogCore(logCore string) error {
err := tf.compatible(context.Background(), tf0_15_0, nil)
if err != nil {
return err
}
tf.logCore = logCore
return nil
}

// SetLogPath sets the TF_LOG_PATH environment variable for Terraform CLI
// execution.
func (tf *Terraform) SetLogPath(path string) error {
tf.logPath = path
// Prevent setting the log path without enabling logging
if tf.log == "" {
if tf.log == "" && tf.logCore == "" && tf.logProvider == "" {
tf.log = "TRACE"
}
return nil
}

// SetLogProvider sets the TF_LOG_PROVIDER environment variable for Terraform
// CLI execution. This must be combined with a call to SetLogPath to take
// effect.
//
// This is only compatible with Terraform CLI 0.15.0 or later.
func (tf *Terraform) SetLogProvider(logProvider string) error {
err := tf.compatible(context.Background(), tf0_15_0, nil)
if err != nil {
return err
}
tf.logProvider = logProvider
return nil
}

// SetAppendUserAgent sets the TF_APPEND_USER_AGENT environment variable for
// Terraform CLI execution.
func (tf *Terraform) SetAppendUserAgent(ua string) error {
Expand Down