-
Notifications
You must be signed in to change notification settings - Fork 5
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 "remove" command #60
Merged
+280
−15
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func doRemove(planPath string, upPath string) error { | ||
planFile, err := os.Open(planPath) | ||
if err != nil { | ||
return fmt.Errorf("remove: opening the plan file: %s", err) | ||
} | ||
defer planFile.Close() | ||
|
||
upFile, err := os.Create(upPath) | ||
if err != nil { | ||
return fmt.Errorf("remove: creating the up file: %s", err) | ||
} | ||
defer upFile.Close() | ||
|
||
toCreate, toDestroy, err := parse(planFile) | ||
if err != nil { | ||
return fmt.Errorf("remove: parsing plan: %s", err) | ||
} | ||
if toCreate.Size() > 0 { | ||
return fmt.Errorf("remove: plan contains resources to create: %v", | ||
sorted(toCreate.List())) | ||
} | ||
|
||
var bld strings.Builder | ||
generateRemoveScript(&bld, sorted(toDestroy.List())) | ||
_, err = upFile.WriteString(bld.String()) | ||
if err != nil { | ||
return fmt.Errorf("remove: writing script file: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func generateRemoveScript(wr io.Writer, addresses []string) { | ||
fmt.Fprintf(wr, `#! /bin/sh | ||
# DO NOT EDIT. Generated by https://github.com/pix4D/terravalet | ||
# This script will remove %d items. | ||
|
||
set -e | ||
|
||
`, len(addresses)) | ||
for _, addr := range addresses { | ||
fmt.Fprintf(wr, "terraform state rm '%s'\n", addr) | ||
} | ||
fmt.Fprintln(wr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-quicktest/qt" | ||
) | ||
|
||
func TestGenerateRemoveScript(t *testing.T) { | ||
addresses := []string{ | ||
`module.a.b.c["foo"]`, | ||
`module.a.b.d["foo.AN-"]`, | ||
} | ||
var bld strings.Builder | ||
want := `#! /bin/sh | ||
# DO NOT EDIT. Generated by https://github.com/pix4D/terravalet | ||
# This script will remove 2 items. | ||
|
||
set -e | ||
|
||
terraform state rm 'module.a.b.c["foo"]' | ||
terraform state rm 'module.a.b.d["foo.AN-"]' | ||
|
||
` | ||
|
||
generateRemoveScript(&bld, addresses) | ||
qt.Assert(t, qt.Equals(bld.String(), want)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,40 @@ | ||
github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa0= | ||
github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM= | ||
github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo= | ||
github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA= | ||
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw= | ||
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM= | ||
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= | ||
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw= | ||
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= | ||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/dexyk/stringosim v0.0.0-20170922105913-9d0b3e91a842 h1:FWXGhOthNyZKdK0YVyDrkg5dCXOfKvexcRG37U1v6AQ= | ||
github.com/dexyk/stringosim v0.0.0-20170922105913-9d0b3e91a842/go.mod h1:PfVoEMbmPGFArz22/wIefW9CzuQhdnE+C9ikEzJvb9Q= | ||
github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA= | ||
github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI= | ||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= | ||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= | ||
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI= | ||
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= | ||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= | ||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= | ||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= | ||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= | ||
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= | ||
github.com/scylladb/go-set v1.0.3-0.20200225121959-cc7b2070d91e h1:7q6NSFZDeGfvvtIRwBrU/aegEYJYmvev0cHAwo17zZQ= | ||
github.com/scylladb/go-set v1.0.3-0.20200225121959-cc7b2070d91e/go.mod h1:DkpGd78rljTxKAnTDPFqXSGxvETQnJyuSOQwsHycqfs= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= | ||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= | ||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// This file runs tests using the 'testscript' package. | ||
// To understand, see: | ||
// - https://github.com/rogpeppe/go-internal | ||
// - https://bitfieldconsulting.com/golang/test-scripts | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/rogpeppe/go-internal/testscript" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
// The commands map holds the set of command names, each with an associated | ||
// run function which should return the code to pass to os.Exit. | ||
// When [testscript.Run] is called, these commands are installed as regular | ||
// commands in the shell path, so can be invoked with "exec". | ||
os.Exit(testscript.RunMain(m, map[string]func() int{ | ||
"terravalet": Main, | ||
})) | ||
} | ||
|
||
func TestScript(t *testing.T) { | ||
testscript.Run(t, testscript.Params{ | ||
Dir: "testdata/script", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Terraform used the selected providers to generate the following execution | ||
plan. Resource actions are indicated with the following symbols: | ||
- destroy | ||
|
||
Terraform will perform the following actions: | ||
|
||
# module.github.github_branch_default.default["foo-c"] will be destroyed | ||
# (because key ["foo-c"] is not in for_each map) | ||
- resource "github_branch_default" "default" { | ||
|
||
# module.github.github_repository.repos["foo-c"] will be destroyed | ||
# (because key ["foo-c"] is not in for_each map) | ||
- resource "github_repository" "repos" { | ||
|
||
# module.github.github_repository_autolink_reference.repo_autolinks["foo-c.AN-"] will be destroyed | ||
# (because key ["foo-c.AN-"] is not in for_each map) | ||
- resource "github_repository_autolink_reference" "repo_autolinks" { | ||
|
||
# module.github.github_repository_autolink_reference.repo_autolinks["foo-c.CV-"] will be destroyed | ||
# (because key ["foo-c.CV-"] is not in for_each map) | ||
- resource "github_repository_autolink_reference" "repo_autolinks" { | ||
|
||
# module.github.github_repository_autolink_reference.repo_autolinks["foo-c.OPF-"] will be destroyed | ||
# (because key ["foo-c.OPF-"] is not in for_each map) | ||
- resource "github_repository_autolink_reference" "repo_autolinks" { | ||
|
||
# module.github.github_repository_collaborators.repo_collaborators["foo-c"] will be destroyed | ||
# (because key ["foo-c"] is not in for_each map) | ||
- resource "github_repository_collaborators" "repo_collaborators" { | ||
|
||
Plan: 0 to add, 0 to change, 6 to destroy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#! /bin/sh | ||
# DO NOT EDIT. Generated by https://github.com/pix4D/terravalet | ||
# | ||
# This script will detach 6 items. | ||
|
||
set -e | ||
|
||
terraform state rm 'module.github.github_branch_default.default["crane-camera-ccqc"]' | ||
|
||
terraform state rm 'module.github.github_repository.repos["crane-camera-ccqc"]' | ||
|
||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["crane-camera-ccqc.AN-"]' | ||
|
||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["crane-camera-ccqc.CV-"]' | ||
|
||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["crane-camera-ccqc.OPF-"]' | ||
|
||
terraform state rm 'module.github.github_repository_collaborators.repo_collaborators["crane-camera-ccqc"]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
exec terravalet remove --up=foo_up.sh --plan=detach.plan.txt | ||
! stderr . | ||
cmp foo_up.sh foo_up.sh.want | ||
|
||
-- foo_up.sh.want -- | ||
#! /bin/sh | ||
# DO NOT EDIT. Generated by https://github.com/pix4D/terravalet | ||
# This script will remove 6 items. | ||
|
||
set -e | ||
|
||
terraform state rm 'module.github.github_branch_default.default["foo"]' | ||
terraform state rm 'module.github.github_repository.repos["foo"]' | ||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["foo.AN-"]' | ||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["foo.CV-"]' | ||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["foo.OPF-"]' | ||
terraform state rm 'module.github.github_repository_collaborators.repo_collaborators["foo"]' | ||
|
||
-- detach.plan.txt -- | ||
Terraform used the selected providers to generate the following execution | ||
plan. Resource actions are indicated with the following symbols: | ||
- destroy | ||
|
||
Terraform will perform the following actions: | ||
|
||
# module.github.github_branch_default.default["foo"] will be destroyed | ||
# (because key ["foo"] is not in for_each map) | ||
- resource "github_branch_default" "default" { | ||
|
||
# module.github.github_repository.repos["foo"] will be destroyed | ||
# (because key ["foo"] is not in for_each map) | ||
- resource "github_repository" "repos" { | ||
|
||
# module.github.github_repository_autolink_reference.repo_autolinks["foo.AN-"] will be destroyed | ||
# (because key ["foo.AN-"] is not in for_each map) | ||
- resource "github_repository_autolink_reference" "repo_autolinks" { | ||
|
||
# module.github.github_repository_autolink_reference.repo_autolinks["foo.CV-"] will be destroyed | ||
# (because key ["foo.CV-"] is not in for_each map) | ||
- resource "github_repository_autolink_reference" "repo_autolinks" { | ||
|
||
# module.github.github_repository_autolink_reference.repo_autolinks["foo.OPF-"] will be destroyed | ||
# (because key ["foo.OPF-"] is not in for_each map) | ||
- resource "github_repository_autolink_reference" "repo_autolinks" { | ||
|
||
# module.github.github_repository_collaborators.repo_collaborators["foo"] will be destroyed | ||
# (because key ["foo"] is not in for_each map) | ||
- resource "github_repository_collaborators" "repo_collaborators" { | ||
|
||
Plan: 0 to add, 0 to change, 6 to destroy. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why there is 2 main / Main functions?
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.
@iAmoric
To understand, it helps to keep in mind that Go is strongly typed. This makes it easy to follow the flow.
A Go program wants a main function with name and signature
main()
. That is, it takes nothing and returns nothing. (This is not the same asNone
in Python ornil
in Go, it is really nothing).Then we have
Main() int
, which returns an integer. This could have been called anything. We call itMain
as convention, because it is doing everything themain()
is doing.Why we added it? Because of
See line 7 ? it calls this
Main()
and, see line 6, it is using its return code foros.Exit
.The power of
testscript
, introduced in this PR, is that when it sees line 7, it also compiles the test executable. Said in another way, it allows to drive integration tests directly from Go code with the standard Go tooling (go test
), ensuring that dependencies are built before the tests are run.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. So if I summarize roughly, you need
Main()
, which returns an integer, because you need a return value when usingTestMain
butmain()
returns nothing?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.
@iAmoric exactly.
Then we could ask ourselves why
main()
returns nothing. There are reasons, rooted in the fact that not all OSes are Unix. But this is another story :-)