github.com/kobeld/docker@v1.12.0-rc1/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  	addServiceFlags(cmd, opts)
    28  	cmd.Flags().SetInterspersed(false)
    29  	return cmd
    30  }
    31  
    32  func runCreate(dockerCli *client.DockerCli, opts *serviceOptions) error {
    33  	client := dockerCli.Client()
    34  
    35  	service, err := opts.ToService()
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	response, err := client.ServiceCreate(context.Background(), service)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	fmt.Fprintf(dockerCli.Out(), "%s\n", response.ID)
    46  	return nil
    47  }