Extract leading comments for resources and modules

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
Khosrow Moossavi
2021-09-29 13:55:50 -04:00
parent 5f00545aac
commit f3c92384a1
26 changed files with 196 additions and 55 deletions

View File

@@ -66,14 +66,14 @@ func loadModuleItems(tfmodule *tfconfig.Module, config *print.Config) (*Module,
}
inputs, required, optional := loadInputs(tfmodule, config)
modulecalls := loadModulecalls(tfmodule)
modulecalls := loadModulecalls(tfmodule, config)
outputs, err := loadOutputs(tfmodule, config)
if err != nil {
return nil, err
}
providers := loadProviders(tfmodule, config)
requirements := loadRequirements(tfmodule)
resources := loadResources(tfmodule)
resources := loadResources(tfmodule, config)
return &Module{
Header: header,
@@ -241,16 +241,23 @@ func formatSource(s, v string) (source, version string) {
return source, version
}
func loadModulecalls(tfmodule *tfconfig.Module) []*ModuleCall {
func loadModulecalls(tfmodule *tfconfig.Module, config *print.Config) []*ModuleCall {
var modules = make([]*ModuleCall, 0)
var source, version string
for _, m := range tfmodule.ModuleCalls {
source, version = formatSource(m.Source, m.Version)
description := ""
if config.Settings.ReadComments {
description = loadComments(m.Pos.Filename, m.Pos.Line)
}
modules = append(modules, &ModuleCall{
Name: m.Name,
Source: source,
Version: version,
Name: m.Name,
Source: source,
Version: version,
Description: types.String(description),
Position: Position{
Filename: m.Pos.Filename,
Line: m.Pos.Line,
@@ -275,6 +282,7 @@ func loadOutputs(tfmodule *tfconfig.Module, config *print.Config) ([]*Output, er
if description == "" && config.Settings.ReadComments {
description = loadComments(o.Pos.Filename, o.Pos.Line)
}
output := &Output{
Name: o.Name,
Description: types.String(description),
@@ -284,6 +292,7 @@ func loadOutputs(tfmodule *tfconfig.Module, config *print.Config) ([]*Output, er
},
ShowValue: config.OutputValues.Enabled,
}
if config.OutputValues.Enabled {
output.Sensitive = values[output.Name].Sensitive
if values[output.Name].Sensitive {
@@ -401,7 +410,7 @@ func loadRequirements(tfmodule *tfconfig.Module) []*Requirement {
return requirements
}
func loadResources(tfmodule *tfconfig.Module) []*Resource {
func loadResources(tfmodule *tfconfig.Module, config *print.Config) []*Resource {
allResources := []map[string]*tfconfig.Resource{tfmodule.ManagedResources, tfmodule.DataResources}
discovered := make(map[string]*Resource)
@@ -421,6 +430,12 @@ func loadResources(tfmodule *tfconfig.Module) []*Resource {
rType := strings.TrimPrefix(r.Type, r.Provider.Name+"_")
key := fmt.Sprintf("%s.%s.%s.%s", r.Provider.Name, r.Mode, rType, r.Name)
description := ""
if config.Settings.ReadComments {
description = loadComments(r.Pos.Filename, r.Pos.Line)
}
discovered[key] = &Resource{
Type: rType,
Name: r.Name,
@@ -428,6 +443,7 @@ func loadResources(tfmodule *tfconfig.Module) []*Resource {
ProviderName: r.Provider.Name,
ProviderSource: source,
Version: types.String(version),
Description: types.String(description),
Position: Position{
Filename: r.Pos.Filename,
Line: r.Pos.Line,

View File

@@ -535,8 +535,10 @@ func TestLoadModulecalls(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
config := print.NewConfig()
module, _ := loadModule(filepath.Join("testdata", tt.path))
modulecalls := loadModulecalls(module)
modulecalls := loadModulecalls(module, config)
assert.Equal(tt.expected, len(modulecalls))
})

View File

@@ -15,15 +15,17 @@ import (
"sort"
terraformsdk "github.com/terraform-docs/plugin-sdk/terraform"
"github.com/terraform-docs/terraform-docs/internal/types"
"github.com/terraform-docs/terraform-docs/print"
)
// ModuleCall represents a submodule called by Terraform module.
type ModuleCall struct {
Name string `json:"name" toml:"name" xml:"name" yaml:"name"`
Source string `json:"source" toml:"source" xml:"source" yaml:"source"`
Version string `json:"version" toml:"version" xml:"version" yaml:"version"`
Position Position `json:"-" toml:"-" xml:"-" yaml:"-"`
Name string `json:"name" toml:"name" xml:"name" yaml:"name"`
Source string `json:"source" toml:"source" xml:"source" yaml:"source"`
Version string `json:"version" toml:"version" xml:"version" yaml:"version"`
Description types.String `json:"description" toml:"description" xml:"description" yaml:"description"`
Position Position `json:"-" toml:"-" xml:"-" yaml:"-"`
}
// FullName returns full name of the modulecall, with version if available

View File

@@ -27,6 +27,7 @@ type Resource struct {
ProviderSource string `json:"source" toml:"source" xml:"source" yaml:"source"`
Mode string `json:"mode" toml:"mode" xml:"mode" yaml:"mode"`
Version types.String `json:"version" toml:"version" xml:"version" yaml:"version"`
Description types.String `json:"description" toml:"description" xml:"description" yaml:"description"`
Position Position `json:"-" toml:"-" xml:"-" yaml:"-"`
}