terraform-exec icon indicating copy to clipboard operation
terraform-exec copied to clipboard

Open the API

Open till opened this issue 3 years ago • 3 comments

Hey 👋🏼

I wanted to ask for your thoughts on the "re-usability" of this tool.

I know that the maintainers/HashiCorp cannot implement everything that people want and it seems like adding commands is a long undertaking right now (from looking at the PRs). I understand that this tool may not be of highest priority etc., and people have something else to do.

Having said all of the above, would it be possible to e.g. open the API of the tool a little bit, so people can implement custom commands more easily downstream?

I am specifically looking at this which would already give me the ability to run arbitrary commands: https://github.com/hashicorp/terraform-exec/blob/main/tfexec/cmd.go#L173-L196

For example, I want to add a state pull and a state push command for my purposes (I understand the potential complications of editing Terraform state), but I can't re-use this now and I am already using terraform-exec in an internal tool for state recovery 🙈 . So I'd generally like to continue to use it for the convenience that's already there. Due to the methods not being public, it leads to copy/paste on my end.

I am copying parts of buildTerraformCmd for some re-use, but that doesn't seem ideal:

func (tf *Terraform) buildTerraformCmd(ctx context.Context, mergeEnv map[string]string, args ...string) *exec.Cmd {
	cmd := exec.Command(tf.execPath, args...)

	cmd.Env = tf.buildEnv(mergeEnv)
	cmd.Dir = tf.workingDir

	tf.logger.Printf("[INFO] running Terraform command: %s", cmd.String())

	return cmd
}

This leads to all of kinds of other code that I need to take parts from:

  • runTerraformCmdJSON
  • runTerraformCmd / runTerraformCmdJSON
  • ...

Any thoughts on allowing for this use?

till avatar Feb 19 '22 12:02 till

Thanks for the well-written feature request and your patience while we get the subcommands implemented.

Our aim with terraform-exec is to have support for all Terraform subcommands which make sense in this API. The library is intended to be opinionated about how you use those subcommands and flags. state pull and state push are perfectly reasonable and those PRs should be merged soon.

If tfexec had implemented all subcommands you want to use, would you still want this feature?

kmoe avatar Apr 12 '22 10:04 kmoe

@kmoe Probably not. At least I can't think of a use-case beyond extending or changing the API.

IMO it wouldn't hurt and it would allow for free use?

But assuming everything is implemented there are of course less reasons.

till avatar Apr 14 '22 09:04 till

supporting arbitrary arguments would enable some options having to code every possible options

as an example, I need to add the -compact-warnings on a plan but can't because it's not supported.

draeron avatar Feb 08 '23 16:02 draeron