Files
terraform-docs/terraform/resource.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

89 lines
2.5 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"
"strings"
"github.com/terraform-docs/terraform-docs/internal/types"
)
// Resource represents a managed or data type that is created by the module
type Resource struct {
Type string `json:"type" toml:"type" xml:"type" yaml:"type"`
Name string `json:"name" toml:"name" xml:"name" yaml:"name"`
ProviderName string `json:"provider" toml:"provider" xml:"provider" yaml:"provider"`
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:"-"`
}
// Spec returns the resource spec addresses a specific resource in the config.
// It takes the form: resource_type.resource_name[resource index]
// For more details, see:
// https://www.terraform.io/docs/cli/state/resource-addressing.html#resource-spec
func (r *Resource) Spec() string {
return r.ProviderName + "_" + r.Type + "." + r.Name
}
// GetMode returns normalized resource type as "resource" or "data source"
func (r *Resource) GetMode() string {
switch r.Mode {
case "managed":
return "resource"
case "data":
return "data source"
default:
return "invalid"
}
}
// URL returns a best guess at the URL for resource documentation
func (r *Resource) URL() string {
kind := ""
switch r.Mode {
case "managed":
kind = "resources"
case "data":
kind = "data-sources"
default:
return ""
}
if strings.Count(r.ProviderSource, "/") > 1 {
return ""
}
return fmt.Sprintf("https://registry.terraform.io/providers/%s/%s/docs/%s/%s", r.ProviderSource, r.Version, kind, r.Type)
}
func sortResourcesByType(x []*Resource) {
sort.Slice(x, func(i, j int) bool {
if x[i].Mode == x[j].Mode {
if x[i].Spec() == x[j].Spec() {
return x[i].Name <= x[j].Name
}
return x[i].Spec() < x[j].Spec()
}
return x[i].Mode > x[j].Mode
})
}
type resources []*Resource
func (rr resources) sort(enabled bool, by string) { //nolint:unparam
// always sort by type
sortResourcesByType(rr)
}