create.go 1002 B

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