github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/commands/build.go (about)

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