-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmdremove.go
54 lines (45 loc) · 1.16 KB
/
cmdremove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)
}