forked from hashicorp/terraform-config-inspect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (48 loc) · 1.09 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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/HeadspaceMeditation/terraform-config-inspect/tfconfig"
flag "github.com/spf13/pflag"
)
var showJSON = flag.Bool("json", false, "produce JSON-formatted output")
var showVersion = flag.Bool("version", false, "print version of terraform-config-inspect")
func main() {
flag.Parse()
if *showVersion {
fmt.Println("0.3.1")
return
}
var dir string
if flag.NArg() > 0 {
dir = flag.Arg(0)
} else {
dir = "."
}
module, _ := tfconfig.LoadModule(dir)
if *showJSON {
showModuleJSON(module)
} else {
showModuleMarkdown(module)
}
if module.Diagnostics.HasErrors() {
os.Exit(1)
}
}
func showModuleJSON(module *tfconfig.Module) {
j, err := json.MarshalIndent(module, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "error producing JSON: %s\n", err)
os.Exit(2)
}
os.Stdout.Write(j)
os.Stdout.Write([]byte{'\n'})
}
func showModuleMarkdown(module *tfconfig.Module) {
err := tfconfig.RenderMarkdown(os.Stdout, module)
if err != nil {
fmt.Fprintf(os.Stderr, "error rendering template: %s\n", err)
os.Exit(2)
}
}