diff --git a/build/git_revision_test.go b/build/git_revision_test.go index 98d99b3..dca27d0 100644 --- a/build/git_revision_test.go +++ b/build/git_revision_test.go @@ -127,6 +127,35 @@ func TestGitRevision_vault(t *testing.T) { } } +func TestGitRevision_packer(t *testing.T) { + testutil.EndToEndTest(t) + + gr := &GitRevision{Product: product.Packer} + gr.SetLogger(testutil.TestLogger()) + + ctx := context.Background() + + execPath, err := gr.Build(ctx) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { gr.Remove(ctx) }) + + v, err := product.Packer.GetVersion(ctx, execPath) + if err != nil { + t.Fatal(err) + } + + latestConstraint, err := version.NewConstraint(">= 1.0") + if err != nil { + t.Fatal(err) + } + if !latestConstraint.Check(v.Core()) { + t.Fatalf("versions don't match (expected: %s, installed: %s)", + latestConstraint, v) + } +} + func TestGitRevisionValidate(t *testing.T) { t.Parallel() diff --git a/product/packer.go b/product/packer.go new file mode 100644 index 0000000..4ad35dc --- /dev/null +++ b/product/packer.go @@ -0,0 +1,54 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package product + +import ( + "context" + "fmt" + "os/exec" + "regexp" + "runtime" + "strings" + + "github.com/hashicorp/go-version" + "github.com/hashicorp/hc-install/internal/build" +) + +var packerVersionOutputRe = regexp.MustCompile(`Packer ` + simpleVersionRe) + +var Packer = Product{ + Name: "packer", + BinaryName: func() string { + if runtime.GOOS == "windows" { + return "packer.exe" + } + return "packer" + }, + GetVersion: func(ctx context.Context, path string) (*version.Version, error) { + cmd := exec.CommandContext(ctx, path, "version") + + out, err := cmd.Output() + if err != nil { + return nil, err + } + + stdout := strings.TrimSpace(string(out)) + + submatches := packerVersionOutputRe.FindStringSubmatch(stdout) + if len(submatches) != 2 { + return nil, fmt.Errorf("unexpected number of version matches %d for %s", len(submatches), stdout) + } + v, err := version.NewVersion(submatches[1]) + if err != nil { + return nil, fmt.Errorf("unable to parse version %q: %w", submatches[1], err) + } + + return v, err + }, + BuildInstructions: &BuildInstructions{ + GitRepoURL: "https://github.com/hashicorp/packer.git", + PreCloneCheck: &build.GoIsInstalled{}, + Build: &build.GoBuild{DetectVendoring: true}, + }, +} diff --git a/product/product.go b/product/product.go index 85f2e11..aea16eb 100644 --- a/product/product.go +++ b/product/product.go @@ -10,6 +10,8 @@ import ( "github.com/hashicorp/go-version" ) +const simpleVersionRe = `v?(?P[0-9]+(?:\.[0-9]+)*(?:-[A-Za-z0-9\.]+)?)` + type Product struct { // Name which identifies the product // on releases.hashicorp.com and in Checkpoint diff --git a/product/terraform.go b/product/terraform.go index afb6b35..39e81f6 100644 --- a/product/terraform.go +++ b/product/terraform.go @@ -15,11 +15,7 @@ import ( "github.com/hashicorp/hc-install/internal/build" ) -var ( - simpleVersionRe = `v?(?P[0-9]+(?:\.[0-9]+)*(?:-[A-Za-z0-9\.]+)?)` - - terraformVersionOutputRe = regexp.MustCompile(`Terraform ` + simpleVersionRe) -) +var terraformVersionOutputRe = regexp.MustCompile(`Terraform ` + simpleVersionRe) var Terraform = Product{ Name: "terraform",