forked from terraform-docs/terraform-docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
120 lines (92 loc) · 3.41 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
114
115
116
117
118
119
120
package main
import (
"fmt"
"log"
"github.com/docopt/docopt.go"
"github.com/segmentio/terraform-docs/internal/pkg/doc"
"github.com/segmentio/terraform-docs/internal/pkg/print"
"github.com/segmentio/terraform-docs/internal/pkg/print/json"
markdown_document "github.com/segmentio/terraform-docs/internal/pkg/print/markdown/document"
markdown_table "github.com/segmentio/terraform-docs/internal/pkg/print/markdown/table"
"github.com/segmentio/terraform-docs/internal/pkg/print/pretty"
"github.com/segmentio/terraform-docs/internal/pkg/settings"
)
var version = "dev"
const usage = `
Usage:
terraform-docs [--no-required] [--no-sort | --sort-inputs-by-required] [--with-aggregate-type-defaults] [json | markdown | md] [document | table] <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
$ terraform-docs md table ./my-module
# Generate Markdown document of inputs and outputs
$ terraform-docs md document ./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
--no-required omit "Required" column when generating Markdown
--no-sort omit sorted rendering of inputs and outputs
--sort-inputs-by-required sort inputs by name and prints required inputs first
--with-aggregate-type-defaults print default values of aggregate types
--version print version
Types of Markdown:
document generate Markdown document of inputs and outputs
table generate Markdown tables of inputs and outputs (default)
`
func main() {
parser := &docopt.Parser{
HelpHandler: docopt.PrintHelpAndExit,
OptionsFirst: true,
SkipHelpFlags: false,
}
args, err := parser.ParseArgs(usage, nil, version)
if err != nil {
log.Fatal(err)
}
paths := args["<path>"].([]string)
document, err := doc.CreateFromPaths(paths)
if err != nil {
log.Fatal(err)
}
var printSettings settings.Settings
if !args["--no-required"].(bool) {
printSettings.Add(print.WithRequired)
}
if !args["--no-sort"].(bool) {
printSettings.Add(print.WithSortByName)
}
if args["--sort-inputs-by-required"].(bool) {
printSettings.Add(print.WithSortInputsByRequired)
}
if args["--with-aggregate-type-defaults"].(bool) {
printSettings.Add(print.WithAggregateTypeDefaults)
}
var out string
switch {
case args["markdown"].(bool), args["md"].(bool):
if args["document"].(bool) {
out, err = markdown_document.Print(document, printSettings)
} else {
out, err = markdown_table.Print(document, printSettings)
}
case args["json"].(bool):
out, err = json.Print(document, printSettings)
default:
out, err = pretty.Print(document, printSettings)
}
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
}