refactor: Add tfconf.Options to load Module with (#193)

This commit is contained in:
Khosrow Moossavi
2020-02-11 14:56:02 -05:00
committed by GitHub
parent 54ab7f9bbb
commit d4a0663909
9 changed files with 32 additions and 14 deletions

View File

@@ -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)
})
},

View File

@@ -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)
})
},

View File

@@ -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)
})
},

View File

@@ -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)
}

View File

@@ -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)
})
},

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -0,0 +1,6 @@
package tfconf
// Options contains required options to load a Module from path
type Options struct {
Path string
}

View File

@@ -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)
}