Skip to content

Commit

Permalink
refactor(git): use go-git instead of shelled out commands (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Jul 4, 2017
1 parent 71fa116 commit d37461a
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 94 deletions.
76 changes: 56 additions & 20 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions conform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,23 @@ scripts:
templates:
build: |
FROM golang:1.8.3 as build
WORKDIR /go/src/github.com/autonomy/conform
WORKDIR $GOPATH/src/github.com/autonomy/conform
COPY ./ ./
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /conform -a -ldflags "-X \"github.com/autonomy/conform/conform.Tag={{ trimAll "v" .GitInfo.Tag }}\" -X \"github.com/autonomy/conform/conform.SHA={{ .GitInfo.SHA }}\" -X \"github.com/autonomy/conform/conform.Built={{ .Built }}\""
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /conform -ldflags "-X \"github.com/autonomy/conform/cmd.Tag={{ trimAll "v" .GitInfo.Tag }}\" -X \"github.com/autonomy/conform/conform.Tag={{ trimAll "v" .GitInfo.Tag }}\" -X \"github.com/autonomy/conform/conform.SHA={{ .GitInfo.SHA }}\" -X \"github.com/autonomy/conform/conform.Built={{ .Built }}\""
test: |
FROM golang:1.8.3 as test
WORKDIR /go/src/github.com/autonomy/conform
WORKDIR $GOPATH/src/github.com/autonomy/conform
RUN go get -u gopkg.in/alecthomas/gometalinter.v1
RUN gometalinter.v1 --install
COPY --from=build /go/src/github.com/autonomy/conform .
COPY --from=build $GOPATH/src/github.com/autonomy/conform .
RUN chmod +x ./scripts/test.sh; sync; ./scripts/test.sh
image: |
FROM alpine:3.6 as image
MAINTAINER Andrew Rynhard <andrew.rynhard@autonomy.io>
RUN apk --update add bash \
&& rm -rf /var/cache/apk/*
WORKDIR /app
COPY --from=build /conform .
ENTRYPOINT ["./conform"]
COPY --from=build /conform /bin
ENTRYPOINT ["conform"]
rules:
build:
Expand Down
63 changes: 60 additions & 3 deletions conform/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"text/template"

Expand All @@ -30,6 +30,10 @@ func NewEnforcer(rule string) (enforcer *Enforcer, err error) {
if err != nil {
return
}
err = exportAll(gitInfo)
if err != nil {
return
}
date := []byte{}
if gitInfo.IsTag {
date, err = exec.Command("/bin/date").Output()
Expand Down Expand Up @@ -129,7 +133,7 @@ func (e *Enforcer) ExtractArtifact(artifact string) error {
// ExecuteScript executes a script for a rule.
func (e *Enforcer) ExecuteScript(script string) error {
if s, ok := e.config.Scripts[script]; ok {
log.Printf("Running %q script", script)
fmt.Printf("Running %q script\n", script)

command := exec.Command("bash", "-c", s)
command.Stdout = os.Stdout
Expand All @@ -152,7 +156,7 @@ func (e *Enforcer) ExecuteScript(script string) error {
// ExecuteRule performs all the relevant actions specified in its' declaration.
func (e *Enforcer) ExecuteRule() error {
if t, ok := e.config.Rules[e.rule]; ok {
log.Printf("Enforcing %q", e.rule)
fmt.Printf("Enforcing %q\n", e.rule)
for _, s := range t.Before {
err := e.ExecuteScript(s)
if err != nil {
Expand Down Expand Up @@ -180,6 +184,59 @@ func (e *Enforcer) ExecuteRule() error {
return fmt.Errorf("Rule %q is not defined in conform.yaml", e.rule)
}

func exportAll(gitInfo *git.Info) (err error) {
fmt.Printf("Branch: %s\n", gitInfo.Branch)
err = ExportConformVar("branch", gitInfo.Branch)
if err != nil {
return
}
fmt.Printf("SHA: %s\n", gitInfo.SHA)
err = ExportConformVar("sha", gitInfo.SHA)
if err != nil {
return
}
fmt.Printf("Tag: %s\n", gitInfo.Tag)
err = ExportConformVar("tag", gitInfo.Tag)
if err != nil {
return
}
fmt.Printf("IsTag: %s\n", strconv.FormatBool(gitInfo.IsTag))
err = ExportConformVar("is_tag", strconv.FormatBool(gitInfo.IsTag))
if err != nil {
return
}
fmt.Printf("Prerelease: %s\n", gitInfo.Prerelease)
err = ExportConformVar("prerelease", gitInfo.Prerelease)
if err != nil {
return
}
fmt.Printf("IsPrerelease: %s\n", strconv.FormatBool(gitInfo.IsPrerelease))
err = ExportConformVar("is_prerelease", strconv.FormatBool(gitInfo.IsPrerelease))
if err != nil {
return
}
fmt.Printf("Status: \n%s\n", strings.TrimRight(gitInfo.Status, "\n"))
err = ExportConformVar("status", strconv.FormatBool(gitInfo.IsDirty))
if err != nil {
return
}
fmt.Printf("IsDirty: %s\n", strconv.FormatBool(gitInfo.IsDirty))
err = ExportConformVar("is_dirty", strconv.FormatBool(gitInfo.IsDirty))
if err != nil {
return
}

return
}

// ExportConformVar exports variable prefixed with CONFORM_
func ExportConformVar(name, value string) (err error) {
variable := fmt.Sprintf("CONFORM_%s", strings.ToUpper(name))
err = os.Setenv(variable, value)

return
}

// FormatImageNameDirty formats the image name.
func (e *Enforcer) FormatImageNameDirty() string {
return fmt.Sprintf("%s:%s", *e.config.Metadata.Repository, "dirty")
Expand Down
Loading

0 comments on commit d37461a

Please sign in to comment.