From dce0fd807b453db387c2539e9a7148f321b699a7 Mon Sep 17 00:00:00 2001 From: Francis Nickels Date: Thu, 14 Jul 2022 13:55:06 -0700 Subject: [PATCH] change order of operations --- main.go | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index c807bb5a40..aa5a5c4871 100644 --- a/main.go +++ b/main.go @@ -159,6 +159,34 @@ func realMain() int { } services.SetUserAgent(httpclient.TerraformUserAgent(version.String())) + // Get the command line args. + binName := filepath.Base(os.Args[0]) + args := os.Args[1:] + + originalWd, err := os.Getwd() + if err != nil { + // It would be very strange to end up here + Ui.Error(fmt.Sprintf("Failed to determine current working directory: %s", err)) + return 1 + } + + //FJN here <<<<<<<<<<<<<<<<<<<< + // The arguments can begin with a -chdir option to ask Terraform to switch + // to a different working directory for the rest of its work. If that + // option is present then extractChdirOption returns a trimmed args with that option removed. + overrideWd, args, err := extractChdirOption(args) + if err != nil { + Ui.Error(fmt.Sprintf("Invalid -chdir option: %s", err)) + return 1 + } + if overrideWd != "" { + err := os.Chdir(overrideWd) + if err != nil { + Ui.Error(fmt.Sprintf("Error handling -chdir option: %s", err)) + return 1 + } + } + providerSrc, diags := providerSource(config.ProviderInstallation, services) if len(diags) > 0 { Ui.Error("There are some problems with the provider_installation configuration:") @@ -189,33 +217,6 @@ func realMain() int { // Initialize the backends. backendInit.Init(services) - // Get the command line args. - binName := filepath.Base(os.Args[0]) - args := os.Args[1:] - - originalWd, err := os.Getwd() - if err != nil { - // It would be very strange to end up here - Ui.Error(fmt.Sprintf("Failed to determine current working directory: %s", err)) - return 1 - } - - // The arguments can begin with a -chdir option to ask Terraform to switch - // to a different working directory for the rest of its work. If that - // option is present then extractChdirOption returns a trimmed args with that option removed. - overrideWd, args, err := extractChdirOption(args) - if err != nil { - Ui.Error(fmt.Sprintf("Invalid -chdir option: %s", err)) - return 1 - } - if overrideWd != "" { - err := os.Chdir(overrideWd) - if err != nil { - Ui.Error(fmt.Sprintf("Error handling -chdir option: %s", err)) - return 1 - } - } - // In tests, Commands may already be set to provide mock commands if Commands == nil { // Commands get to hold on to the original working directory here,