Skip to content

Commit

Permalink
builder: Support for --no-cache
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
  • Loading branch information
aluzzardi committed Nov 1, 2018
1 parent 8e188cc commit 611f46c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
11 changes: 9 additions & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,32 @@ var buildCmd = &cobra.Command{
if err != nil {
ui.Fatal("unable to resolve flag: %v", err)
}
noCache, err := cmd.Flags().GetBool("no-cache")
if err != nil {
ui.Fatal("unable to resolve flag: %v", err)
}

rootDir := getCwd(cmd)
name := filepath.Base(rootDir)
build(name, rootDir, verbose)
build(name, rootDir, verbose, noCache)
},
}

func init() {
buildCmd.Flags().String("cwd", ".", "specifies the current working directory")
buildCmd.Flags().BoolP("verbose", "v", false, "enable verbose mode")
buildCmd.Flags().Bool("no-cache", false, "disable caching")

rootCmd.AddCommand(buildCmd)
}

func build(name, rootDir string, verbose bool) {
func build(name, rootDir string, verbose, noCache bool) {
ctx := context.Background()
ui.Info("Building %s", name)
b := builder.New(rootDir, name)
opts := builder.BuildOpts{
Verbose: verbose,
NoCache: noCache,
}
if err := b.Build(ctx, opts); err != nil {
ui.Fatal("Failed to build the application: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func create(name, rootDir string) {
ui.Fatal("Failed to initialize: %v", err)
}

build(name, rootDir, false)
build(name, rootDir, false, false)

ui.Success("Sucess! Created %s at %s", ui.Emphasize(name), ui.Emphasize(rootDir))
printGettingStarted(name)
Expand Down
8 changes: 7 additions & 1 deletion pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Builder struct {
// BuildOpts contains a list of build options.
type BuildOpts struct {
Verbose bool
NoCache bool
}

// New creates a new Builder.
Expand All @@ -33,7 +34,12 @@ func New(rootDir, name string) *Builder {

// Build executes a build.
func (b *Builder) Build(ctx context.Context, opts BuildOpts) error {
cmd := exec.CommandContext(ctx, "docker", "build", "-t", b.name, b.rootDir)
args := []string{"build", "-t", b.name}
if opts.NoCache {
args = append(args, "--no-cache")
}
args = append(args, b.rootDir)
cmd := exec.CommandContext(ctx, "docker", args...)
outReader, err := cmd.StdoutPipe()
if err != nil {
return err
Expand Down

0 comments on commit 611f46c

Please sign in to comment.