From 076c47de0674138246c72d5dde77b94c85b941d9 Mon Sep 17 00:00:00 2001 From: Mario Di Miceli Date: Mon, 13 May 2024 17:39:08 +0200 Subject: [PATCH 1/6] if user has a terraform use it --- terraform/binary_path.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/terraform/binary_path.go b/terraform/binary_path.go index aa869930b..3ab7d5eff 100644 --- a/terraform/binary_path.go +++ b/terraform/binary_path.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "os/exec" "strconv" "strings" "time" @@ -45,6 +46,12 @@ func NewBinary() *Binary { } func (binary *Binary) BinaryPath() (string, error) { + // if user has a terraform use it + userTerraform, err := exec.LookPath("terraform") + if err == nil && userTerraform != "" { + return userTerraform, nil + } + destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName) exists, err := binary.FS.Exists(destinationPath) if err != nil { From 618bb14dba43e6bc494e05e452dcbb6e8eecfb46 Mon Sep 17 00:00:00 2001 From: Mario Di Miceli Date: Tue, 14 May 2024 09:44:40 +0200 Subject: [PATCH 2/6] add global flag --- application/configuration.go | 7 ++++--- bbl/main.go | 4 ++-- commands/usage.go | 11 ++++++----- config/global_flags.go | 17 +++++++++-------- terraform/binary_path.go | 24 ++++++++++++++---------- terraform/binary_path_test.go | 7 ++++--- terraform/cli.go | 18 ++++++++++-------- 7 files changed, 49 insertions(+), 39 deletions(-) diff --git a/application/configuration.go b/application/configuration.go index b6fbdc326..5d0a0ae92 100644 --- a/application/configuration.go +++ b/application/configuration.go @@ -3,9 +3,10 @@ package application import "github.com/cloudfoundry/bosh-bootloader/storage" type GlobalConfiguration struct { - StateDir string - Debug bool - Name string + StateDir string + Debug bool + Name string + UseTfLocalBinary bool } type StringSlice []string diff --git a/bbl/main.go b/bbl/main.go index 701e36db7..57423e68a 100644 --- a/bbl/main.go +++ b/bbl/main.go @@ -94,14 +94,14 @@ func main() { // Terraform terraformOutputBuffer := bytes.NewBuffer([]byte{}) dotTerraformDir := filepath.Join(appConfig.Global.StateDir, "terraform", ".terraform") - bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir) + bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary) var ( terraformCLI terraform.CLI out io.Writer ) if appConfig.Global.Debug { errBuffer := io.MultiWriter(os.Stderr, terraformOutputBuffer) - terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir) + terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary) out = os.Stdout } else { terraformCLI = bufferingCLI diff --git a/commands/usage.go b/commands/usage.go index 51da03d3c..82a54d024 100644 --- a/commands/usage.go +++ b/commands/usage.go @@ -13,11 +13,12 @@ Usage: bbl [GLOBAL OPTIONS] %s [OPTIONS] Global Options: - --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command - --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" - --debug [-d] Prints debugging output env:"BBL_DEBUG" - --version [-v] Prints version - --no-confirm [-n] No confirm + --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command + --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" + --debug [-d] Prints debugging output env:"BBL_DEBUG" + --version [-v] Prints version + --no-confirm [-n] No confirm + --use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY" %s ` CommandUsage = ` diff --git a/config/global_flags.go b/config/global_flags.go index eca6525a4..b33b488c5 100644 --- a/config/global_flags.go +++ b/config/global_flags.go @@ -1,14 +1,15 @@ package config type GlobalFlags struct { - Help bool `short:"h" long:"help"` - Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"` - Version bool `short:"v" long:"version"` - NoConfirm bool `short:"n" long:"no-confirm"` - StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"` - StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"` - EnvID string ` long:"name"` - IAAS string ` long:"iaas" env:"BBL_IAAS"` + Help bool `short:"h" long:"help"` + Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"` + Version bool `short:"v" long:"version"` + NoConfirm bool `short:"n" long:"no-confirm"` + StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"` + StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"` + EnvID string ` long:"name"` + IAAS string ` long:"iaas" env:"BBL_IAAS"` + UseTfLocalBinary bool `short:"u" long:"use-tf-local-binary" env:"BBL_USE_TF_LOCAL_BINARY"` AWSAccessKeyID string `long:"aws-access-key-id" env:"BBL_AWS_ACCESS_KEY_ID"` AWSSecretAccessKey string `long:"aws-secret-access-key" env:"BBL_AWS_SECRET_ACCESS_KEY"` diff --git a/terraform/binary_path.go b/terraform/binary_path.go index 3ab7d5eff..80d5e3583 100644 --- a/terraform/binary_path.go +++ b/terraform/binary_path.go @@ -28,28 +28,32 @@ type tfBinaryPathFs interface { } type Binary struct { - FS tfBinaryPathFs - EmbedData embed.FS - Path string + FS tfBinaryPathFs + EmbedData embed.FS + Path string + UseLocalBinary bool } //go:embed binary_dist var content embed.FS -func NewBinary() *Binary { +func NewBinary(tfUseLocalBinary bool) *Binary { fs := afero.Afero{Fs: afero.NewOsFs()} return &Binary{ - FS: fs, - Path: "binary_dist", - EmbedData: content, + FS: fs, + Path: "binary_dist", + EmbedData: content, + UseLocalBinary: tfUseLocalBinary, } } func (binary *Binary) BinaryPath() (string, error) { // if user has a terraform use it - userTerraform, err := exec.LookPath("terraform") - if err == nil && userTerraform != "" { - return userTerraform, nil + if binary.UseLocalBinary { + userTerraform, err := exec.LookPath(tfBinDataAssetName) + if err == nil && userTerraform != "" { + return userTerraform, nil + } } destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName) diff --git a/terraform/binary_path_test.go b/terraform/binary_path_test.go index 1f868d49f..2b7cc6805 100644 --- a/terraform/binary_path_test.go +++ b/terraform/binary_path_test.go @@ -39,9 +39,10 @@ var _ = Describe("BinaryPath", func() { fileSystem.ExistsCall.Returns.Bool = false binary = &terraform.Binary{ - Path: "testassets/success", - EmbedData: content, - FS: fileSystem, + Path: "testassets/success", + EmbedData: content, + FS: fileSystem, + UseLocalBinary: false, } }) diff --git a/terraform/cli.go b/terraform/cli.go index d4e619b2b..24235ccde 100644 --- a/terraform/cli.go +++ b/terraform/cli.go @@ -8,16 +8,18 @@ import ( ) type CLI struct { - errorBuffer io.Writer - outputBuffer io.Writer - tfDataDir string + errorBuffer io.Writer + outputBuffer io.Writer + tfDataDir string + tfUseLocalBinary bool } -func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string) CLI { +func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, tfUseLocalBinary bool) CLI { return CLI{ - errorBuffer: errorBuffer, - outputBuffer: outputBuffer, - tfDataDir: tfDataDir, + errorBuffer: errorBuffer, + outputBuffer: outputBuffer, + tfDataDir: tfDataDir, + tfUseLocalBinary: tfUseLocalBinary, } } @@ -26,7 +28,7 @@ func (c CLI) Run(stdout io.Writer, workingDirectory string, args []string) error } func (c CLI) RunWithEnv(stdout io.Writer, workingDirectory string, args []string, extraEnvVars []string) error { - path, err := NewBinary().BinaryPath() + path, err := NewBinary(c.tfUseLocalBinary).BinaryPath() if err != nil { return err } From f0efeb5b0f0099b0fbbbc6ea13232425014ed621 Mon Sep 17 00:00:00 2001 From: Mario Di Miceli Date: Tue, 14 May 2024 09:59:05 +0200 Subject: [PATCH 3/6] fix lint and unit tests --- commands/usage_test.go | 22 ++++++++++++---------- terraform/binary_path.go | 8 ++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/commands/usage_test.go b/commands/usage_test.go index a7f2ae83f..08c56ee5a 100644 --- a/commands/usage_test.go +++ b/commands/usage_test.go @@ -39,11 +39,12 @@ Usage: bbl [GLOBAL OPTIONS] COMMAND [OPTIONS] Global Options: - --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command - --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" - --debug [-d] Prints debugging output env:"BBL_DEBUG" - --version [-v] Prints version - --no-confirm [-n] No confirm + --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command + --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" + --debug [-d] Prints debugging output env:"BBL_DEBUG" + --version [-v] Prints version + --no-confirm [-n] No confirm + --use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY" Basic Commands: A good place to start up Deploys BOSH director on an IAAS, creates CF/Concourse load balancers. Updates existing director. @@ -84,11 +85,12 @@ Troubleshooting Commands: bbl [GLOBAL OPTIONS] my-command [OPTIONS] Global Options: - --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command - --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" - --debug [-d] Prints debugging output env:"BBL_DEBUG" - --version [-v] Prints version - --no-confirm [-n] No confirm + --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command + --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" + --debug [-d] Prints debugging output env:"BBL_DEBUG" + --version [-v] Prints version + --no-confirm [-n] No confirm + --use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY" [my-command command options] some message diff --git a/terraform/binary_path.go b/terraform/binary_path.go index 80d5e3583..ea1f765fc 100644 --- a/terraform/binary_path.go +++ b/terraform/binary_path.go @@ -50,10 +50,10 @@ func NewBinary(tfUseLocalBinary bool) *Binary { func (binary *Binary) BinaryPath() (string, error) { // if user has a terraform use it if binary.UseLocalBinary { - userTerraform, err := exec.LookPath(tfBinDataAssetName) - if err == nil && userTerraform != "" { - return userTerraform, nil - } + userTerraform, err := exec.LookPath(tfBinDataAssetName) + if err == nil && userTerraform != "" { + return userTerraform, nil + } } destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName) From c9224c67522605a2544932653afd81d756d41e71 Mon Sep 17 00:00:00 2001 From: Mario Di Miceli Date: Fri, 23 Aug 2024 17:14:58 +0200 Subject: [PATCH 4/6] set local terraform binary path --- application/configuration.go | 8 ++++---- bbl/main.go | 4 ++-- commands/usage.go | 6 +++--- commands/usage_test.go | 12 ++++++------ config/global_flags.go | 18 +++++++++--------- terraform/binary_path.go | 31 ++++++++++++++----------------- terraform/binary_path_test.go | 8 ++++---- terraform/cli.go | 20 ++++++++++---------- 8 files changed, 52 insertions(+), 55 deletions(-) diff --git a/application/configuration.go b/application/configuration.go index 5d0a0ae92..af41f6625 100644 --- a/application/configuration.go +++ b/application/configuration.go @@ -3,10 +3,10 @@ package application import "github.com/cloudfoundry/bosh-bootloader/storage" type GlobalConfiguration struct { - StateDir string - Debug bool - Name string - UseTfLocalBinary bool + StateDir string + Debug bool + Name string + TerraformBinary bool } type StringSlice []string diff --git a/bbl/main.go b/bbl/main.go index 57423e68a..904d385c4 100644 --- a/bbl/main.go +++ b/bbl/main.go @@ -94,14 +94,14 @@ func main() { // Terraform terraformOutputBuffer := bytes.NewBuffer([]byte{}) dotTerraformDir := filepath.Join(appConfig.Global.StateDir, "terraform", ".terraform") - bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary) + bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary) var ( terraformCLI terraform.CLI out io.Writer ) if appConfig.Global.Debug { errBuffer := io.MultiWriter(os.Stderr, terraformOutputBuffer) - terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary) + terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.TerraformBinary) out = os.Stdout } else { terraformCLI = bufferingCLI diff --git a/commands/usage.go b/commands/usage.go index 82a54d024..1d3c540b1 100644 --- a/commands/usage.go +++ b/commands/usage.go @@ -14,11 +14,11 @@ Usage: Global Options: --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command - --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" - --debug [-d] Prints debugging output env:"BBL_DEBUG" + --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" + --debug [-d] Prints debugging output env:"BBL_DEBUG" --version [-v] Prints version --no-confirm [-n] No confirm - --use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY" + --terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY" %s ` CommandUsage = ` diff --git a/commands/usage_test.go b/commands/usage_test.go index 08c56ee5a..2f42d0b2f 100644 --- a/commands/usage_test.go +++ b/commands/usage_test.go @@ -40,11 +40,11 @@ Usage: Global Options: --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command - --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" - --debug [-d] Prints debugging output env:"BBL_DEBUG" + --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" + --debug [-d] Prints debugging output env:"BBL_DEBUG" --version [-v] Prints version --no-confirm [-n] No confirm - --use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY" + --terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY" Basic Commands: A good place to start up Deploys BOSH director on an IAAS, creates CF/Concourse load balancers. Updates existing director. @@ -86,11 +86,11 @@ Troubleshooting Commands: Global Options: --help [-h] Prints usage. Use "bbl [command] --help" for more information about a command - --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" - --debug [-d] Prints debugging output env:"BBL_DEBUG" + --state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY" + --debug [-d] Prints debugging output env:"BBL_DEBUG" --version [-v] Prints version --no-confirm [-n] No confirm - --use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY" + --terraform-binary Path of a terraform binary (optional). If the file does not exist the embedded binary is used. env:"BBL_TERRAFORM_BINARY" [my-command command options] some message diff --git a/config/global_flags.go b/config/global_flags.go index b33b488c5..7ada1f3ac 100644 --- a/config/global_flags.go +++ b/config/global_flags.go @@ -1,15 +1,15 @@ package config type GlobalFlags struct { - Help bool `short:"h" long:"help"` - Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"` - Version bool `short:"v" long:"version"` - NoConfirm bool `short:"n" long:"no-confirm"` - StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"` - StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"` - EnvID string ` long:"name"` - IAAS string ` long:"iaas" env:"BBL_IAAS"` - UseTfLocalBinary bool `short:"u" long:"use-tf-local-binary" env:"BBL_USE_TF_LOCAL_BINARY"` + Help bool `short:"h" long:"help"` + Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"` + Version bool `short:"v" long:"version"` + NoConfirm bool `short:"n" long:"no-confirm"` + StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"` + StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"` + EnvID string ` long:"name"` + IAAS string ` long:"iaas" env:"BBL_IAAS"` + TerraformBinary string ` long:"terraform-binary" env:"BBL_TERRAFORM_BINARY"` AWSAccessKeyID string `long:"aws-access-key-id" env:"BBL_AWS_ACCESS_KEY_ID"` AWSSecretAccessKey string `long:"aws-secret-access-key" env:"BBL_AWS_SECRET_ACCESS_KEY"` diff --git a/terraform/binary_path.go b/terraform/binary_path.go index ea1f765fc..2a730ee60 100644 --- a/terraform/binary_path.go +++ b/terraform/binary_path.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "os" - "os/exec" "strconv" "strings" "time" @@ -28,36 +27,34 @@ type tfBinaryPathFs interface { } type Binary struct { - FS tfBinaryPathFs - EmbedData embed.FS - Path string - UseLocalBinary bool + FS tfBinaryPathFs + EmbedData embed.FS + Path string + TerraformBinary string } //go:embed binary_dist var content embed.FS -func NewBinary(tfUseLocalBinary bool) *Binary { +func NewBinary(terraformBinary string) *Binary { fs := afero.Afero{Fs: afero.NewOsFs()} return &Binary{ - FS: fs, - Path: "binary_dist", - EmbedData: content, - UseLocalBinary: tfUseLocalBinary, + FS: fs, + Path: "binary_dist", + EmbedData: content, + TerraformBinary: terraformBinary, } } func (binary *Binary) BinaryPath() (string, error) { - // if user has a terraform use it - if binary.UseLocalBinary { - userTerraform, err := exec.LookPath(tfBinDataAssetName) - if err == nil && userTerraform != "" { - return userTerraform, nil - } + // if user sets a terraform binary use it + exists, err := binary.FS.Exists(binary.TerraformBinary) + if err == nil && exists { + return binary.TerraformBinary, nil } destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName) - exists, err := binary.FS.Exists(destinationPath) + exists, err = binary.FS.Exists(destinationPath) if err != nil { return "", err } diff --git a/terraform/binary_path_test.go b/terraform/binary_path_test.go index 2b7cc6805..bca30037c 100644 --- a/terraform/binary_path_test.go +++ b/terraform/binary_path_test.go @@ -39,10 +39,10 @@ var _ = Describe("BinaryPath", func() { fileSystem.ExistsCall.Returns.Bool = false binary = &terraform.Binary{ - Path: "testassets/success", - EmbedData: content, - FS: fileSystem, - UseLocalBinary: false, + Path: "testassets/success", + EmbedData: content, + FS: fileSystem, + TerraformBinary: "", } }) diff --git a/terraform/cli.go b/terraform/cli.go index 24235ccde..43bc64e4a 100644 --- a/terraform/cli.go +++ b/terraform/cli.go @@ -8,18 +8,18 @@ import ( ) type CLI struct { - errorBuffer io.Writer - outputBuffer io.Writer - tfDataDir string - tfUseLocalBinary bool + errorBuffer io.Writer + outputBuffer io.Writer + tfDataDir string + terraformBinary string } -func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, tfUseLocalBinary bool) CLI { +func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, terraformBinary string) CLI { return CLI{ - errorBuffer: errorBuffer, - outputBuffer: outputBuffer, - tfDataDir: tfDataDir, - tfUseLocalBinary: tfUseLocalBinary, + errorBuffer: errorBuffer, + outputBuffer: outputBuffer, + tfDataDir: tfDataDir, + terraformBinary: terraformBinary, } } @@ -28,7 +28,7 @@ func (c CLI) Run(stdout io.Writer, workingDirectory string, args []string) error } func (c CLI) RunWithEnv(stdout io.Writer, workingDirectory string, args []string, extraEnvVars []string) error { - path, err := NewBinary(c.tfUseLocalBinary).BinaryPath() + path, err := NewBinary(c.terraformBinary).BinaryPath() if err != nil { return err } From ac76dfdff31181cbdc7d1d3a2b6dc1e60783d16e Mon Sep 17 00:00:00 2001 From: Mario Di Miceli Date: Fri, 23 Aug 2024 17:55:08 +0200 Subject: [PATCH 5/6] add unit tests --- terraform/binary_path.go | 10 ++++++---- terraform/binary_path_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/terraform/binary_path.go b/terraform/binary_path.go index 2a730ee60..7ad589141 100644 --- a/terraform/binary_path.go +++ b/terraform/binary_path.go @@ -48,13 +48,15 @@ func NewBinary(terraformBinary string) *Binary { func (binary *Binary) BinaryPath() (string, error) { // if user sets a terraform binary use it - exists, err := binary.FS.Exists(binary.TerraformBinary) - if err == nil && exists { - return binary.TerraformBinary, nil + if binary.TerraformBinary != "" { + exists, err := binary.FS.Exists(binary.TerraformBinary) + if err == nil && exists { + return binary.TerraformBinary, nil + } } destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName) - exists, err = binary.FS.Exists(destinationPath) + exists, err := binary.FS.Exists(destinationPath) if err != nil { return "", err } diff --git a/terraform/binary_path_test.go b/terraform/binary_path_test.go index bca30037c..e32b29e19 100644 --- a/terraform/binary_path_test.go +++ b/terraform/binary_path_test.go @@ -60,6 +60,33 @@ var _ = Describe("BinaryPath", func() { Expect(fileSystem.ChtimesCall.Receives.ModTime).To(Equal(modTime)) }) + Context("when a custom terraform binary path is set", func() { + BeforeEach(func() { + binary.TerraformBinary = "/some/custom/path/bbl-terraform" + }) + + Context("and the file exists", func() { + BeforeEach(func() { + fileSystem.ExistsCall.Returns.Bool = true + }) + + It("doesn't rewrite the file", func() { + res, err := binary.BinaryPath() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("/some/custom/path/bbl-terraform")) + Expect(fileSystem.WriteFileCall.CallCount).To(Equal(0)) + }) + }) + + Context("but the file does not exist", func() { + It("uses the embedded binary ", func() { + res, err := binary.BinaryPath() + Expect(err).NotTo(HaveOccurred()) + Expect(res).To(Equal("/some/tmp/path/bbl-terraform")) + }) + }) + }) + Context("when there is no my-terraform-binary in box", func() { BeforeEach(func() { binary.EmbedData = contentOnlyModTime From aa141d4ae24e61671b4b418f97fcecba7302084c Mon Sep 17 00:00:00 2001 From: Mario Di Miceli Date: Fri, 23 Aug 2024 18:05:41 +0200 Subject: [PATCH 6/6] fix golangci-lint errors --- application/logger.go | 5 ++++- terraform/executor.go | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/application/logger.go b/application/logger.go index 34208baa8..10e2e6106 100644 --- a/application/logger.go +++ b/application/logger.go @@ -76,7 +76,10 @@ func (l *Logger) Prompt(message string) bool { l.newline = true var proceed string - fmt.Fscanln(l.reader, &proceed) + _, err := fmt.Fscanln(l.reader, &proceed) + if err != nil { + return false + } proceed = strings.ToLower(proceed) if proceed == "yes" || proceed == "y" { diff --git a/terraform/executor.go b/terraform/executor.go index a21ba39a4..b75f6c800 100644 --- a/terraform/executor.go +++ b/terraform/executor.go @@ -156,7 +156,7 @@ func (e Executor) runTFCommandWithEnvs(args, envs []string) error { if e.debug { return err } - return fmt.Errorf(redactedError) + return fmt.Errorf("%s", redactedError) } return nil @@ -224,7 +224,7 @@ func (e Executor) Validate(credentials map[string]string) error { if e.debug { return err } - return fmt.Errorf(redactedError) + return fmt.Errorf("%s", redactedError) } return nil