mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 04:48:33 +07:00
refactor: Add tfconf.Options to load Module with (#193)
This commit is contained in:
@@ -11,7 +11,7 @@ var jsonCmd = &cobra.Command{
|
||||
Use: "json [PATH]",
|
||||
Short: "Generate JSON of inputs and outputs",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
doPrint(args, func(module *tfconf.Module) (string, error) {
|
||||
doPrint(args[0], func(module *tfconf.Module) (string, error) {
|
||||
return json.Print(module, settings)
|
||||
})
|
||||
},
|
||||
|
||||
@@ -13,7 +13,7 @@ var markdownCmd = &cobra.Command{
|
||||
Aliases: []string{"md"},
|
||||
Short: "Generate Markdown of inputs and outputs",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
doPrint(args, func(module *tfconf.Module) (string, error) {
|
||||
doPrint(args[0], func(module *tfconf.Module) (string, error) {
|
||||
return table.Print(module, settings)
|
||||
})
|
||||
},
|
||||
@@ -25,7 +25,7 @@ var mdTableCmd = &cobra.Command{
|
||||
Aliases: []string{"tbl"},
|
||||
Short: "Generate Markdown tables of inputs and outputs",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
doPrint(args, func(module *tfconf.Module) (string, error) {
|
||||
doPrint(args[0], func(module *tfconf.Module) (string, error) {
|
||||
return table.Print(module, settings)
|
||||
})
|
||||
},
|
||||
@@ -37,7 +37,7 @@ var mdDocumentCmd = &cobra.Command{
|
||||
Aliases: []string{"doc"},
|
||||
Short: "Generate Markdown document of inputs and outputs",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
doPrint(args, func(module *tfconf.Module) (string, error) {
|
||||
doPrint(args[0], func(module *tfconf.Module) (string, error) {
|
||||
return document.Print(module, settings)
|
||||
})
|
||||
},
|
||||
|
||||
@@ -11,7 +11,7 @@ var prettyCmd = &cobra.Command{
|
||||
Use: "pretty [PATH]",
|
||||
Short: "Generate colorized pretty of inputs and outputs",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
doPrint(args, func(module *tfconf.Module) (string, error) {
|
||||
doPrint(args[0], func(module *tfconf.Module) (string, error) {
|
||||
return pretty.Print(module, settings)
|
||||
})
|
||||
},
|
||||
|
||||
@@ -86,8 +86,11 @@ func FormatterCmds() []*cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
func doPrint(paths []string, fn func(*tfconf.Module) (string, error)) {
|
||||
module, err := tfconf.CreateModule(paths[0])
|
||||
func doPrint(path string, fn func(*tfconf.Module) (string, error)) {
|
||||
options := &tfconf.Options{
|
||||
Path: path,
|
||||
}
|
||||
module, err := tfconf.CreateModule(options)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ var yamlCmd = &cobra.Command{
|
||||
Use: "yaml [PATH]",
|
||||
Short: "Generate YAML of inputs and outputs",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
doPrint(args, func(module *tfconf.Module) (string, error) {
|
||||
doPrint(args[0], func(module *tfconf.Module) (string, error) {
|
||||
return yaml.Print(module, settings)
|
||||
})
|
||||
},
|
||||
|
||||
@@ -15,8 +15,10 @@ func GetExpected(goldenFile string) (*tfconf.Module, string, error) {
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
module, err := tfconf.CreateModule(path)
|
||||
options := &tfconf.Options{
|
||||
Path: path,
|
||||
}
|
||||
module, err := tfconf.CreateModule(options)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ type Module struct {
|
||||
Providers []*Provider `json:"providers" yaml:"providers"`
|
||||
RequiredInputs []*Input `json:"-" yaml:"-"`
|
||||
OptionalInputs []*Input `json:"-" yaml:"-"`
|
||||
|
||||
options *Options
|
||||
}
|
||||
|
||||
// HasInputs indicates if the document has inputs.
|
||||
@@ -67,10 +69,10 @@ func (m *Module) Sort(settings *print.Settings) {
|
||||
|
||||
// CreateModule returns new instance of Module with all the inputs and
|
||||
// outputs dircoverd from provided 'path' containing Terraform config
|
||||
func CreateModule(path string) (*Module, error) {
|
||||
mod := loadModule(path)
|
||||
func CreateModule(options *Options) (*Module, error) {
|
||||
mod := loadModule(options.Path)
|
||||
|
||||
header := readHeader(path)
|
||||
header := readHeader(options.Path)
|
||||
|
||||
var inputs = make([]*Input, 0, len(mod.Variables))
|
||||
var requiredInputs = make([]*Input, 0, len(mod.Variables))
|
||||
@@ -149,6 +151,8 @@ func CreateModule(path string) (*Module, error) {
|
||||
Providers: providers,
|
||||
RequiredInputs: requiredInputs,
|
||||
OptionalInputs: optionalInputs,
|
||||
|
||||
options: options,
|
||||
}
|
||||
return module, nil
|
||||
}
|
||||
|
||||
6
internal/pkg/tfconf/options.go
Normal file
6
internal/pkg/tfconf/options.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package tfconf
|
||||
|
||||
// Options contains required options to load a Module from path
|
||||
type Options struct {
|
||||
Path string
|
||||
}
|
||||
@@ -159,7 +159,10 @@ func printExample(buf *bytes.Buffer, name string) error {
|
||||
buf.WriteString("```\n\n")
|
||||
buf.WriteString("generates the following output:\n\n")
|
||||
|
||||
module, err := tfconf.CreateModule("./examples")
|
||||
options := &tfconf.Options{
|
||||
Path: "./examples",
|
||||
}
|
||||
module, err := tfconf.CreateModule(options)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user