github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/api/client/service/create.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/api/client"
     7  	"github.com/docker/docker/cli"
     8  	"github.com/spf13/cobra"
     9  	"golang.org/x/net/context"
    10  )
    11  
    12  func newCreateCommand(dockerCli *client.DockerCli) *cobra.Command {
    13  	opts := newServiceOptions()
    14  
    15  	cmd := &cobra.Command{
    16  		Use:   "create [OPTIONS] IMAGE [COMMAND] [ARG...]",
    17  		Short: "Create a new service",
    18  		Args:  cli.RequiresMinArgs(1),
    19  		RunE: func(cmd *cobra.Command, args []string) error {
    20  			opts.image = args[0]
    21  			if len(args) > 1 {
    22  				opts.args = args[1:]
    23  			}
    24  			return runCreate(dockerCli, opts)
    25  		},
    26  	}
    27  	flags := cmd.Flags()
    28  	flags.StringVar(&opts.mode, flagMode, "replicated", "Service mode (replicated or global)")
    29  	addServiceFlags(cmd, opts)
    30  	cmd.Flags().SetInterspersed(false)
    31  	return cmd
    32  }
    33  
    34  func runCreate(dockerCli *client.DockerCli, opts *serviceOptions) error {
    35  	client := dockerCli.Client()
    36  
    37  	service, err := opts.ToService()
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	response, err := client.ServiceCreate(context.Background(), service)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID)
    48  	return nil
    49  }