Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new Tags flag #103

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -10,6 +10,7 @@ require (
github.com/mitchellh/cli v1.1.2
github.com/russross/blackfriday v1.6.0
github.com/zclconf/go-cty v1.10.0
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0
)

require (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -265,6 +265,7 @@ golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0 h1:Dh6fw+p6FyRl5x/FvNswO1ji0lIGzm3KP8Y9VkS9PTE=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
17 changes: 15 additions & 2 deletions internal/cmd/generate.go
@@ -1,29 +1,42 @@
package cmd

import (
"bytes"
"flag"
"fmt"

"github.com/hashicorp/terraform-plugin-docs/internal/provider"
"golang.org/x/tools/go/buildutil"
)

type generateCmd struct {
commonCmd

flagLegacySidebar bool
buildTags string
}

func (cmd *generateCmd) Synopsis() string {
return "generates a plugin website from code, templates, and examples for the current directory"
}

func (cmd *generateCmd) Help() string {
return `Usage: tfplugindocs generate`
buf := bytes.Buffer{}
flags := cmd.Flags()
flags.SetOutput(&buf)
// PrintDefaults implicitly prints to output
// thus our buffer
flags.PrintDefaults()
return fmt.Sprintf(`Usage: tfplugindocs generate [options]
Available options:
%s
`, buf.String())
}

func (cmd *generateCmd) Flags() *flag.FlagSet {
fs := flag.NewFlagSet("generate", flag.ExitOnError)
fs.BoolVar(&cmd.flagLegacySidebar, "legacy-sidebar", false, "generate the legacy .erb sidebar file")
fs.StringVar(&cmd.buildTags, "tags", "", buildutil.TagsFlagDoc)
return fs
}

Expand All @@ -39,7 +52,7 @@ func (cmd *generateCmd) Run(args []string) int {
}

func (cmd *generateCmd) runInternal() error {
err := provider.Generate(cmd.ui, cmd.flagLegacySidebar)
err := provider.Generate(cmd.ui, cmd.flagLegacySidebar, cmd.buildTags)
if err != nil {
return fmt.Errorf("unable to generate website: %w", err)
}
Expand Down
21 changes: 13 additions & 8 deletions internal/provider/generate.go
Expand Up @@ -72,8 +72,8 @@ var (

type generator struct {
legacySidebar bool

ui cli.Ui
tags string
ui cli.Ui
}

func (g *generator) infof(format string, a ...interface{}) {
Expand All @@ -84,11 +84,11 @@ func (g *generator) warnf(format string, a ...interface{}) {
g.ui.Warn(fmt.Sprintf(format, a...))
}

func Generate(ui cli.Ui, legacySidebar bool) error {
func Generate(ui cli.Ui, legacySidebar bool, tags string) error {
g := &generator{
legacySidebar: legacySidebar,

ui: ui,
tags: tags,
ui: ui,
}

ctx := context.Background()
Expand Down Expand Up @@ -150,7 +150,7 @@ func (g *generator) Generate(ctx context.Context) error {
}

g.infof("exporting schema from Terraform")
providerSchema, err := g.terraformProviderSchema(ctx, providerName)
providerSchema, err := g.terraformProviderSchema(ctx, providerName, g.tags)
if err != nil {
return err
}
Expand Down Expand Up @@ -354,6 +354,7 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj
g.infof("rendering templated website to static markdown")

err = filepath.Walk(websiteTmp, func(path string, info os.FileInfo, err error) error {

if info.IsDir() {
// skip directories
return nil
Expand Down Expand Up @@ -458,7 +459,7 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj
return nil
}

func (g *generator) terraformProviderSchema(ctx context.Context, providerName string) (*tfjson.ProviderSchema, error) {
func (g *generator) terraformProviderSchema(ctx context.Context, providerName string, tags string) (*tfjson.ProviderSchema, error) {
var err error

shortName := providerShortName(providerName)
Expand All @@ -481,7 +482,11 @@ func (g *generator) terraformProviderSchema(ctx context.Context, providerName st
case "windows":
outFile = outFile + ".exe"
}
buildCmd := exec.Command("go", "build", "-o", outFile)
args := []string{"build", "-o", outFile}
if tags != "" {
args = append(args, "-tags", tags)
}
buildCmd := exec.Command("go", args...)
// TODO: constrain env here to make it a little safer?
_, err = runCmd(buildCmd)
if err != nil {
Expand Down