Skip to content

Commit

Permalink
Use temporary directory
Browse files Browse the repository at this point in the history
  • Loading branch information
guineveresaenger committed Nov 27, 2024
1 parent a336743 commit 27033ba
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
31 changes: 15 additions & 16 deletions dynamic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"

"github.com/blang/semver"
"github.com/opentofu/opentofu/shim/run"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"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"
Expand Down Expand Up @@ -158,19 +156,20 @@ func initialSetup() (info.Provider, pfbridge.ProviderMetadata, func() error) {
}
fullDocs = args.Remote.Docs
if fullDocs {
upstreamRepoDir := "terraform-provider-" + info.Name + "-v" + info.Version
// Only clone if the directory doesn't exist in the expected location
if _, err := os.Stat(upstreamRepoDir); errors.Is(err, os.ErrNotExist) {
versionWithPrefix := "v" + info.Version
ghRepo := "https://github.com/" + info.GitHubOrg + "/terraform-provider-" + info.Name

cmd := exec.Command("git", "clone", "--depth", "1", "-b", versionWithPrefix, ghRepo, upstreamRepoDir)
err = cmd.Run()
if err != nil {
return plugin.ParameterizeResponse{}, err
}
// 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 = upstreamRepoDir
info.UpstreamRepoPath = tmpDir
}
return plugin.ParameterizeResponse{
Name: p.Name(),
Expand Down
2 changes: 1 addition & 1 deletion dynamic/parameterize/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ func ParseArgs(args []string) (Args, error) {
remote.Name = args[0]
return Args{Remote: &remote}, nil
default:
return Args{}, fmt.Errorf("expected to be parameterized by 1-3 arguments: <name> [version] fullDocs")
return Args{}, fmt.Errorf("expected to be parameterized by 1-3 arguments: <name> [version] [fullDocs]")
}
}
4 changes: 2 additions & 2 deletions dynamic/parameterize/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func TestParseArgs(t *testing.T) {
{
name: "no args",
args: []string{},
errMsg: autogold.Expect("expected to be parameterized by 1-3 arguments: <name> [version] fullDocs"),
errMsg: autogold.Expect("expected to be parameterized by 1-3 arguments: <name> [version] [fullDocs]"),
},
{
name: "too many args",
args: []string{"arg1", "arg2", "arg3", "arg4"},
errMsg: autogold.Expect("expected to be parameterized by 1-3 arguments: <name> [version] fullDocs"),
errMsg: autogold.Expect("expected to be parameterized by 1-3 arguments: <name> [version] [fullDocs]"),
},
{
name: "invalid third arg",
Expand Down
2 changes: 1 addition & 1 deletion pkg/tfbridge/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type Provider struct {
// - TFProviderModuleVersion
//
// For dynamically bridged providers, when the fullDocs option is set on the parameterized inputs,
// this field will be automatically set to "terraform-provider-<provider>-v<version>".
// this field will be automatically set to a temporary directory containing a shallow clone of the upstream repo.

// This list may change over time.
UpstreamRepoPath string
Expand Down
2 changes: 1 addition & 1 deletion pkg/tfgen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ type GenerateSchemaResult struct {

func GenerateSchemaWithOptions(opts GenerateSchemaOptions) (*GenerateSchemaResult, error) {
ctx := context.Background()

info := opts.ProviderInfo
sink := opts.DiagnosticsSink
g, err := NewGenerator(GeneratorOptions{
Expand All @@ -802,6 +801,7 @@ func GenerateSchemaWithOptions(opts GenerateSchemaOptions) (*GenerateSchemaResul
if err != nil {
return nil, errors.Wrapf(err, "failed to create generator")
}

return g.generateSchemaResult(ctx)
}

Expand Down

0 comments on commit 27033ba

Please sign in to comment.