Files
terraform-docs/terraform/modulecall.go
Khosrow Moossavi 4a9ffe7c33 Move plugin-sdk to in-tree in core repository
Moving terraform-docs/plugin-sdk standalone module to in-tree, because
maintaining both of them, specifically if anything needs to be added to
Config, or terraform will required dual effort on both repository. As
such now everything is consolidated under one repository. Example usage
for plugin developer after this move is as follow:

```go
package main

import (
	_ "embed" //nolint

	"github.com/terraform-docs/terraform-docs/plugin"
	"github.com/terraform-docs/terraform-docs/print"
	"github.com/terraform-docs/terraform-docs/template"
	"github.com/terraform-docs/terraform-docs/terraform"
)

func main() {
	plugin.Serve(&plugin.ServeOpts{
		Name:    "template",
		Version: "0.1.0",
		Printer: printer,
	})
}

//go:embed sections.tmpl
var tplCustom []byte

// Print the custom format template. You have all the flexibility to generate
// the output however you choose to.
func printer(config *print.Config, module *terraform.Module) (string, error) {
	tpl := template.New(config,
		&template.Item{Name: "custom", Text: string(tplCustom)},
	)

	rendered, err := tpl.Render("custom", module)
	if err != nil {
		return "", err
	}

	return rendered, nil
}
```

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-10-05 18:54:49 -04:00

75 lines
1.9 KiB
Go

/*
Copyright 2021 The terraform-docs Authors.
Licensed under the MIT license (the "License"); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at the LICENSE file in
the root directory of this source tree.
*/
package terraform
import (
"fmt"
"sort"
"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"`
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
func (mc *ModuleCall) FullName() string {
if mc.Version != "" {
return fmt.Sprintf("%s,%s", mc.Source, mc.Version)
}
return mc.Source
}
func sortModulecallsByName(x []*ModuleCall) {
sort.Slice(x, func(i, j int) bool {
return x[i].Name < x[j].Name
})
}
func sortModulecallsBySource(x []*ModuleCall) {
sort.Slice(x, func(i, j int) bool {
if x[i].Source == x[j].Source {
return x[i].Name < x[j].Name
}
return x[i].Source < x[j].Source
})
}
func sortModulecallsByPosition(x []*ModuleCall) {
sort.Slice(x, func(i, j int) bool {
return x[i].Position.Filename < x[j].Position.Filename || x[i].Position.Line < x[j].Position.Line
})
}
type modulecalls []*ModuleCall
func (mm modulecalls) sort(enabled bool, by string) {
if !enabled {
sortModulecallsByPosition(mm)
} else {
switch by {
case print.SortName, print.SortRequired:
sortModulecallsByName(mm)
case print.SortType:
sortModulecallsBySource(mm)
default:
sortModulecallsByPosition(mm)
}
}
}