Skip to content

Commit 5242263

Browse files
mdimicelijpalermo
authored andcommitted
add global flag
1 parent 2efd100 commit 5242263

File tree

7 files changed

+49
-39
lines changed

7 files changed

+49
-39
lines changed

application/configuration.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package application
33
import "github.com/cloudfoundry/bosh-bootloader/storage"
44

55
type GlobalConfiguration struct {
6-
StateDir string
7-
Debug bool
8-
Name string
6+
StateDir string
7+
Debug bool
8+
Name string
9+
UseTfLocalBinary bool
910
}
1011

1112
type StringSlice []string

bbl/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ func main() {
9494
// Terraform
9595
terraformOutputBuffer := bytes.NewBuffer([]byte{})
9696
dotTerraformDir := filepath.Join(appConfig.Global.StateDir, "terraform", ".terraform")
97-
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir)
97+
bufferingCLI := terraform.NewCLI(terraformOutputBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary)
9898
var (
9999
terraformCLI terraform.CLI
100100
out io.Writer
101101
)
102102
if appConfig.Global.Debug {
103103
errBuffer := io.MultiWriter(os.Stderr, terraformOutputBuffer)
104-
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir)
104+
terraformCLI = terraform.NewCLI(errBuffer, terraformOutputBuffer, dotTerraformDir, globals.UseTfLocalBinary)
105105
out = os.Stdout
106106
} else {
107107
terraformCLI = bufferingCLI

commands/usage.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ Usage:
1313
bbl [GLOBAL OPTIONS] %s [OPTIONS]
1414
1515
Global Options:
16-
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
17-
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
18-
--debug [-d] Prints debugging output env:"BBL_DEBUG"
19-
--version [-v] Prints version
20-
--no-confirm [-n] No confirm
16+
--help [-h] Prints usage. Use "bbl [command] --help" for more information about a command
17+
--state-dir [-s] Directory containing the bbl state env:"BBL_STATE_DIRECTORY"
18+
--debug [-d] Prints debugging output env:"BBL_DEBUG"
19+
--version [-v] Prints version
20+
--no-confirm [-n] No confirm
21+
--use-tf-local-binary [-u] Use the local terraform binary if it exists env:"BBL_USE_TF_LOCAL_BINARY"
2122
%s
2223
`
2324
CommandUsage = `

config/global_flags.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package config
22

33
type GlobalFlags struct {
4-
Help bool `short:"h" long:"help"`
5-
Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"`
6-
Version bool `short:"v" long:"version"`
7-
NoConfirm bool `short:"n" long:"no-confirm"`
8-
StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"`
9-
StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"`
10-
EnvID string ` long:"name"`
11-
IAAS string ` long:"iaas" env:"BBL_IAAS"`
4+
Help bool `short:"h" long:"help"`
5+
Debug bool `short:"d" long:"debug" env:"BBL_DEBUG"`
6+
Version bool `short:"v" long:"version"`
7+
NoConfirm bool `short:"n" long:"no-confirm"`
8+
StateDir string `short:"s" long:"state-dir" env:"BBL_STATE_DIRECTORY"`
9+
StateBucket string ` long:"state-bucket" env:"BBL_STATE_BUCKET"`
10+
EnvID string ` long:"name"`
11+
IAAS string ` long:"iaas" env:"BBL_IAAS"`
12+
UseTfLocalBinary bool `short:"u" long:"use-tf-local-binary" env:"BBL_USE_TF_LOCAL_BINARY"`
1213

1314
AWSAccessKeyID string `long:"aws-access-key-id" env:"BBL_AWS_ACCESS_KEY_ID"`
1415
AWSSecretAccessKey string `long:"aws-secret-access-key" env:"BBL_AWS_SECRET_ACCESS_KEY"`

terraform/binary_path.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,32 @@ type tfBinaryPathFs interface {
2828
}
2929

3030
type Binary struct {
31-
FS tfBinaryPathFs
32-
EmbedData embed.FS
33-
Path string
31+
FS tfBinaryPathFs
32+
EmbedData embed.FS
33+
Path string
34+
UseLocalBinary bool
3435
}
3536

3637
//go:embed binary_dist
3738
var content embed.FS
3839

39-
func NewBinary() *Binary {
40+
func NewBinary(tfUseLocalBinary bool) *Binary {
4041
fs := afero.Afero{Fs: afero.NewOsFs()}
4142
return &Binary{
42-
FS: fs,
43-
Path: "binary_dist",
44-
EmbedData: content,
43+
FS: fs,
44+
Path: "binary_dist",
45+
EmbedData: content,
46+
UseLocalBinary: tfUseLocalBinary,
4547
}
4648
}
4749

4850
func (binary *Binary) BinaryPath() (string, error) {
4951
// if user has a terraform use it
50-
userTerraform, err := exec.LookPath("terraform")
51-
if err == nil && userTerraform != "" {
52-
return userTerraform, nil
52+
if binary.UseLocalBinary {
53+
userTerraform, err := exec.LookPath(tfBinDataAssetName)
54+
if err == nil && userTerraform != "" {
55+
return userTerraform, nil
56+
}
5357
}
5458

5559
destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName)

terraform/binary_path_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ var _ = Describe("BinaryPath", func() {
3939
fileSystem.ExistsCall.Returns.Bool = false
4040

4141
binary = &terraform.Binary{
42-
Path: "testassets/success",
43-
EmbedData: content,
44-
FS: fileSystem,
42+
Path: "testassets/success",
43+
EmbedData: content,
44+
FS: fileSystem,
45+
UseLocalBinary: false,
4546
}
4647
})
4748

terraform/cli.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ import (
88
)
99

1010
type CLI struct {
11-
errorBuffer io.Writer
12-
outputBuffer io.Writer
13-
tfDataDir string
11+
errorBuffer io.Writer
12+
outputBuffer io.Writer
13+
tfDataDir string
14+
tfUseLocalBinary bool
1415
}
1516

16-
func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string) CLI {
17+
func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string, tfUseLocalBinary bool) CLI {
1718
return CLI{
18-
errorBuffer: errorBuffer,
19-
outputBuffer: outputBuffer,
20-
tfDataDir: tfDataDir,
19+
errorBuffer: errorBuffer,
20+
outputBuffer: outputBuffer,
21+
tfDataDir: tfDataDir,
22+
tfUseLocalBinary: tfUseLocalBinary,
2123
}
2224
}
2325

@@ -26,7 +28,7 @@ func (c CLI) Run(stdout io.Writer, workingDirectory string, args []string) error
2628
}
2729

2830
func (c CLI) RunWithEnv(stdout io.Writer, workingDirectory string, args []string, extraEnvVars []string) error {
29-
path, err := NewBinary().BinaryPath()
31+
path, err := NewBinary(c.tfUseLocalBinary).BinaryPath()
3032
if err != nil {
3133
return err
3234
}

0 commit comments

Comments
 (0)