From 7d3b3fda0d193e4d198850944bdea873c78bf77f Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Thu, 26 Jan 2017 13:08:07 -0800 Subject: [PATCH] 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 --- docs/reference/commandline/cli.md | 8 +++++ docs/reference/commandline/service_ls.md | 35 +++++++++++++++++-- docs/reference/commandline/stack_services.md | 36 ++++++++++++++++++-- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/docs/reference/commandline/cli.md b/docs/reference/commandline/cli.md index 30ae431bab..369f90030a 100644 --- a/docs/reference/commandline/cli.md +++ b/docs/reference/commandline/cli.md @@ -137,6 +137,13 @@ Docker's client uses this property. If this property is not set, the client falls back to the default table format. For a list of supported formatting directives, see the [**Formatting** section in the `docker plugin ls` documentation](plugin_ls.md) +The property `servicesFormat` specifies the default format for `docker +service ls` output. When the `--format` flag is not provided with the +`docker service ls` command, Docker's client uses this property. If this +property is not set, the client falls back to the default json format. For a +list of supported formatting directives, see the +[**Formatting** section in the `docker service ls` documentation](service_ls.md) + The property `serviceInspectFormat` specifies the default format for `docker service inspect` output. When the `--format` flag is not provided with the `docker service inspect` command, Docker's client uses this property. If this @@ -194,6 +201,7 @@ Following is a sample `config.json` file: "imagesFormat": "table {{.ID}}\\t{{.Repository}}\\t{{.Tag}}\\t{{.CreatedAt}}", "pluginsFormat": "table {{.ID}}\t{{.Name}}\t{{.Enabled}}", "statsFormat": "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}", + "servicesFormat": "table {{.ID}}\t{{.Name}}\t{{.Mode}}", "serviceInspectFormat": "pretty", "detachKeys": "ctrl-e,e", "credsStore": "secretservice", diff --git a/docs/reference/commandline/service_ls.md b/docs/reference/commandline/service_ls.md index ccd68af750..b4e87ab89c 100644 --- a/docs/reference/commandline/service_ls.md +++ b/docs/reference/commandline/service_ls.md @@ -24,9 +24,10 @@ Aliases: ls, list Options: - -f, --filter value Filter output based on conditions provided - --help Print usage - -q, --quiet Only display IDs + -f, --filter filter Filter output based on conditions provided + --format string Pretty-print services using a Go template + --help Print usage + -q, --quiet Only display IDs ``` This command when run targeting a manager, lists services are running in the @@ -103,6 +104,34 @@ ID NAME MODE REPLICAS IMAGE 0bcjwfh8ychr redis replicated 1/1 redis:3.0.6 ``` +## Formatting + +The formatting options (`--format`) pretty-prints services output +using a Go template. + +Valid placeholders for the Go template are listed below: + +Placeholder | Description +------------|------------------------------------------------------------------------------------------ +`.ID` | Service ID +`.Name` | Service name +`.Mode` | Service mode (replicated, global) +`.Replicas` | Service replicas +`.Image` | Service image + +When using the `--format` option, the `service ls` command will either +output the data exactly as the template declares or, when using the +`table` directive, includes column headers as well. + +The following example uses a template without headers and outputs the +`ID`, `Mode`, and `Replicas` entries separated by a colon for all services: + +```bash +$ docker service ls --format "{{.ID}}: {{.Mode}} {{.Replicas}}" +0zmvwuiu3vue: replicated 10/10 +fm6uf97exkul: global 5/5 +``` + ## Related information * [service create](service_create.md) diff --git a/docs/reference/commandline/stack_services.md b/docs/reference/commandline/stack_services.md index 62779b4aa1..1814d65356 100644 --- a/docs/reference/commandline/stack_services.md +++ b/docs/reference/commandline/stack_services.md @@ -22,9 +22,10 @@ Usage: docker stack services [OPTIONS] STACK List the services in the stack Options: - -f, --filter value Filter output based on conditions provided - --help Print usage - -q, --quiet Only display IDs + -f, --filter filter Filter output based on conditions provided + --format string Pretty-print services using a Go template + --help Print usage + -q, --quiet Only display IDs ``` Lists the services that are running as part of the specified stack. This @@ -62,6 +63,35 @@ The currently supported filters are: * name (`--filter name=myapp_web`) * label (`--filter label=key=value`) +## Formatting + +The formatting options (`--format`) pretty-prints services output +using a Go template. + +Valid placeholders for the Go template are listed below: + +Placeholder | Description +------------|------------------------------------------------------------------------------------------ +`.ID` | Service ID +`.Name` | Service name +`.Mode` | Service mode (replicated, global) +`.Replicas` | Service replicas +`.Image` | Service image + +When using the `--format` option, the `stack services` command will either +output the data exactly as the template declares or, when using the +`table` directive, includes column headers as well. + +The following example uses a template without headers and outputs the +`ID`, `Mode`, and `Replicas` entries separated by a colon for all services: + +```bash +$ docker stack services --format "{{.ID}}: {{.Mode}} {{.Replicas}}" +0zmvwuiu3vue: replicated 10/10 +fm6uf97exkul: global 5/5 +``` + + ## Related information * [stack deploy](stack_deploy.md)