From 5cbcf28ca2b333fa551de2b9f52b3248d94a48bc Mon Sep 17 00:00:00 2001 From: guineveresaenger Date: Wed, 27 Nov 2024 17:28:04 -0800 Subject: [PATCH] Pass a local docs path to the local provider gen --- dynamic/info.go | 2 +- dynamic/main.go | 53 +++++++++++++++++-------------- dynamic/parameterize/args.go | 10 +++++- dynamic/parameterize/args_test.go | 17 ++++++++++ 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/dynamic/info.go b/dynamic/info.go index 7c5d9e530..19751e477 100644 --- a/dynamic/info.go +++ b/dynamic/info.go @@ -84,7 +84,7 @@ func providerInfo(ctx context.Context, p run.Provider, value parameterize.Value) }, } // Add presumed best-effort GitHub org to the provider info. - // We do not yet handle full docsgen for a local dynamic provider so we do not set the GitHubOrg field in that case. + // We do not set the GitHubOrg field for a local dynamic provider. if value.Remote != nil { // https://github.com/opentofu/registry/issues/1337: // Due to discrepancies in the registry protocol/implementation, diff --git a/dynamic/main.go b/dynamic/main.go index 8b1de3205..36880f820 100644 --- a/dynamic/main.go +++ b/dynamic/main.go @@ -18,6 +18,9 @@ import ( "context" "encoding/json" "fmt" + "os" + "os/exec" + "github.com/blang/semver" "github.com/opentofu/opentofu/shim/run" "github.com/pulumi/pulumi/pkg/v3/codegen/schema" @@ -25,8 +28,6 @@ import ( "github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors" "github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin" "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" - "os" - "os/exec" "github.com/pulumi/pulumi-terraform-bridge/dynamic/parameterize" "github.com/pulumi/pulumi-terraform-bridge/dynamic/version" @@ -65,19 +66,12 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) { var fullDocs bool metadata = pfbridge.ProviderMetadata{ XGetSchema: func(ctx context.Context, req plugin.GetSchemaRequest) ([]byte, error) { - // By default, only read in docs from TF schema - var schemaDocsOnly = true - // If full docs is expected set schemaDocsOnly to false - if fullDocs { - schemaDocsOnly = false - } - packageSchema, err := tfgen.GenerateSchemaWithOptions(tfgen.GenerateSchemaOptions{ ProviderInfo: info, DiagnosticsSink: diag.DefaultSink(os.Stdout, os.Stderr, diag.FormatOptions{ Color: colors.Always, }), - XInMemoryDocs: schemaDocsOnly, + XInMemoryDocs: !fullDocs, }) if err != nil { return nil, err @@ -154,23 +148,36 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) { if err != nil { return plugin.ParameterizeResponse{}, err } - fullDocs = args.Remote.Docs - if fullDocs { - // Write the upstream files at this version to a temporary directory - tmpDir, err := os.MkdirTemp("", "upstreamRepoDir") - if err != nil { - return plugin.ParameterizeResponse{}, err + + if args.Remote != nil { + fullDocs = args.Remote.Docs + if fullDocs { + // Write the upstream files at this version to a temporary directory + tmpDir, err := os.MkdirTemp("", "upstreamRepoDir") + if err != nil { + return plugin.ParameterizeResponse{}, err + } + versionWithPrefix := "v" + info.Version + ghRepo := "https://github.com/" + info.GitHubOrg + "/terraform-provider-" + info.Name + + cmd := exec.Command( + "git", "clone", "--depth", "1", "-b", versionWithPrefix, ghRepo, tmpDir, + ) + err = cmd.Run() + if err != nil { + return plugin.ParameterizeResponse{}, err + } + info.UpstreamRepoPath = tmpDir } - versionWithPrefix := "v" + info.Version - ghRepo := "https://github.com/" + info.GitHubOrg + "/terraform-provider-" + info.Name + } - cmd := exec.Command("git", "clone", "--depth", "1", "-b", versionWithPrefix, ghRepo, tmpDir) - err = cmd.Run() - if err != nil { - return plugin.ParameterizeResponse{}, err + if args.Local != nil { + if args.Local.DocsLocation != "" { + info.UpstreamRepoPath = args.Local.DocsLocation + fullDocs = true } - info.UpstreamRepoPath = tmpDir } + return plugin.ParameterizeResponse{ Name: p.Name(), Version: v, diff --git a/dynamic/parameterize/args.go b/dynamic/parameterize/args.go index ffe5dafa6..db99b98cc 100644 --- a/dynamic/parameterize/args.go +++ b/dynamic/parameterize/args.go @@ -39,14 +39,22 @@ type RemoteArgs struct { type LocalArgs struct { // Path is the path to the provider binary. It can be relative or absolute. Path string + // DocsLocation is the path to the provider documentation. + DocsLocation string } func ParseArgs(args []string) (Args, error) { // Check for a leading '.' or '/' to indicate a path + if len(args) >= 1 && (strings.HasPrefix(args[0], "./") || strings.HasPrefix(args[0], "/")) { if len(args) > 1 { - return Args{}, fmt.Errorf("path based providers are only parameterized by 1 argument: ") + docsArg := args[1] + docsLocation, found := strings.CutPrefix(docsArg, "docsLocation=") + if !found { + return Args{}, fmt.Errorf("path based providers are only parameterized by 2 arguments: [docsLocation]") + } + return Args{Local: &LocalArgs{Path: args[0], DocsLocation: docsLocation}}, nil } return Args{Local: &LocalArgs{Path: args[0]}}, nil } diff --git a/dynamic/parameterize/args_test.go b/dynamic/parameterize/args_test.go index 78f1c1c1b..e45e8ffb0 100644 --- a/dynamic/parameterize/args_test.go +++ b/dynamic/parameterize/args_test.go @@ -35,6 +35,23 @@ func TestParseArgs(t *testing.T) { args: []string{"./my-provider"}, expect: Args{Local: &LocalArgs{Path: "./my-provider"}}, }, + { + name: "local too many args", + args: []string{"./my-provider", "nonsense"}, + errMsg: autogold.Expect( + "path based providers are only parameterized by 2 arguments: [docsLocation]", + ), + }, + { + name: "local with docs location", + args: []string{"./my-provider", "docsLocation=./my-provider"}, + expect: Args{ + Local: &LocalArgs{ + Path: "./my-provider", + DocsLocation: "./my-provider", + }, + }, + }, { name: "remote", args: []string{"my-registry.io/typ"},