Skip to content

Commit

Permalink
Merge pull request #53 from ethanmdavidson/improve-debug-logging
Browse files Browse the repository at this point in the history
Improve debug logging
  • Loading branch information
ethanmdavidson authored Apr 8, 2023
2 parents 41b6064 + 0b09e25 commit 3e202cd
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
intervale: "weekly"
interval: "weekly"

5 changes: 4 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ jobs:
go-version-file: 'go.mod'
cache: true

- name: Install Tools
run: make prep

- name: Execute tests
run: make dev && make testacc
run: make test && make testacc

- name: Upload test artifacts for debugging
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce
Expand Down
35 changes: 24 additions & 11 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,50 @@ HASHICORP_PACKER_PLUGIN_SDK_VERSION?=$(shell go list -m github.com/hashicorp/pac
COUNT?=1
TEST?=$(shell go list ./...)

.PHONY: dev
prep: phony
go install honnef.co/go/tools/cmd/staticcheck@latest

build:
build: phony
@go build -o ${BINARY}

dev: build
dev: phony build
@mkdir -p ~/.packer.d/plugins/
@mv ${BINARY} ~/.packer.d/plugins/${BINARY}

run-example: dev
run-example: phony dev
@packer build ./example

test:
@go test -race -count $(COUNT) $(TEST) -timeout=3m
test: phony
go mod tidy
go fmt ./...
go vet ./...
staticcheck -checks="all" -tests ./...
go test -race -count $(COUNT) $(TEST) -timeout=3m

# the acceptance tests have a weird habit of messing up the tty (e.g. turning off echo mode, so
# terminal stops showing what you type). If this happens to you, run `reset` or `stty sane` to fix.
testacc: dev
testacc: phony dev
@PACKER_ACC=1 go test -count $(COUNT) -v $(TEST) -timeout=120m

install-packer-sdc: ## Install packer sofware development command
install-packer-sdc: phony ## Install packer software development command
@go install github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc@${HASHICORP_PACKER_PLUGIN_SDK_VERSION}

ci-release-docs: install-packer-sdc
ci-release-docs: phony install-packer-sdc
@packer-sdc renderdocs -src docs -partials docs-partials/ -dst docs/
@/bin/sh -c "[ -d docs ] && zip -r docs.zip docs/"

plugin-check: install-packer-sdc build
plugin-check: phony install-packer-sdc build
@packer-sdc plugin-check ${BINARY}

generate: install-packer-sdc
generate: phony install-packer-sdc
@go generate -v ./...
packer-sdc renderdocs -src ./docs -dst ./.docs -partials ./docs-partials
# see the .docs folder for a preview of the docs

# instead of listing every target in .PHONY, we create one
# 'phony' target which all the other targets depend on.
# This saves me from having to remember to add each new target
# to the .PHONY list, and is a little cleaner than putting
# `.PHONY: target` before each target
.PHONY: phony
phony:
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Add the plugin to your packer config:
packer {
required_plugins {
git = {
version = ">=v0.3.2"
version = ">= 0.3.5"
source = "github.com/ethanmdavidson/git"
}
}
Expand Down Expand Up @@ -52,6 +52,6 @@ The typical development flow looks something like this:

For local development, you will need to install:
- [Packer](https://learn.hashicorp.com/tutorials/packer/get-started-install-cli) >= 1.7
- [Go](https://golang.org/doc/install) >= 1.18
- [Go](https://golang.org/doc/install) >= 1.19
- Make
`
16 changes: 16 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Package common contains some logic that is shared by all datasources
package common

import (
"log"
"path/filepath"
)

func PrintOpeningRepo(path string) {
absPath, err := filepath.Abs(path)
if err != nil {
log.Printf("Finding repository from '%s' (unable to determine absolute path, %s)\n", path, err.Error())
} else {
log.Printf("Finding repository from '%s' (%s)\n", path, absPath)
}
}
49 changes: 40 additions & 9 deletions datasource/commit/data.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Package commit contains logic for providing commit data to Packer
//
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,DatasourceOutput
package commit

import (
"github.com/ethanmdavidson/packer-plugin-git/common"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/zclconf/go-cty/cty"
"log"
)

type Config struct {
Expand Down Expand Up @@ -53,45 +57,72 @@ func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
}

func (d *Datasource) Execute() (cty.Value, error) {
log.Println("Starting execution")
output := DatasourceOutput{}
emptyOutput := hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec())

common.PrintOpeningRepo(d.config.Path)
openOptions := &git.PlainOpenOptions{DetectDotGit: true}
repo, err := git.PlainOpenWithOptions(d.config.Path, openOptions)
if err != nil {
return emptyOutput, err
}
log.Println("Repo opened")

hash, err := repo.ResolveRevision(plumbing.Revision(d.config.CommitIsh))
if err != nil {
return emptyOutput, err
}
branches, err := repo.Branches()
log.Printf("Hash found: '%s'\n", hash.String())

branchIter, err := repo.Branches()
if err != nil {
return emptyOutput, err
}
branchesAtResolvedCommit := make([]string, 0)
_ = branches.ForEach(func(ref *plumbing.Reference) error {
if ref.Hash().String() == hash.String() {
branchesAtResolvedCommit = append(branchesAtResolvedCommit, ref.Name().Short())
}
return nil
})
log.Println("Branches found")

commit, err := repo.CommitObject(*hash)
if err != nil {
return emptyOutput, err
}
log.Printf("Commit found: '%s'\n", commit.String())

output.Hash = hash.String()
output.Branches = branchesAtResolvedCommit
log.Printf("output.Hash: '%s'\n", output.Hash)

output.Branches = make([]string, 0)
_ = branchIter.ForEach(func(ref *plumbing.Reference) error {
if ref.Hash().String() == hash.String() {
log.Printf("Adding branch '%s'\n", ref.Name().Short())
output.Branches = append(output.Branches, ref.Name().Short())
} else {
log.Printf("Not in branch '%s' (%s)\n", ref.Name().Short(), ref.Hash().String())
}
return nil
})
log.Printf("len(output.Branches): '%d'\n", len(output.Branches))

output.Author = commit.Author.String()
log.Printf("output.Author: '%s'\n", output.Author)

output.Committer = commit.Committer.String()
log.Printf("output.Committer: '%s'\n", output.Committer)

output.PGPSignature = commit.PGPSignature
log.Printf("len(output.PGPSignature): '%d'\n", len(output.PGPSignature))

output.Message = commit.Message
log.Printf("len(output.Message): '%d'\n", len(output.Message))

output.TreeHash = commit.TreeHash.String()
log.Printf("output.TreeHash: '%s'\n", output.TreeHash)

output.ParentHashes = make([]string, 0)
for _, parent := range commit.ParentHashes {
log.Printf("Adding parent hash: '%s'\n", parent.String())
output.ParentHashes = append(output.ParentHashes, parent.String())
}
log.Printf("len(output.ParentHashes): '%d'\n", len(output.ParentHashes))

return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
}
8 changes: 5 additions & 3 deletions datasource/commit/data_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package commit
import (
_ "embed"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -38,9 +38,11 @@ func TestAccGitCommitDatasource(t *testing.T) {
if err != nil {
return fmt.Errorf("Unable find %s", logfile)
}
defer logs.Close()
defer func(logs *os.File) {
_ = logs.Close()
}(logs)

logsBytes, err := ioutil.ReadAll(logs)
logsBytes, err := io.ReadAll(logs)
if err != nil {
return fmt.Errorf("Unable to read %s", logfile)
}
Expand Down
26 changes: 26 additions & 0 deletions datasource/repository/data.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Package repository contains logic for providing repo data to Packer
//
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,DatasourceOutput
package repository

import (
"github.com/ethanmdavidson/packer-plugin-git/common"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/zclconf/go-cty/cty"
"log"
)

type Config struct {
Expand Down Expand Up @@ -45,47 +49,69 @@ func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
}

func (d *Datasource) Execute() (cty.Value, error) {
log.Println("Starting execution")
output := DatasourceOutput{}
emptyOutput := hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec())

common.PrintOpeningRepo(d.config.Path)
openOptions := &git.PlainOpenOptions{DetectDotGit: true}
repo, err := git.PlainOpenWithOptions(d.config.Path, openOptions)
if err != nil {
return emptyOutput, err
}
log.Println("Repo opened")

head, err := repo.Head()
if err != nil {
return emptyOutput, err
}
log.Printf("Head found: '%s'\n", head.String())

worktree, err := repo.Worktree()
if err != nil {
return emptyOutput, err
}
log.Println("Worktree found")

status, err := worktree.Status()
if err != nil {
return emptyOutput, err
}
log.Printf("Worktree status found: '%s'\n", status.String())

branchIter, err := repo.Branches()
if err != nil {
return emptyOutput, err
}
log.Println("Branches found")

tagIter, err := repo.Tags()
if err != nil {
return emptyOutput, err
}
log.Println("Tags found")

output.Head = head.Name().Short()
log.Printf("output.Head: '%s'\n", output.Head)

output.IsClean = status.IsClean()
log.Printf("output.IsClean: '%t'\n", output.IsClean)

output.Branches = make([]string, 0)
_ = branchIter.ForEach(func(reference *plumbing.Reference) error {
log.Printf("Adding branch: '%s'\n", reference.Name().Short())
output.Branches = append(output.Branches, reference.Name().Short())
return nil
})
log.Printf("len(output.Branches): '%d'\n", len(output.Branches))

output.Tags = make([]string, 0)
_ = tagIter.ForEach(func(reference *plumbing.Reference) error {
log.Printf("Adding tag: '%s'\n", reference.Name().Short())
output.Tags = append(output.Tags, reference.Name().Short())
return nil
})
log.Printf("len(output.Tags): '%d'\n", len(output.Tags))

return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
}
8 changes: 5 additions & 3 deletions datasource/repository/data_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package repository
import (
_ "embed"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -38,9 +38,11 @@ func TestAccGitRepositoryDatasource(t *testing.T) {
if err != nil {
return fmt.Errorf("Unable find %s", logfile)
}
defer logs.Close()
defer func(logs *os.File) {
_ = logs.Close()
}(logs)

logsBytes, err := ioutil.ReadAll(logs)
logsBytes, err := io.ReadAll(logs)
if err != nil {
return fmt.Errorf("Unable to read %s", logfile)
}
Expand Down
Loading

0 comments on commit 3e202cd

Please sign in to comment.