From bb3d513c4f267dcb320d872333f4e73f7923b4fb Mon Sep 17 00:00:00 2001 From: Pam Selle <204372+pselle@users.noreply.github.com> Date: Mon, 13 Jan 2020 15:22:34 -0500 Subject: [PATCH 1/3] Some deletions from static analysis --- backend/local/backend.go | 39 --------------------------------------- config.go | 25 ------------------------- 2 files changed, 64 deletions(-) diff --git a/backend/local/backend.go b/backend/local/backend.go index 62c016c49..e73959499 100644 --- a/backend/local/backend.go +++ b/backend/local/backend.go @@ -14,7 +14,6 @@ import ( "github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/command/clistate" "github.com/hashicorp/terraform/configs/configschema" - "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/states/statemgr" "github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/tfdiags" @@ -180,11 +179,6 @@ func (b *Local) Configure(obj cty.Value) tfdiags.Diagnostics { var diags tfdiags.Diagnostics - type Config struct { - Path string `hcl:"path,optional"` - WorkspaceDir string `hcl:"workspace_dir,optional"` - } - if val := obj.GetAttr("path"); !val.IsNull() { p := val.AsString() b.StatePath = p @@ -456,39 +450,6 @@ func (b *Local) Colorize() *colorstring.Colorize { } } -func (b *Local) schemaConfigure(ctx context.Context) error { - d := schema.FromContextBackendConfig(ctx) - - // Set the path if it is set - pathRaw, ok := d.GetOk("path") - if ok { - path := pathRaw.(string) - if path == "" { - return fmt.Errorf("configured path is empty") - } - - b.StatePath = path - b.StateOutPath = path - } - - if raw, ok := d.GetOk("workspace_dir"); ok { - path := raw.(string) - if path != "" { - b.StateWorkspaceDir = path - } - } - - // Legacy name, which ConflictsWith workspace_dir - if raw, ok := d.GetOk("environment_dir"); ok { - path := raw.(string) - if path != "" { - b.StateWorkspaceDir = path - } - } - - return nil -} - // StatePaths returns the StatePath, StateOutPath, and StateBackupPath as // configured from the CLI. func (b *Local) StatePaths(name string) (stateIn, stateOut, backupOut string) { diff --git a/config.go b/config.go index fcb48ffb9..a3ff1e88e 100644 --- a/config.go +++ b/config.go @@ -24,23 +24,6 @@ type Config = cliconfig.Config // discovery behavior for a particular hostname. type ConfigHost = cliconfig.ConfigHost -// ConfigCredentialsHelper is the structure of the "credentials_helper" -// nested block within the CLI configuration. -type ConfigCredentialsHelper = cliconfig.ConfigCredentialsHelper - -// BuiltinConfig is the built-in defaults for the configuration. These -// can be overridden by user configurations. -var BuiltinConfig = cliconfig.BuiltinConfig - -// ConfigFile returns the default path to the configuration file. -// -// On Unix-like systems this is the ".terraformrc" file in the home directory. -// On Windows, this is the "terraform.rc" file in the application data -// directory. -func ConfigFile() (string, error) { - return cliconfig.ConfigFile() -} - // ConfigDir returns the configuration directory for Terraform. func ConfigDir() (string, error) { return cliconfig.ConfigDir() @@ -52,11 +35,3 @@ func ConfigDir() (string, error) { func LoadConfig() (*Config, tfdiags.Diagnostics) { return cliconfig.LoadConfig() } - -// EnvConfig returns a Config populated from environment variables. -// -// Any values specified in this config should override those set in the -// configuration file. -func EnvConfig() *Config { - return cliconfig.EnvConfig() -} From 37d16b2f79527f448506d54fa854e522b8f3473b Mon Sep 17 00:00:00 2001 From: Pam Selle <204372+pselle@users.noreply.github.com> Date: Mon, 13 Jan 2020 15:30:46 -0500 Subject: [PATCH 2/3] Deletions from backend local --- backend/local/backend_apply_test.go | 20 -------------------- backend/local/backend_local.go | 8 -------- backend/local/backend_plan.go | 10 ---------- 3 files changed, 38 deletions(-) diff --git a/backend/local/backend_apply_test.go b/backend/local/backend_apply_test.go index cbf674761..79c049a9e 100644 --- a/backend/local/backend_apply_test.go +++ b/backend/local/backend_apply_test.go @@ -261,26 +261,6 @@ func testOperationApply(t *testing.T, configDir string) (*backend.Operation, fun }, configCleanup } -// testApplyState is just a common state that we use for testing refresh. -func testApplyState() *terraform.State { - return &terraform.State{ - Version: 2, - Modules: []*terraform.ModuleState{ - &terraform.ModuleState{ - Path: []string{"root"}, - Resources: map[string]*terraform.ResourceState{ - "test_instance.foo": &terraform.ResourceState{ - Type: "test_instance", - Primary: &terraform.InstanceState{ - ID: "bar", - }, - }, - }, - }, - }, - } -} - // applyFixtureSchema returns a schema suitable for processing the // configuration in testdata/apply . This schema should be // assigned to a mock provider named "test". diff --git a/backend/local/backend_local.go b/backend/local/backend_local.go index 3a308345d..4fffc5b66 100644 --- a/backend/local/backend_local.go +++ b/backend/local/backend_local.go @@ -410,11 +410,3 @@ func (v unparsedUnknownVariableValue) ParseVariableValue(mode configs.VariablePa SourceType: terraform.ValueFromInput, }, nil } - -const validateWarnHeader = ` -There are warnings related to your configuration. If no errors occurred, -Terraform will continue despite these warnings. It is a good idea to resolve -these warnings in the near future. - -Warnings: -` diff --git a/backend/local/backend_plan.go b/backend/local/backend_plan.go index 089453f1d..fdf5dd796 100644 --- a/backend/local/backend_plan.go +++ b/backend/local/backend_plan.go @@ -313,16 +313,6 @@ func RenderPlan(plan *plans.Plan, state *states.State, schemas *terraform.Schema ))) } -const planErrNoConfig = ` -No configuration files found! - -Plan requires configuration to be present. Planning without a configuration -would mark everything for destruction, which is normally not what is desired. -If you would like to destroy everything, please run plan with the "-destroy" -flag or create a single empty configuration file. Otherwise, please create -a Terraform configuration file in the path being executed and try again. -` - const planHeaderIntro = ` An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: From 78b12205587fe839f10d946ea3fdc06719decb05 Mon Sep 17 00:00:00 2001 From: Pam Selle <204372+pselle@users.noreply.github.com> Date: Mon, 13 Jan 2020 16:50:05 -0500 Subject: [PATCH 3/3] Remove config.go and update things using its aliases --- checkpoint.go | 5 +++-- commands.go | 6 +++--- config.go | 37 ------------------------------------- main.go | 3 ++- plugins.go | 4 +++- 5 files changed, 11 insertions(+), 44 deletions(-) delete mode 100644 config.go diff --git a/checkpoint.go b/checkpoint.go index 4837e4763..5885bb345 100644 --- a/checkpoint.go +++ b/checkpoint.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/go-checkpoint" "github.com/hashicorp/terraform/command" + "github.com/hashicorp/terraform/command/cliconfig" ) func init() { @@ -17,7 +18,7 @@ var checkpointResult chan *checkpoint.CheckResponse // runCheckpoint runs a HashiCorp Checkpoint request. You can read about // Checkpoint here: https://github.com/hashicorp/go-checkpoint. -func runCheckpoint(c *Config) { +func runCheckpoint(c *cliconfig.Config) { // If the user doesn't want checkpoint at all, then return. if c.DisableCheckpoint { log.Printf("[INFO] Checkpoint disabled. Not running.") @@ -25,7 +26,7 @@ func runCheckpoint(c *Config) { return } - configDir, err := ConfigDir() + configDir, err := cliconfig.ConfigDir() if err != nil { log.Printf("[ERR] Checkpoint setup error: %s", err) checkpointResult <- nil diff --git a/commands.go b/commands.go index b72c5e856..ea730e11f 100644 --- a/commands.go +++ b/commands.go @@ -6,7 +6,7 @@ import ( "github.com/mitchellh/cli" - "github.com/hashicorp/terraform-svchost" + svchost "github.com/hashicorp/terraform-svchost" "github.com/hashicorp/terraform-svchost/auth" "github.com/hashicorp/terraform-svchost/disco" "github.com/hashicorp/terraform/command" @@ -37,7 +37,7 @@ const ( OutputPrefix = "o:" ) -func initCommands(config *Config, services *disco.Disco) { +func initCommands(config *cliconfig.Config, services *disco.Disco) { var inAutomation bool if v := os.Getenv(runningInAutomationEnvName); v != "" { inAutomation = true @@ -390,7 +390,7 @@ func makeShutdownCh() <-chan struct{} { return resultCh } -func credentialsSource(config *Config) (auth.CredentialsSource, error) { +func credentialsSource(config *cliconfig.Config) (auth.CredentialsSource, error) { helperPlugins := pluginDiscovery.FindPlugins("credentials", globalPluginDirs()) return config.CredentialsSource(helperPlugins) } diff --git a/config.go b/config.go deleted file mode 100644 index a3ff1e88e..000000000 --- a/config.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -// This file has some compatibility aliases/wrappers for functionality that -// has now moved into command/cliconfig . -// -// Don't add anything new here! If new functionality is needed, better to just -// add it in command/cliconfig and then call there directly. - -import ( - "github.com/hashicorp/terraform/command/cliconfig" - "github.com/hashicorp/terraform/tfdiags" -) - -//go:generate go run ./scripts/generate-plugins.go - -// Config is the structure of the configuration for the Terraform CLI. -// -// This is not the configuration for Terraform itself. That is in the -// "configs" package. -type Config = cliconfig.Config - -// ConfigHost is the structure of the "host" nested block within the CLI -// configuration, which can be used to override the default service host -// discovery behavior for a particular hostname. -type ConfigHost = cliconfig.ConfigHost - -// ConfigDir returns the configuration directory for Terraform. -func ConfigDir() (string, error) { - return cliconfig.ConfigDir() -} - -// LoadConfig reads the CLI configuration from the various filesystem locations -// and from the environment, returning a merged configuration along with any -// diagnostics (errors and warnings) encountered along the way. -func LoadConfig() (*Config, tfdiags.Diagnostics) { - return cliconfig.LoadConfig() -} diff --git a/main.go b/main.go index 628e1447d..3fbd4cbcc 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/go-plugin" "github.com/hashicorp/terraform-svchost/disco" + "github.com/hashicorp/terraform/command/cliconfig" "github.com/hashicorp/terraform/command/format" "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/httpclient" @@ -125,7 +126,7 @@ func wrappedMain() int { log.Printf("[INFO] Go runtime version: %s", runtime.Version()) log.Printf("[INFO] CLI args: %#v", os.Args) - config, diags := LoadConfig() + config, diags := cliconfig.LoadConfig() if len(diags) > 0 { // Since we haven't instantiated a command.Meta yet, we need to do // some things manually here and use some "safe" defaults for things diff --git a/plugins.go b/plugins.go index cf2d54253..47ae2e4f6 100644 --- a/plugins.go +++ b/plugins.go @@ -5,6 +5,8 @@ import ( "log" "path/filepath" "runtime" + + "github.com/hashicorp/terraform/command/cliconfig" ) // globalPluginDirs returns directories that should be searched for @@ -16,7 +18,7 @@ import ( func globalPluginDirs() []string { var ret []string // Look in ~/.terraform.d/plugins/ , or its equivalent on non-UNIX - dir, err := ConfigDir() + dir, err := cliconfig.ConfigDir() if err != nil { log.Printf("[ERROR] Error finding global config directory: %s", err) } else {