-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
92 lines (78 loc) · 1.96 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"bufio"
"flag"
"fmt"
"github.com/saymedia/packer2terraform/packer2terraform"
"io"
"io/ioutil"
"log"
"os"
)
func help() {
log.Println(`Usage packer2terraform [options...]
packer2terraform turns Packer's machine-readable output into a Terraform-readable format.
Options:
-f Filename of the input CSV. Alternatively use STDIN.
-h This help information.
-template Filename of the template to use in the output.
Example:
packer -machine-readable build app.json | \
packer2terraform -template templates/amazon-ebs.hcl > app.tfvars
`)
}
func main() {
tmpl := flag.String("template", "", "a template file")
csv := flag.String("f", "", "a csv file")
helpMe := flag.Bool("h", false, "help")
flag.Parse()
if *helpMe {
help()
os.Exit(0)
}
// Read a file or use STDIN
var csvFile io.Reader
if len(*csv) > 0 {
f, err := os.Open(*csv)
if err != nil {
log.Fatalf("CSV file read failed %s", err)
}
csvFile = bufio.NewReader(f)
} else if stat, _ := os.Stdin.Stat(); stat.Mode()&os.ModeCharDevice == 0 {
// has STDIN data
csvFile = bufio.NewReader(os.Stdin)
} else {
// No input data
help()
os.Exit(0)
}
// Get the CSV as a string array
parsed, err := packer2terraform.ReadCSV(csvFile)
if err != nil {
log.Fatalf("CSV read failed %s", err)
}
// Extract the artifacts
artifacts, err := packer2terraform.ExtractArtifacts(parsed)
if err != nil {
// fmt.Errorf("Packer build failed: %s", err)
log.Fatalf("Packer build failed: %s", err)
}
// Print artifacts using a template
var templateString string
if len(*tmpl) == 0 {
templateString = packer2terraform.TemplateAmazonEBS
} else {
buf, err := ioutil.ReadFile(*tmpl)
if err != nil {
log.Fatalf("Template file read failed: %s", err)
}
templateString = string(buf)
}
doc, err := packer2terraform.ToTemplate(artifacts, templateString)
if err != nil {
log.Fatalf("Template render failed: %s", err)
}
fmt.Println(doc)
// Done
os.Exit(0)
}