github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/build.go (about)

     1  /*
     2   * Copyright 2018 the original author or authors.
     3   *
     4   *   Licensed under the Apache License, Version 2.0 (the "License");
     5   *   you may not use this file except in compliance with the License.
     6   *   You may obtain a copy of the License at
     7   *  
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *  
    10   *   Unless required by applicable law or agreed to in writing, software
    11   *   distributed under the License is distributed on an "AS IS" BASIS,
    12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *   See the License for the specific language governing permissions and
    14   *   limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/spf13/cobra"
    23  	"github.com/projectriff/riff-cli/pkg/options"
    24  	"strings"
    25  	"github.com/projectriff/riff-cli/pkg/docker"
    26  	"github.com/projectriff/riff-cli/pkg/ioutils"
    27  	"os"
    28  	"github.com/projectriff/riff-cli/pkg/osutils"
    29  	"path/filepath"
    30  	"github.com/projectriff/riff-cli/cmd/utils"
    31  	"github.com/projectriff/riff-cli/cmd/opts"
    32  )
    33  
    34  var buildCmd = &cobra.Command{
    35  	Use:   "build",
    36  	Short: "Build a function container",
    37  	Long: `Build the function based on the code available in the path directory, using the name
    38    and version specified for the image that is built.`,
    39  	Example: `  riff build -n <name> -v <version> -f <path> [--push]`,
    40  	RunE: func(cmd *cobra.Command, args []string) error {
    41  		return build(options.GetBuildOptions(opts.CreateOptions))
    42  	},
    43  	//TODO: DRY
    44  	PreRun: func(cmd *cobra.Command, args []string) {
    45  		if !opts.CreateOptions.Initialized {
    46  			utils.MergeBuildOptions(*cmd.Flags(), &opts.CreateOptions)
    47  
    48  			if len(args) > 0 {
    49  				if len(args) == 1 && opts.CreateOptions.FilePath == "" {
    50  					opts.CreateOptions.FilePath = args[0]
    51  				} else {
    52  					ioutils.Errorf("Invalid argument(s) %v\n", args)
    53  					cmd.Usage()
    54  					os.Exit(1)
    55  				}
    56  			}
    57  
    58  			err := options.ValidateAndCleanInitOptions(&opts.CreateOptions.InitOptions)
    59  			if err != nil {
    60  				ioutils.Error(err)
    61  				os.Exit(1)
    62  			}
    63  		}
    64  		opts.CreateOptions.Initialized = true
    65  	},
    66  }
    67  
    68  func build(opts options.BuildOptions) error {
    69  	buildArgs := buildArgs(opts)
    70  	pushArgs := pushArgs(opts)
    71  	if opts.DryRun {
    72  		fmt.Printf("\nBuild command: docker %s\n", strings.Join(buildArgs, " "))
    73  		if (opts.Push) {
    74  			fmt.Printf("\nPush command: docker %s\n", strings.Join(pushArgs, " "))
    75  		}
    76  		fmt.Println("")
    77  		return nil
    78  	}
    79  
    80  	fmt.Println("Building image ...")
    81  	out, err := docker.Exec(buildArgs)
    82  	if err != nil {
    83  		ioutils.Errorf("Error %v\n", err)
    84  		return err
    85  	}
    86  	fmt.Println(out)
    87  
    88  	if opts.Push {
    89  		fmt.Println("Pushing image...")
    90  		out, err = docker.Exec(pushArgs)
    91  		if err != nil {
    92  			ioutils.Errorf("Error %v\n", err)
    93  			return err
    94  		}
    95  		fmt.Println(out)
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  func buildArgs(opts options.BuildOptions) []string {
   102  	image := options.ImageName(opts)
   103  	path := opts.FilePath
   104  	if !osutils.IsDirectory(opts.FilePath) {
   105  		path = filepath.Dir(path)
   106  	}
   107  	return []string{"build", "-t", image, path}
   108  }
   109  
   110  func pushArgs(opts options.BuildOptions) []string {
   111  	image := options.ImageName(opts)
   112  	return []string{"push", image}
   113  }
   114  
   115  func init() {
   116  	rootCmd.AddCommand(buildCmd)
   117  	utils.CreateBuildFlags(buildCmd.Flags())
   118  }