Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
pkg/terraform: Unexport low-level functions
Browse files Browse the repository at this point in the history
ExecuteSync() and ExecuteAsync() are low level functions and are used
mainly internally by pkg/terraform.

Add a dedicated Output() method to the Executor API. Callers should
not have to allow on low-level functions to get outputs.
  • Loading branch information
johananl committed Aug 11, 2020
1 parent 0e469d0 commit e82451b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func ManualConfigPrompt(c *Config) terraform.ExecutionHook {
}

func readDNSEntries(ex *terraform.Executor) ([]dnsEntry, error) {
output, err := ex.ExecuteSync("output", "-json", "dns_entries")
output, err := ex.OutputBytes("dns_entries")
if err != nil {
return nil, errors.Wrap(err, "failed to get DNS entries")
}
Expand Down
26 changes: 18 additions & 8 deletions pkg/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (ex *Executor) executeVerbose(args ...string) error {
}

func (ex *Executor) execute(verbose bool, args ...string) error {
pid, done, err := ex.ExecuteAsync(args...)
pid, done, err := ex.executeAsync(args...)
if err != nil {
return fmt.Errorf(
"executing Terraform with arguments '%s' in directory %s: %w",
Expand Down Expand Up @@ -327,17 +327,17 @@ func (ex *Executor) LoadVars() (map[string]interface{}, error) {
return nil, errors.New("Could not parse config as JSON object")
}

// ExecuteAsync runs the given command and arguments against Terraform, and returns
// executeAsync runs the given command and arguments against Terraform, and returns
// an identifier that can be used to read the output of the process as it is
// executed and after.
//
// ExecuteAsync is non-blocking, and takes a lock in the execution path.
// This function is non-blocking, and takes a lock in the execution path.
// Locking is handled by Terraform itself.
//
// An error is returned if the Terraform binary could not be found, or if the
// Terraform call itself failed, in which case, details can be found in the
// output.
func (ex *Executor) ExecuteAsync(args ...string) (int, chan struct{}, error) {
func (ex *Executor) executeAsync(args ...string) (int, chan struct{}, error) {
cmd := ex.generateCommand(args...)
rPipe, wPipe := io.Pipe()
cmd.Stdout = wPipe
Expand Down Expand Up @@ -381,8 +381,8 @@ func (ex *Executor) ExecuteAsync(args ...string) (int, chan struct{}, error) {
return cmd.Process.Pid, done, nil
}

// ExecuteSync is like Execute, but synchronous.
func (ex *Executor) ExecuteSync(args ...string) ([]byte, error) {
// executeSync is like executeAsync, but synchronous.
func (ex *Executor) executeSync(args ...string) ([]byte, error) {
// Initialize the signal handler.
h := signalHandler(ex.logger)

Expand Down Expand Up @@ -412,14 +412,24 @@ func (ex *Executor) Plan() error {
// Output gets output value from Terraform in JSON format and tries to unmarshal it
// to a given struct.
func (ex *Executor) Output(key string, s interface{}) error {
o, err := ex.ExecuteSync("output", "-json", key)
o, err := ex.executeSync("output", "-json", key)
if err != nil {
return fmt.Errorf("failed getting Terraform output for key %q: %w", key, err)
}

return json.Unmarshal(o, s)
}

// OutputBytes returns the value of the Terraform output key in JSON format as a byte slice.
func (ex *Executor) OutputBytes(key string) ([]byte, error) {
o, err := ex.executeSync("output", "-json", key)
if err != nil {
return []byte{}, fmt.Errorf("getting Terraform output for key %q: %w", key, err)
}

return o, nil
}

// GenerateCommand prepares a Terraform command with the given arguments
// by setting up the command, configuration, working directory
// (so the files such as terraform.tfstate are stored at the right place) and
Expand Down Expand Up @@ -514,7 +524,7 @@ func (ex *Executor) logPath(id int) string {
}

func (ex *Executor) checkVersion() error {
vOutput, err := ex.ExecuteSync("--version")
vOutput, err := ex.executeSync("--version")
if err != nil {
return fmt.Errorf("Error checking Terraform version: %w", err)
}
Expand Down

0 comments on commit e82451b

Please sign in to comment.