mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 04:48:33 +07:00
Extract leading comments for resources and modules
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:"-"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user