docs: Enhance automatic document generation (#227)

* Enhance automatic document generation

* update contribuor guide

* fix broken link
This commit is contained in:
Khosrow Moossavi
2020-03-29 18:17:55 -04:00
committed by GitHub
parent 9043f268ad
commit 40bd96be44
23 changed files with 130 additions and 75 deletions

View File

@@ -22,28 +22,31 @@ import (
// that we wanted to inject custom "Example" section with generated output based on the "examples"
// folder.
var basedir = "/docs"
var formatdir = "/formats"
func main() {
for _, c := range cmd.FormatterCmds() {
err := generate(c, "./docs/formats")
if err != nil {
log.Fatal(err)
}
err := generate(cmd.RootCmd(), "", "FORMATS_GUIDE")
if err != nil {
log.Fatal(err)
}
}
func generate(cmd *cobra.Command, dir string) error {
func generate(cmd *cobra.Command, subdir string, basename string) error {
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue
}
if err := generate(c, dir); err != nil {
if c.Annotations["kind"] == "" || c.Annotations["kind"] != "formatter" {
continue
}
b := strings.Replace(strings.Replace(c.CommandPath(), " ", "-", -1), "terraform-docs-", "", -1)
if err := generate(c, formatdir, b); err != nil {
return err
}
}
cmdpath := strings.Replace(cmd.CommandPath(), "terraform-docs ", "", -1)
basename := strings.Replace(cmdpath, " ", "-", -1) + ".md"
filename := filepath.Join(dir, basename)
filename := filepath.Join("."+basedir, subdir, basename+".md")
f, err := os.Create(filename)
if err != nil {
return err
@@ -90,15 +93,20 @@ func generateMarkdown(cmd *cobra.Command, w io.Writer) error {
return err
}
err := printExample(buf, name)
if err != nil {
return err
if len(cmd.Commands()) == 0 {
if err := printExample(buf, name); err != nil {
return err
}
} else {
if err := printSeeAlso(buf, cmd.Commands()); err != nil {
return err
}
}
if !cmd.DisableAutoGenTag {
buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n")
}
_, err = buf.WriteTo(w)
_, err := buf.WriteTo(w)
return err
}
@@ -170,12 +178,12 @@ func printExample(buf *bytes.Buffer, name string) error {
Required: settings.SortByRequired,
},
}
tfmodule, err := module.LoadWithOptions(options)
if err != nil {
log.Fatal(err)
}
if printer := getPrinter(name, settings); printer != nil {
tfmodule, err := module.LoadWithOptions(options)
if err != nil {
log.Fatal(err)
}
output, err := printer.Print(tfmodule, settings)
if err != nil {
return err
@@ -193,3 +201,31 @@ func printExample(buf *bytes.Buffer, name string) error {
buf.WriteString("\n\n")
return nil
}
func printSeeAlso(buf *bytes.Buffer, children []*cobra.Command) error {
buf.WriteString("### SEE ALSO\n\n")
for _, child := range children {
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
continue
}
if child.Annotations["kind"] == "" || child.Annotations["kind"] != "formatter" {
continue
}
cname := child.CommandPath()
link := strings.Replace(strings.Replace(cname, " ", "-", -1)+".md", "terraform-docs-", "", -1)
buf.WriteString(fmt.Sprintf("* [%s](%s%s/%s)\t - %s\n", cname, basedir, formatdir, link, child.Short))
for _, c := range child.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue
}
if c.Annotations["kind"] == "" || c.Annotations["kind"] != "formatter" {
continue
}
cname := c.CommandPath()
link := strings.Replace(strings.Replace(cname, " ", "-", -1)+".md", "terraform-docs-", "", -1)
buf.WriteString(fmt.Sprintf(" * [%s](%s%s/%s)\t - %s\n", cname, basedir, formatdir, link, c.Short))
}
}
buf.WriteString("\n")
return nil
}