github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/commands/build.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/fnproject/cli/common"
     9  	"github.com/urfave/cli"
    10  )
    11  
    12  // BuildCommand returns build cli.command
    13  func BuildCommand() cli.Command {
    14  	cmd := buildcmd{}
    15  	flags := append([]cli.Flag{}, cmd.flags()...)
    16  	return cli.Command{
    17  		Name:        "build",
    18  		Usage:       "\tBuild function version",
    19  		Category:    "DEVELOPMENT COMMANDS",
    20  		Description: "This command builds a new function.",
    21  		ArgsUsage:   "[function-subdirectory]",
    22  		Aliases:     []string{"bu"},
    23  		Flags:       flags,
    24  		Action:      cmd.build,
    25  	}
    26  }
    27  
    28  type buildcmd struct {
    29  	verbose bool
    30  	noCache bool
    31  }
    32  
    33  func (b *buildcmd) flags() []cli.Flag {
    34  	return []cli.Flag{
    35  		cli.BoolFlag{
    36  			Name:        "v",
    37  			Usage:       "Verbose mode",
    38  			Destination: &b.verbose,
    39  		},
    40  		cli.BoolFlag{
    41  			Name:        "no-cache",
    42  			Usage:       "Don't use docker cache",
    43  			Destination: &b.noCache,
    44  		},
    45  		cli.StringSliceFlag{
    46  			Name:  "build-arg",
    47  			Usage: "Set build-time variables",
    48  		},
    49  		cli.StringFlag{
    50  			Name:  "working-dir, w",
    51  			Usage: "Specify the working directory to build a function, must be the full path.",
    52  		},
    53  	}
    54  }
    55  
    56  // build will take the found valid function and build it
    57  func (b *buildcmd) build(c *cli.Context) error {
    58  	dir := common.GetDir(c)
    59  
    60  	path := c.Args().First()
    61  	if path != "" {
    62  		fmt.Printf("Building function at: /%s\n", path)
    63  		dir = filepath.Join(dir, path)
    64  	}
    65  
    66  	err := os.Chdir(dir)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	defer os.Chdir(dir)
    71  
    72  	ffV, err := common.ReadInFuncFile()
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	switch common.GetFuncYamlVersion(ffV) {
    78  	case common.LatestYamlVersion:
    79  		fpath, ff, err := common.FindAndParseFuncFileV20180708(dir)
    80  		if err != nil {
    81  			return err
    82  		}
    83  
    84  		buildArgs := c.StringSlice("build-arg")
    85  		ff, err = common.BuildFuncV20180708(c.GlobalBool("verbose"), fpath, ff, buildArgs, b.noCache)
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		fmt.Printf("Function %v built successfully.\n", ff.ImageNameV20180708())
    91  		return nil
    92  
    93  	default:
    94  		fpath, ff, err := common.FindAndParseFuncfile(dir)
    95  		if err != nil {
    96  			return err
    97  		}
    98  
    99  		buildArgs := c.StringSlice("build-arg")
   100  		ff, err = common.BuildFunc(c.GlobalBool("verbose"), fpath, ff, buildArgs, b.noCache)
   101  		if err != nil {
   102  			return err
   103  		}
   104  
   105  		fmt.Printf("Function %v built successfully.\n", ff.ImageName())
   106  		return nil
   107  	}
   108  }