Skip to content

Commit

Permalink
Make tfplugindocs location configurable with flag instead of PATH
Browse files Browse the repository at this point in the history
Add a -tfplugindocsPath command line argument to the parsing script to
make it a bit more robust than dynamically setting the PATH variable in
the Makefile. Defaults to local bin, as the Makefile expects, but I
still set the argument in the Makefile in case someone modifies the BIN
variable.
  • Loading branch information
bengesoff committed Feb 17, 2021
1 parent faca4d9 commit 255b34b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
3 changes: 1 addition & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ $(BIN)/%:
@echo "Installing tools from tools/tools.go"
@cat tools/tools.go | grep _ | awk -F '"' '{print $$2}' | GOBIN=$(BIN) xargs -tI {} go install {}

# Inject ./bin into PATH to allow scripts/generate-docs.go to access local tfplugindocs binary
generate-docs: $(BIN)/tfplugindocs
PATH=$(PATH):$(BIN) go run scripts/generate-docs.go
go run scripts/generate-docs.go -tfplugindocsPath=$(BIN)/tfplugindocs

validate-docs: $(BIN)/tfplugindocs
$(BIN)/tfplugindocs validate
Expand Down
22 changes: 19 additions & 3 deletions scripts/generate-docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package main

import (
"flag"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -57,6 +58,13 @@ type Page struct {
}

func main() {
tfplugindocsPath := flag.String("tfplugindocsPath", "bin/tfplugindocs", "location where tfplugindocs is installed")
flag.Parse()

if !tfPluginDocsExists(*tfplugindocsPath) {
log.Fatalf("tfplugindocs not found at '%s' - have you run the Makefile?", *tfplugindocsPath)
}

baseDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -185,11 +193,19 @@ func main() {

replaceTemplatesDir(tmplDir, tempDir)

runTFPluginDocs()
runTFPluginDocs(*tfplugindocsPath)

replaceTemplatesDir(tmplDir, tmplDir+"-backup")
}

func tfPluginDocsExists(tfplugindocsLocation string) bool {
stat, err := os.Stat(tfplugindocsLocation)
if os.IsNotExist(err) {
return false
}
return !stat.IsDir()
}

// getTemplate walks the templates directory filtering non-tmpl extension
// files, and parsing all the templates found (ensuring they must parse).
func getTemplate(tmplDir string) *template.Template {
Expand Down Expand Up @@ -341,8 +357,8 @@ func replaceTemplatesDir(tmplDir string, tempDir string) {
// consist of precompiled templates and that the original untouched templates
// will still exist in the /templates-backup directory ready to be restored
// once the /docs content has been generated.
func runTFPluginDocs() {
cmd := exec.Command("tfplugindocs", "generate")
func runTFPluginDocs(tfplugindocsLocation string) {
cmd := exec.Command(tfplugindocsLocation, "generate")
err := cmd.Run()
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 255b34b

Please sign in to comment.