-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add StateList #206
Add StateList #206
Conversation
@@ -4,6 +4,16 @@ import ( | |||
"encoding/json" | |||
) | |||
|
|||
// AddressOption represents an address. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There may be opinions regarding how this is named, or even implemented. terraform state show
is documented as taking zero or more addresses as options. As such I added an AddressOption, which ends up getting applied (for StateList
) as positional arguments after all the other options.
I'm more than happy to change how this is implemented if there are other suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this PR as well.
I have the same question for this command as in your other PR:
Could you share your use case for this command in the context of automation?
Keep in mind that this command (unlike most others we integrated) does not provide machine-readable (JSON) output and AFAIK therefore doesn't provide any compatibility guarantees for the output format.
If we have a use case though we could propose enhancement of Terraform CLI so that it does offer JSON output - this would then make the integration much easier here.
Use case described here on #205. I'll also mention that as these two PRs are related in terms of how I would use them, and because they're both being submitted at the same time, I'm more than happy to combine them into one. I kept them split to hopefully make them simpler to review and also to allow you flexibility in reviewing them as separate concepts. |
As discussed in the comments of #205, I think it makes sense for this to wait until |
Yep, I will be doing some exploratory work which will likely result adding JSON output for more commands and I will certainly use your use case in the process to advocate for I will let you know here once any relevant PR is ready upstream. Thanks. |
As mentioned in #205 (comment) the use case can also be satisfied with existing functionality. See below. $ terraform state list
random_pet.adam
random_pet.basil
random_pet.claire which is equivalent to $ terraform show -json | jq -r '.values.root_module.resources[].address'
random_pet.adam
random_pet.basil
random_pet.claire and similarly to package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"github.com/hashicorp/terraform-exec/tfexec"
)
func main() {
workDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
execPath, err := exec.LookPath("terraform")
if err != nil {
log.Fatal(err)
}
tf, err := tfexec.NewTerraform(workDir, execPath)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
state, err := tf.Show(ctx)
if err != nil {
log.Fatal(err)
}
for _, resource := range state.Values.RootModule.Resources {
fmt.Println(resource.Address)
}
} I hope that helps. |
This PR adds the StateList command.