Skip to content

Commit

Permalink
add global flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mdimiceli committed May 14, 2024
1 parent 17e0c5b commit 34949d7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 39 deletions.
7 changes: 4 additions & 3 deletions application/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions bbl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions commands/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
17 changes: 9 additions & 8 deletions config/global_flags.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand Down
24 changes: 14 additions & 10 deletions terraform/binary_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 53 in terraform/binary_path.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

File is not `goimports`-ed (goimports)

Check failure on line 53 in terraform/binary_path.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not `goimports`-ed (goimports)

Check failure on line 53 in terraform/binary_path.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

File is not `goimports`-ed (goimports)

Check failure on line 53 in terraform/binary_path.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

File is not `goimports`-ed (goimports)
if err == nil && userTerraform != "" {
return userTerraform, nil
}
}

destinationPath := fmt.Sprintf("%s/%s", binary.FS.GetTempDir(os.TempDir()), bblTfBinaryName)
Expand Down
7 changes: 4 additions & 3 deletions terraform/binary_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
})

Expand Down
18 changes: 10 additions & 8 deletions terraform/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand Down

0 comments on commit 34949d7

Please sign in to comment.