Skip to content

Commit

Permalink
refactor(docker): read Dockerfile from stdin (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrynhard authored Jul 4, 2017
1 parent dcc9fe5 commit 9927a05
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage.txt
Dockerfile
vendor
29 changes: 11 additions & 18 deletions conform/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package conform
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -55,7 +54,7 @@ func NewEnforcer(rule string) (enforcer *Enforcer, err error) {
}

// ExecuteBuild executes a docker build.
func (e *Enforcer) ExecuteBuild() error {
func (e *Enforcer) ExecuteBuild(dockerfile string) error {
image := e.FormatImageNameSHA()
if e.GitInfo.IsDirty {
image = e.FormatImageNameDirty()
Expand All @@ -66,8 +65,9 @@ func (e *Enforcer) ExecuteBuild() error {
return err
}

args := append([]string{"build", "--tag", image, "."})
args := append([]string{"build", "--tag", image, "-f", "-", "."})
command := exec.Command("docker", args...)
command.Stdin = strings.NewReader(dockerfile)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err = command.Start()
Expand All @@ -80,28 +80,21 @@ func (e *Enforcer) ExecuteBuild() error {
}

// RenderDockerfile writes the final Dockerfile to disk.
func (e *Enforcer) RenderDockerfile(target *config.Rule) error {
dockerfile := `# THIS FILE IS AUTOGENERATED BY CONFORM. DO NOT EDIT!
# Note: This file should be ignored by git.
`
func (e *Enforcer) RenderDockerfile(target *config.Rule) (dockerfile string, err error) {
for _, p := range target.Templates {
r, err := e.RenderTemplate(p)
if err != nil {
return err
r, _err := e.RenderTemplate(p)
if _err != nil {
err = _err
return
}
dockerfile = dockerfile + "\n" + *r
}

if e.config.Debug {
fmt.Println(dockerfile)
} else {
err := ioutil.WriteFile("Dockerfile", []byte(dockerfile), 0644)
if err != nil {
return err
}
}

return nil
return
}

// RenderTemplate executes the template and returns it.
Expand Down Expand Up @@ -163,11 +156,11 @@ func (e *Enforcer) ExecuteRule() error {
return err
}
}
err := e.RenderDockerfile(t)
dockerfile, err := e.RenderDockerfile(t)
if err != nil {
return err
}
err = e.ExecuteBuild()
err = e.ExecuteBuild(dockerfile)
if err != nil {
return err
}
Expand Down

0 comments on commit 9927a05

Please sign in to comment.