|
| 1 | +.. _go-template-output: |
| 2 | + |
| 3 | +==================================================== |
| 4 | +Customize the {+mcli+} Output Using a Go Template |
| 5 | +==================================================== |
| 6 | + |
| 7 | +You can customize the {+mcli+} output fields and format using a ``Go`` |
| 8 | +template. The template can be specified with the command for a simple output |
| 9 | +or through a separate file for a complex output. For information on the |
| 10 | +template, see `Package template <https://golang.org/pkg/text/template/>`__. |
| 11 | +For information on the types and properties available for each response, see: |
| 12 | + |
| 13 | +- `Atlas types <https://go.mongodb.org/atlas/mongodbatlas/>`__ |
| 14 | +- `Clour Manager types <https://go.mongodb.org/ops-manager/opsmngr/>`__ |
| 15 | +- `Ops Manager types <https://go.mongodb.org/ops-manager/opsmngr/>`__ |
| 16 | + |
| 17 | +Syntax |
| 18 | +------ |
| 19 | + |
| 20 | +You can specify a template with the command using the ``--output`` or |
| 21 | +``-o`` option: |
| 22 | + |
| 23 | +.. code-block:: shell |
| 24 | + :copyable: false |
| 25 | + |
| 26 | + --output|-o go-template="{{<template>}}" |
| 27 | + |
| 28 | +You can specify a template using a file using the ``--output`` or |
| 29 | +``-o`` option: |
| 30 | + |
| 31 | +.. code-block:: shell |
| 32 | + :copyable: false |
| 33 | + |
| 34 | + --output|-o go-template-file="<path-to-template-file>" |
| 35 | + |
| 36 | +Examples |
| 37 | +-------- |
| 38 | + |
| 39 | +.. tabs:: |
| 40 | + |
| 41 | + .. tab:: Specify Template With the Command |
| 42 | + |
| 43 | + For example, the following command uses the template to retrieve a |
| 44 | + count of the number of projects in the specified organization using the |
| 45 | + {+default-profile+}: |
| 46 | + |
| 47 | + .. code-block:: shell |
| 48 | + |
| 49 | + mongocli iam projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template="Count: {{.TotalCount}}" |
| 50 | + |
| 51 | + The preceding command returns the following output: |
| 52 | + |
| 53 | + .. code-block:: shell |
| 54 | + :copyable: false |
| 55 | + |
| 56 | + Count: 2 |
| 57 | + |
| 58 | + .. tab:: Specify Template Using a File |
| 59 | + |
| 60 | + For example, consider the following file named ``template.tmpl``: |
| 61 | + |
| 62 | + .. code-block:: shell |
| 63 | + |
| 64 | + Projects: {{range .Results}}{{.ID}} {{end}} |
| 65 | + |
| 66 | + The following command uses the ``template.tmpl`` file to retrieve |
| 67 | + the IDs of the projects in the specified organization using the |
| 68 | + {+default-profile+}: |
| 69 | + |
| 70 | + .. code-block:: shell |
| 71 | + |
| 72 | + mongocli iam projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template-file="template.tmpl" |
| 73 | + |
| 74 | + The preceding command returns the following output: |
| 75 | + |
| 76 | + .. code-block:: shell |
| 77 | + :copyable: false |
| 78 | + |
| 79 | + Projects: 5e2211c17a3e5a48f5497de3 5f455b39749bea5fb575dccc |
0 commit comments