-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fe05f1
commit 395f4d3
Showing
12 changed files
with
243 additions
and
51 deletions.
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
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,30 @@ | ||
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
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", | ||
}) | ||
} |
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-c"]' | ||
terraform state rm 'module.github.github_repository.repos["foo-c"]' | ||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["foo-c.AN-"]' | ||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["foo-c.CV-"]' | ||
terraform state rm 'module.github.github_repository_autolink_reference.repo_autolinks["foo-c.OPF-"]' | ||
terraform state rm 'module.github.github_repository_collaborators.repo_collaborators["foo-c"]' | ||
|
||
-- 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-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,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"]' |