github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_create.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  	"errors"
    21  
    22  	"github.com/containerd/nerdctl/pkg/clientutil"
    23  	"github.com/containerd/nerdctl/pkg/cmd/compose"
    24  	"github.com/containerd/nerdctl/pkg/composer"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  func newComposeCreateCommand() *cobra.Command {
    29  	var composeCreateCommand = &cobra.Command{
    30  		Use:           "create [flags] [SERVICE...]",
    31  		Short:         "Creates containers for one or more services",
    32  		RunE:          composeCreateAction,
    33  		SilenceUsage:  true,
    34  		SilenceErrors: true,
    35  	}
    36  	composeCreateCommand.Flags().Bool("build", false, "Build images before starting containers.")
    37  	composeCreateCommand.Flags().Bool("no-build", false, "Don't build an image even if it's missing, conflict with --build.")
    38  	composeCreateCommand.Flags().Bool("force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
    39  	composeCreateCommand.Flags().Bool("no-recreate", false, "Don't recreate containers if they exist, conflict with --force-recreate.")
    40  	composeCreateCommand.Flags().String("pull", "missing", "Pull images before running. (support always|missing|never)")
    41  	return composeCreateCommand
    42  }
    43  
    44  func composeCreateAction(cmd *cobra.Command, args []string) error {
    45  	globalOptions, err := processRootCmdFlags(cmd)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	build, err := cmd.Flags().GetBool("build")
    50  	if err != nil {
    51  		return err
    52  	}
    53  	noBuild, err := cmd.Flags().GetBool("no-build")
    54  	if err != nil {
    55  		return err
    56  	}
    57  	if build && noBuild {
    58  		return errors.New("flag --build and --no-build cannot be specified together")
    59  	}
    60  	forceRecreate, err := cmd.Flags().GetBool("force-recreate")
    61  	if err != nil {
    62  		return err
    63  	}
    64  	noRecreate, err := cmd.Flags().GetBool("no-recreate")
    65  	if err != nil {
    66  		return nil
    67  	}
    68  	if forceRecreate && noRecreate {
    69  		return errors.New("flag --force-recreate and --no-recreate cannot be specified together")
    70  	}
    71  
    72  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	defer cancel()
    77  	options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr())
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	opt := composer.CreateOptions{
    87  		Build:         build,
    88  		NoBuild:       noBuild,
    89  		ForceRecreate: forceRecreate,
    90  		NoRecreate:    noRecreate,
    91  	}
    92  
    93  	if cmd.Flags().Changed("pull") {
    94  		pull, err := cmd.Flags().GetString("pull")
    95  		if err != nil {
    96  			return err
    97  		}
    98  		opt.Pull = &pull
    99  	}
   100  
   101  	return c.Create(ctx, opt, args)
   102  }