-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathgenerate.go
121 lines (104 loc) · 4.15 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cmd
import (
"flag"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-docs/internal/provider"
)
type generateCmd struct {
commonCmd
flagIgnoreDeprecated bool
flagProviderName string
flagRenderedProviderName string
flagBlocksSection bool
flagProviderDir string
flagProvidersSchema string
flagRenderedWebsiteDir string
flagExamplesDir string
flagWebsiteTmpDir string
flagWebsiteSourceDir string
tfVersion string
}
func (cmd *generateCmd) Synopsis() string {
return "generates a plugin website from code, templates, and examples"
}
func (cmd *generateCmd) Help() string {
strBuilder := &strings.Builder{}
longestName := 0
longestUsage := 0
cmd.Flags().VisitAll(func(f *flag.Flag) {
if len(f.Name) > longestName {
longestName = len(f.Name)
}
if len(f.Usage) > longestUsage {
longestUsage = len(f.Usage)
}
})
strBuilder.WriteString("\nUsage: tfplugindocs generate [<args>]\n\n")
cmd.Flags().VisitAll(func(f *flag.Flag) {
if f.DefValue != "" {
strBuilder.WriteString(fmt.Sprintf(" --%s <ARG> %s%s%s (default: %q)\n",
f.Name,
strings.Repeat(" ", longestName-len(f.Name)+2),
f.Usage,
strings.Repeat(" ", longestUsage-len(f.Usage)+2),
f.DefValue,
))
} else {
strBuilder.WriteString(fmt.Sprintf(" --%s <ARG> %s%s%s\n",
f.Name,
strings.Repeat(" ", longestName-len(f.Name)+2),
f.Usage,
strings.Repeat(" ", longestUsage-len(f.Usage)+2),
))
}
})
strBuilder.WriteString("\n")
return strBuilder.String()
}
func (cmd *generateCmd) Flags() *flag.FlagSet {
fs := flag.NewFlagSet("generate", flag.ExitOnError)
fs.BoolVar(&cmd.flagBlocksSection, "blocks-section", false, "render blocks in a separate section instead of including them with attributes in the required and optional sections.")
fs.StringVar(&cmd.flagProviderName, "provider-name", "", "provider name, as used in Terraform configurations; defaults to the --provider-dir short name (after removing `terraform-provider-` prefix)")
fs.StringVar(&cmd.flagProviderDir, "provider-dir", "", "relative or absolute path to the root provider code directory when running the command outside the root provider code directory")
fs.StringVar(&cmd.flagProvidersSchema, "providers-schema", "", "path to the providers schema JSON file, which contains the output of the terraform providers schema -json command. Setting this flag will skip building the provider and calling Terraform CLI")
fs.StringVar(&cmd.flagRenderedProviderName, "rendered-provider-name", "", "provider name, as generated in documentation (ex. page titles, ...)")
fs.StringVar(&cmd.flagRenderedWebsiteDir, "rendered-website-dir", "docs", "output directory based on provider-dir")
fs.StringVar(&cmd.flagExamplesDir, "examples-dir", "examples", "examples directory based on provider-dir")
fs.StringVar(&cmd.flagWebsiteTmpDir, "website-temp-dir", "", "temporary directory (used during generation)")
fs.StringVar(&cmd.flagWebsiteSourceDir, "website-source-dir", "templates", "templates directory based on provider-dir")
fs.StringVar(&cmd.tfVersion, "tf-version", "", "terraform binary version to download. If not provided, will look for a terraform binary in the local environment. If not found in the environment, will download the latest version of Terraform")
fs.BoolVar(&cmd.flagIgnoreDeprecated, "ignore-deprecated", false, "don't generate documentation for deprecated resources and data-sources")
return fs
}
func (cmd *generateCmd) Run(args []string) int {
fs := cmd.Flags()
err := fs.Parse(args)
if err != nil {
cmd.ui.Error(fmt.Sprintf("unable to parse flags: %s", err))
return 1
}
return cmd.run(cmd.runInternal)
}
func (cmd *generateCmd) runInternal() error {
err := provider.Generate(
cmd.ui,
cmd.flagProviderDir,
cmd.flagProviderName,
cmd.flagProvidersSchema,
cmd.flagRenderedProviderName,
cmd.flagRenderedWebsiteDir,
cmd.flagExamplesDir,
cmd.flagWebsiteTmpDir,
cmd.flagWebsiteSourceDir,
cmd.tfVersion,
cmd.flagIgnoreDeprecated,
cmd.flagBlocksSection,
)
if err != nil {
return fmt.Errorf("unable to generate website: %w", err)
}
return nil
}