Add --format to docker service ls

This fix tries to improve the display of `docker service ls`
and adds `--format` flag to `docker service ls`.

In addition to `--format` flag, several other improvement:
1. Updates `docker stacks service`.
2. Adds `servicesFormat` to config file.

Related docs has been updated.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang
2017-02-01 08:33:19 -08:00
parent cb8723543e
commit 31fb756bb6
5 changed files with 327 additions and 69 deletions
+23 -5
View File
@@ -9,6 +9,7 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/cli"
"github.com/docker/docker/cli/command"
"github.com/docker/docker/cli/command/formatter"
"github.com/docker/docker/cli/command/service"
"github.com/docker/docker/opts"
"github.com/spf13/cobra"
@@ -16,6 +17,7 @@ import (
type servicesOptions struct {
quiet bool
format string
filter opts.FilterOpt
namespace string
}
@@ -34,6 +36,7 @@ func newServicesCommand(dockerCli *command.DockerCli) *cobra.Command {
}
flags := cmd.Flags()
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
flags.StringVar(&opts.format, "format", "", "Pretty-print services using a Go template")
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
return cmd
@@ -57,9 +60,8 @@ func runServices(dockerCli *command.DockerCli, opts servicesOptions) error {
return nil
}
if opts.quiet {
service.PrintQuiet(out, services)
} else {
info := map[string]formatter.ServiceListInfo{}
if !opts.quiet {
taskFilter := filters.NewArgs()
for _, service := range services {
taskFilter.Add("service", service.ID)
@@ -69,11 +71,27 @@ func runServices(dockerCli *command.DockerCli, opts servicesOptions) error {
if err != nil {
return err
}
nodes, err := client.NodeList(ctx, types.NodeListOptions{})
if err != nil {
return err
}
service.PrintNotQuiet(out, services, nodes, tasks)
info = service.GetServicesStatus(services, nodes, tasks)
}
return nil
format := opts.format
if len(format) == 0 {
if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.quiet {
format = dockerCli.ConfigFile().ServicesFormat
} else {
format = formatter.TableFormatKey
}
}
servicesCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewServiceListFormat(format, opts.quiet),
}
return formatter.ServiceListWrite(servicesCtx, services, info)
}