Skip to content

Commit

Permalink
product: Add packer (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jan 8, 2025
1 parent 5d81835 commit 58694f3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
29 changes: 29 additions & 0 deletions build/git_revision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
54 changes: 54 additions & 0 deletions product/packer.go
Original file line number Diff line number Diff line change
@@ -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},
},
}
2 changes: 2 additions & 0 deletions product/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/hashicorp/go-version"
)

const simpleVersionRe = `v?(?P<version>[0-9]+(?:\.[0-9]+)*(?:-[A-Za-z0-9\.]+)?)`

type Product struct {
// Name which identifies the product
// on releases.hashicorp.com and in Checkpoint
Expand Down
6 changes: 1 addition & 5 deletions product/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import (
"github.com/hashicorp/hc-install/internal/build"
)

var (
simpleVersionRe = `v?(?P<version>[0-9]+(?:\.[0-9]+)*(?:-[A-Za-z0-9\.]+)?)`

terraformVersionOutputRe = regexp.MustCompile(`Terraform ` + simpleVersionRe)
)
var terraformVersionOutputRe = regexp.MustCompile(`Terraform ` + simpleVersionRe)

var Terraform = Product{
Name: "terraform",
Expand Down

0 comments on commit 58694f3

Please sign in to comment.