github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_build.go (about)

     1  /*
     2     Copyright The containerd 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 main
    18  
    19  import (
    20  	"github.com/containerd/nerdctl/pkg/clientutil"
    21  	"github.com/containerd/nerdctl/pkg/cmd/compose"
    22  	"github.com/containerd/nerdctl/pkg/composer"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newComposeBuildCommand() *cobra.Command {
    27  	var composeBuildCommand = &cobra.Command{
    28  		Use:           "build [flags] [SERVICE...]",
    29  		Short:         "Build or rebuild services",
    30  		RunE:          composeBuildAction,
    31  		SilenceUsage:  true,
    32  		SilenceErrors: true,
    33  	}
    34  	composeBuildCommand.Flags().StringArray("build-arg", nil, "Set build-time variables for services.")
    35  	composeBuildCommand.Flags().Bool("no-cache", false, "Do not use cache when building the image.")
    36  	composeBuildCommand.Flags().String("progress", "", "Set type of progress output (auto, plain, tty). Use plain to show container output")
    37  
    38  	return composeBuildCommand
    39  }
    40  
    41  func composeBuildAction(cmd *cobra.Command, args []string) error {
    42  	globalOptions, err := processRootCmdFlags(cmd)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	buildArg, err := cmd.Flags().GetStringArray("build-arg")
    47  	if err != nil {
    48  		return err
    49  	}
    50  	noCache, err := cmd.Flags().GetBool("no-cache")
    51  	if err != nil {
    52  		return err
    53  	}
    54  	progress, err := cmd.Flags().GetString("progress")
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	defer cancel()
    64  	options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr())
    69  	if err != nil {
    70  		return err
    71  	}
    72  	bo := composer.BuildOptions{
    73  		Args:     buildArg,
    74  		NoCache:  noCache,
    75  		Progress: progress,
    76  	}
    77  	return c.Build(ctx, bo, args)
    78  }