forked from terraform-docs/terraform-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (86 loc) · 2.17 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/segmentio/terraform-docs/doc"
"github.com/segmentio/terraform-docs/print"
"github.com/tj/docopt"
)
var version = "v0.2.0"
const usage = `
Usage:
terraform-docs [--no-required] [json | md | markdown] <path>...
terraform-docs -h | --help
Examples:
# View inputs and outputs
$ terraform-docs ./my-module
# View inputs and outputs for variables.tf and outputs.tf only
$ terraform-docs variables.tf outputs.tf
# Generate a JSON of inputs and outputs
$ terraform-docs json ./my-module
# Generate markdown tables of inputs and outputs
$ terraform-docs md ./my-module
# Generate markdown tables of inputs and outputs, but don't print "Required" column
$ terraform-docs --no-required md ./my-module
# Generate markdown tables of inputs and outputs for the given module and ../config.tf
$ terraform-docs md ./my-module ../config.tf
Options:
-h, --help show help information
`
func main() {
args, err := docopt.Parse(usage, nil, true, version, true)
if err != nil {
log.Fatal(err)
}
var names []string
paths := args["<path>"].([]string)
for _, p := range paths {
pi, err := os.Stat(p)
if err != nil {
log.Fatal(err)
}
if !pi.IsDir() {
names = append(names, p)
continue
}
files, err := filepath.Glob(fmt.Sprintf("%s/*.tf", p))
if err != nil {
log.Fatal(err)
}
names = append(names, files...)
}
files := make(map[string]*ast.File, len(names))
for _, name := range names {
buf, err := ioutil.ReadFile(name)
if err != nil {
log.Fatal(err)
}
f, err := hcl.ParseBytes(buf)
if err != nil {
log.Fatal(err)
}
files[name] = f
}
doc := doc.Create(files)
printRequired := !args["--no-required"].(bool)
var out string
switch {
case args["markdown"].(bool):
out, err = print.Markdown(doc, printRequired)
case args["md"].(bool):
out, err = print.Markdown(doc, printRequired)
case args["json"].(bool):
out, err = print.JSON(doc)
default:
out, err = print.Pretty(doc)
}
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
}