github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/stack/swarm/services.go (about) 1 package swarm 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/docker/cli/cli/command" 8 "github.com/docker/cli/cli/command/formatter" 9 "github.com/docker/cli/cli/command/service" 10 "github.com/docker/cli/cli/command/stack/options" 11 "github.com/docker/docker/api/types" 12 "github.com/docker/docker/api/types/filters" 13 ) 14 15 // RunServices is the swarm implementation of docker stack services 16 func RunServices(dockerCli command.Cli, opts options.Services) error { 17 ctx := context.Background() 18 client := dockerCli.Client() 19 20 filter := getStackFilterFromOpt(opts.Namespace, opts.Filter) 21 services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: filter}) 22 if err != nil { 23 return err 24 } 25 26 // if no services in this stack, print message and exit 0 27 if len(services) == 0 { 28 fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", opts.Namespace) 29 return nil 30 } 31 32 info := map[string]formatter.ServiceListInfo{} 33 if !opts.Quiet { 34 taskFilter := filters.NewArgs() 35 for _, service := range services { 36 taskFilter.Add("service", service.ID) 37 } 38 39 tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: taskFilter}) 40 if err != nil { 41 return err 42 } 43 44 nodes, err := client.NodeList(ctx, types.NodeListOptions{}) 45 if err != nil { 46 return err 47 } 48 49 info = service.GetServicesStatus(services, nodes, tasks) 50 } 51 52 format := opts.Format 53 if len(format) == 0 { 54 if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.Quiet { 55 format = dockerCli.ConfigFile().ServicesFormat 56 } else { 57 format = formatter.TableFormatKey 58 } 59 } 60 61 servicesCtx := formatter.Context{ 62 Output: dockerCli.Out(), 63 Format: formatter.NewServiceListFormat(format, opts.Quiet), 64 } 65 return formatter.ServiceListWrite(servicesCtx, services, info) 66 }