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

feat: Implement parsing of provider build output. #82

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions internal/app/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func createBuildCommand(providerName string, version string, goPath string) stri
return buildCommands["default"][0].command
}

func (a *App) buildProvider(dir string, providerName string, version string, customBuildCommand string) {
func (a *App) buildProvider(buildDir string, providerName string, version string, customBuildCommand string) string {
var buildCommand string

fmt.Fprintf(a.Out, "Compiling...\n")
Expand All @@ -226,10 +226,12 @@ func (a *App) buildProvider(dir string, providerName string, version string, cus
logrus.Infof("Using build command: %s", buildCommand)

// #nosec G204
executeBashCommand(buildCommand, a.Config.ProvidersCacheDir+"/"+dir)
buildOutput := executeBashCommand(buildCommand, buildDir)

return buildOutput
}

func (a *App) moveBinaryToCorrectLocation(providerName string, version string, executableName string) {
func (a *App) moveBinaryToCorrectLocation(providerName string, version string, executableName string, buildOutput string, buildDir string) {
if len(version) == 0 {
version = "master"
} else {
Expand All @@ -239,6 +241,12 @@ func (a *App) moveBinaryToCorrectLocation(providerName string, version string, e
newPath := a.createDestinationAndReturnExecutablePath(providerName, version, executableName)

pathOfExecutable := a.Config.GoPath + "/bin/" + executableName

customPath, hasCustomPath := parseBuildOutputAndGetBinaryOutputPath(buildOutput)
if hasCustomPath {
pathOfExecutable = buildDir + "/" + customPath
}

logrus.Info("Move from " + pathOfExecutable + " to " + newPath)
err := os.Rename(pathOfExecutable, newPath)

Expand All @@ -247,6 +255,21 @@ func (a *App) moveBinaryToCorrectLocation(providerName string, version string, e
}
}

func parseBuildOutputAndGetBinaryOutputPath(buildOutput string) (string, bool) {
re := regexp.MustCompile(`go build.*?-o ([a-zA-Z\/\.\d_-]*)`)
find := re.FindStringSubmatch(buildOutput)

if len(find) == 0 {
logrus.Info("No custom build path found")

return "", false
}

logrus.Infof("Found custom build path: %s", find[1])

return find[1], true
}

func (a *App) createDestinationAndReturnExecutablePath(providerName string, version string, executableName string) string {
oldTfVersion, _ := goversion.NewVersion("0.12.31")
currentTfVersion := getTerraformVersion()
Expand Down Expand Up @@ -296,10 +319,11 @@ func (a *App) Install(providerName string, version string, customBuildCommand st

fmt.Fprintf(a.Out, "Getting source code...\n")
sourceCodeDir := checkoutSourceCode(a.Config.ProvidersCacheDir, gitRepo, version)
a.buildProvider(sourceCodeDir, providerName, version, customBuildCommand)
buildDir := a.Config.ProvidersCacheDir + "/" + sourceCodeDir
buildOutput := a.buildProvider(buildDir, providerName, version, customBuildCommand)

name := extractRepoNameFromURL(gitRepo)
a.moveBinaryToCorrectLocation(providerName, version, name)
a.moveBinaryToCorrectLocation(providerName, version, name, buildOutput, buildDir)
fmt.Fprintf(a.Out, "Successfully installed %s %s\n", providerName, version)

return true
Expand Down
16 changes: 16 additions & 0 deletions internal/app/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,19 @@ func TestCheckoutSourceCode(t *testing.T) {
checkoutSourceCode(tmpDir+"/cliCache", "https://github.com/hashicorp/terraform-provider-random", "v2.2.0")
})
}

func TestParseBuildOutputAndGetBinaryOutputPath(t *testing.T) {
t.Run("Should parse correct output from go build -o command", func(t *testing.T) {
expected := "build/darwin_arm64/terraform-provider-pingdom_v1.1.3"
actual, ok := parseBuildOutputAndGetBinaryOutputPath("go build -o build/darwin_arm64/terraform-provider-pingdom_v1.1.3 ")
if ok && expected != actual {
t.Fatalf("expected %#v, but got %#v", expected, actual)
}
})
t.Run("Should parse correct output from go install command", func(t *testing.T) {
_, ok := parseBuildOutputAndGetBinaryOutputPath("go install")
if ok {
t.Fatalf("expected ok to be false")
}
})
}