github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/apps/distgo/cmd/docker/default_image.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package docker
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"os/exec"
    22  	"path"
    23  
    24  	"github.com/pkg/errors"
    25  
    26  	"github.com/palantir/godel/apps/distgo/params"
    27  	"github.com/palantir/godel/apps/distgo/pkg/script"
    28  )
    29  
    30  type defaultImageBuilder struct {
    31  	image *params.DockerImage
    32  }
    33  
    34  func (di *defaultImageBuilder) build(buildSpec params.ProductBuildSpecWithDeps, stdout io.Writer) error {
    35  	contextDir := path.Join(buildSpec.Spec.ProjectDir, di.image.ContextDir)
    36  	var args []string
    37  	args = append(args, "build")
    38  	args = append(args, "--tag", fmt.Sprintf("%s:%s", di.image.Repository, di.image.Tag))
    39  	buildArgs, err := script.GetBuildArgs(buildSpec.Spec, di.image.BuildArgsScript)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	args = append(args, buildArgs...)
    44  	args = append(args, contextDir)
    45  
    46  	buildCmd := exec.Command("docker", args...)
    47  	bufWriter := &bytes.Buffer{}
    48  	buildCmd.Stdout = io.MultiWriter(stdout, bufWriter)
    49  	buildCmd.Stderr = bufWriter
    50  	if err := buildCmd.Run(); err != nil {
    51  		return errors.Wrap(err, fmt.Sprintf("docker build failed with error:\n%s\n", bufWriter.String()))
    52  	}
    53  	return nil
    54  }