Repository: segmentio/terraform-docs
Pull request #193 (2020-02-11 02:25:36)

Add tfconf.Options to load Module with
### Prerequisites

Put an `x` into the box(es) that apply:

- [ ] This pull request fixes a bug.
- [ ] This pull request adds a feature.
- [x] This pull request enhances existing functionality.
- [ ] This pull request introduces breaking change.

For more information, see the [Contributing Guide](https://github.com/segmentio/terraform-docs/tree/master/CONTRIBUTING.md).

### Description

This PR adds a new struct `tfconf.Options` to encapsulate required options for loading a Module.

### Issues Resolved

List any existing issues this pull request resolves.

### Checklist

Put an `x` into all boxes that apply:

- [ ] I have read the [Contributing Guidelines](https://github.com/segmentio/terraform-docs/tree/master/CONTRIBUTING.md).

#### Tests

- [ ] I have added tests to cover my changes.
- [ ] All tests pass when I run `make test`.

#### Documentation

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.

#### Code Style

- [ ] My code follows the code style of this project.



Patch:
diff --git a/cmd/json.go b/cmd/json.go
index 49fa0ac..a389a6a 100644
--- a/cmd/json.go
+++ b/cmd/json.go
@@ -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)
 		})
 	},
diff --git a/cmd/markdown.go b/cmd/markdown.go
index 312c8fd..9073100 100644
--- a/cmd/markdown.go
+++ b/cmd/markdown.go
@@ -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)
 		})
 	},
diff --git a/cmd/pretty.go b/cmd/pretty.go
index 6427c12..4a93797 100644
--- a/cmd/pretty.go
+++ b/cmd/pretty.go
@@ -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)
 		})
 	},
diff --git a/cmd/root.go b/cmd/root.go
index 938fcd6..2616e17 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -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)
 	}
diff --git a/cmd/yaml.go b/cmd/yaml.go
index 08bda57..f86fb44 100644
--- a/cmd/yaml.go
+++ b/cmd/yaml.go
@@ -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)
 		})
 	},
diff --git a/internal/pkg/tfconf/module.go b/internal/pkg/tfconf/module.go
index f81b5d0..918974c 100644
--- a/internal/pkg/tfconf/module.go
+++ b/internal/pkg/tfconf/module.go
@@ -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
 }
diff --git a/internal/pkg/tfconf/options.go b/internal/pkg/tfconf/options.go
new file mode 100644
index 0000000..e47bdea
--- /dev/null
+++ b/internal/pkg/tfconf/options.go
@@ -0,0 +1,6 @@
+package tfconf
+
+// Options contains required options to load a Module from path
+type Options struct {
+	Path string
+}
diff --git a/scripts/docs/generate.go b/scripts/docs/generate.go
index 7d9ffea..cacdfc9 100644
--- a/scripts/docs/generate.go
+++ b/scripts/docs/generate.go
@@ -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)
 	}


Test patch:
diff --git a/internal/pkg/testutil/testing.go b/internal/pkg/testutil/testing.go
index 47f8f8e..536b790 100644
--- a/internal/pkg/testutil/testing.go
+++ b/internal/pkg/testutil/testing.go
@@ -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
 	}