github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/stack/services.go (about) 1 // +build experimental 2 3 package stack 4 5 import ( 6 "fmt" 7 8 "golang.org/x/net/context" 9 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/api/types/filters" 12 "github.com/docker/docker/cli" 13 "github.com/docker/docker/cli/command" 14 "github.com/docker/docker/cli/command/service" 15 "github.com/docker/docker/opts" 16 "github.com/spf13/cobra" 17 ) 18 19 type servicesOptions struct { 20 quiet bool 21 filter opts.FilterOpt 22 namespace string 23 } 24 25 func newServicesCommand(dockerCli *command.DockerCli) *cobra.Command { 26 opts := servicesOptions{filter: opts.NewFilterOpt()} 27 28 cmd := &cobra.Command{ 29 Use: "services [OPTIONS] STACK", 30 Short: "List the services in the stack", 31 Args: cli.ExactArgs(1), 32 RunE: func(cmd *cobra.Command, args []string) error { 33 opts.namespace = args[0] 34 return runServices(dockerCli, opts) 35 }, 36 } 37 flags := cmd.Flags() 38 flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs") 39 flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided") 40 41 return cmd 42 } 43 44 func runServices(dockerCli *command.DockerCli, opts servicesOptions) error { 45 ctx := context.Background() 46 client := dockerCli.Client() 47 48 filter := opts.filter.Value() 49 filter.Add("label", labelNamespace+"="+opts.namespace) 50 51 services, err := client.ServiceList(ctx, types.ServiceListOptions{Filter: filter}) 52 if err != nil { 53 return err 54 } 55 56 out := dockerCli.Out() 57 58 // if no services in this stack, print message and exit 0 59 if len(services) == 0 { 60 fmt.Fprintf(out, "Nothing found in stack: %s\n", opts.namespace) 61 return nil 62 } 63 64 if opts.quiet { 65 service.PrintQuiet(out, services) 66 } else { 67 taskFilter := filters.NewArgs() 68 for _, service := range services { 69 taskFilter.Add("service", service.ID) 70 } 71 72 tasks, err := client.TaskList(ctx, types.TaskListOptions{Filter: taskFilter}) 73 if err != nil { 74 return err 75 } 76 nodes, err := client.NodeList(ctx, types.NodeListOptions{}) 77 if err != nil { 78 return err 79 } 80 service.PrintNotQuiet(out, services, nodes, tasks) 81 } 82 return nil 83 }