Skip to content

Commit

Permalink
feat(renderer): allow templates to be retrieved from URL (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Sep 29, 2017
1 parent c2cb181 commit c53f523
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
44 changes: 2 additions & 42 deletions .conform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,7 @@ policies:

script:
template: |
#!/bin/bash
set -e
# Only run if not a PR.
if [[ "${TRAVIS_PULL_REQUEST}" != "false" ]]; then
exit 0
fi
if [[ "${TRAVIS_BRANCH}" == "master" ]] || [[ ! -z "${TRAVIS_TAG}" ]]; then
# Check if working tree is clean.
if [[ "{{ .Git.IsClean }}" != "true" ]]; then
echo "The working tree is dirty."
exit 1
fi
echo "Pushing image {{ .Docker.Image }}"
docker login -u "${DOCKER_USERNAME}" -p "${DOCKER_PASSWORD}"
docker push {{ .Docker.Image }}
# Check if ref is a tag.
if [[ "{{ .Git.IsTag }}" != "true" ]]; then
exit 0
fi
# Check if the tag is a prerelease.
if [[ "{{ .Version.IsPrerelease }}" == "true" ]]; then
echo "Publishing prerelease {{ .Version.Major }}.{{ .Version.Minor }}.{{ .Version.Patch }}-{{ .Version.Prerelease }}"
docker tag {{ .Docker.Image }} {{ .Repository }}:{{ .Version.Major }}.{{ .Version.Minor }}.{{ .Version.Patch }}-{{ .Version.Prerelease }}
docker push {{ .Repository }}:{{ .Version.Major }}.{{ .Version.Minor }}.{{ .Version.Patch }}-{{ .Version.Prerelease }}
else
echo "Publishing release {{ .Version.Major }}.{{ .Version.Minor }}.{{ .Version.Patch }}"
docker tag {{ .Docker.Image }} {{ .Repository }}:{{ .Version.Major }}.{{ .Version.Minor }}.{{ .Version.Patch }}
docker push {{ .Repository }}:{{ .Version.Major }}.{{ .Version.Minor }}.{{ .Version.Patch }}
docker tag {{ .Docker.Image }} {{ .Repository }}:latest
docker push {{ .Repository }}:latest
fi
fi
{{ fromURL "https://raw.githubusercontent.com/autonomy/conform-templates/master/docker/deploy.sh" }}
pipeline:
stages:
Expand Down Expand Up @@ -94,10 +57,7 @@ tasks:
{{ end }}
test:
template: |
FROM autonomy/golang:1.8.3 as test
WORKDIR $GOPATH/src/github.com/{{ .Repository }}
COPY ./ ./
RUN test.sh
{{ fromURL "https://raw.githubusercontent.com/autonomy/conform-templates/master/golang/Dockerfile.test" }}
image:
template: |
FROM alpine:3.6 as image
Expand Down
40 changes: 39 additions & 1 deletion pkg/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package renderer

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"text/template"

"github.com/Masterminds/sprig"
Expand All @@ -14,7 +17,8 @@ type Renderer struct{}
// Render renders a template.
func (r *Renderer) Render(m *metadata.Metadata, s string) (string, error) {
var wr bytes.Buffer
tmpl, err := template.New("").Funcs(sprig.TxtFuncMap()).Parse(s)
funcMap := makeFuncMap(m)
tmpl, err := template.New("").Funcs(funcMap).Parse(s)
if err != nil {
return "", err
}
Expand All @@ -36,3 +40,37 @@ func RenderTemplate(contents string, metadata *metadata.Metadata) (string, error

return rendered, nil
}

func makeFuncMap(m *metadata.Metadata) template.FuncMap {
funcMap := sprig.TxtFuncMap()

funcMap["fromURL"] = func(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer func() {
_err := resp.Body.Close()
if _err != nil {
fmt.Printf("Failed to close the response body: %#v", _err)
}
}()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("Failed to download from URL: %d", resp.StatusCode)
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

rendered, err := RenderTemplate(string(b), m)
if err != nil {
return "", err
}

return rendered, nil
}

return funcMap
}

0 comments on commit c53f523

Please sign in to comment.