Skip to content

Commit 8fcf396

Browse files
pierwillcorryroot
andauthored
(DOCSP-25515): Consolidate CLI output format docs (#806)
Co-authored-by: corryroot <[email protected]>
1 parent f02b130 commit 8fcf396

File tree

5 files changed

+255
-248
lines changed

5 files changed

+255
-248
lines changed

config/redirects

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ raw: ${prefix}/ -> ${base}/stable/
66
raw: ${prefix} -> ${base}/stable/
77

88
#For v1.17 and later, after migration to autogenerated reference docs
9+
[v1.17-*]: ${prefix}/${version}/reference/json-path/ -> ${base}/${version}/configure/custom-output/
10+
[v1.17-*]: ${prefix}/${version}/configure/go-template-output/ -> ${base}/${version}/configure/custom-output/
911
[v1.17-*]: ${prefix}/${version}/reference/atlas -> ${base}/${version}/command/mongocli-atlas/
1012
[v1.17-*]: ${prefix}/${version}/reference/atlas/alert-commands -> ${base}/${version}/command/mongocli-atlas-alerts/
1113
[v1.17-*]: ${prefix}/${version}/reference/atlas/alerts-ack -> ${base}/${version}/command/mongocli-atlas-alerts-acknowledge/

source/configure.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Configure the {+mcli+}
1818
/configure/configuration-file
1919
/configure/environment-variables
2020
/configure/autocomplete
21-
/configure/go-template-output
21+
/configure/custom-output
2222

2323
You can define the settings that the {+mcli+} uses to interact with
2424
MongoDB services like |service|, |cloud-short|, and |onprem| by using

source/configure/custom-output.txt

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
.. _go-template-output:
2+
3+
=============================
4+
Customize the {+mcli+} Output
5+
=============================
6+
7+
.. default-domain:: mongodb
8+
9+
You can customize the {+mcli+} output fields and format using a ``Go``
10+
template or a JSON path.
11+
12+
Go templates
13+
------------
14+
15+
You can specify the Go template with the command for a simple output
16+
or through a separate file for a complex output. For information on the
17+
template, see `Package template <https://golang.org/pkg/text/template/>`__.
18+
For information on the types and properties available for each response, see:
19+
20+
- `Atlas types <https://go.mongodb.org/atlas/mongodbatlas/>`__
21+
- `Cloud Manager types <https://go.mongodb.org/ops-manager/opsmngr/>`__
22+
- `Ops Manager types <https://go.mongodb.org/ops-manager/opsmngr/>`__
23+
24+
Syntax
25+
~~~~~~
26+
27+
You can specify a template with the command using the ``--output`` or
28+
``-o`` option:
29+
30+
.. code-block:: shell
31+
:copyable: false
32+
33+
--output|-o go-template="{{<template>}}"
34+
35+
You can specify a template using a file using the ``--output`` or
36+
``-o`` option:
37+
38+
.. code-block:: shell
39+
:copyable: false
40+
41+
--output|-o go-template-file="<path-to-template-file>"
42+
43+
Examples
44+
~~~~~~~~
45+
46+
.. tabs::
47+
48+
.. tab:: Specify Template With the Command
49+
:tabid: template-command
50+
51+
Retrieve the Number of Projects
52+
```````````````````````````````
53+
54+
The following command uses the template to retrieve a count of
55+
the number of projects in the specified organization using the
56+
{+default-profile+}:
57+
58+
.. code-block:: shell
59+
60+
mongocli iam projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template="Count: {{.TotalCount}}"
61+
62+
The preceding command returns the following output:
63+
64+
.. code-block:: shell
65+
:copyable: false
66+
67+
Count: 2
68+
69+
.. _quick-start-atlas-retrieve-conection-string:
70+
71+
Retrieve Your |service| Cluster Connection String
72+
`````````````````````````````````````````````````
73+
74+
75+
The following :ref:`mongocli-atlas-clusters-describe` command
76+
uses the template to retrieve the connection string for an
77+
|service| cluster named ``getStarted``. It uses the default
78+
profile for accessing |service|.
79+
80+
.. code-block:: sh
81+
82+
mongocli atlas clusters describe getStarted -o go-template="Parse: {{.SrvAddress}}"
83+
84+
The previous command returns a string similar to the following:
85+
86+
.. code-block:: sh
87+
:copyable: false
88+
89+
Parse: mongodb+srv://getstarted.example.mongodb.net
90+
91+
You can use the MongoDB Shell, {+mongosh+}, to connect to the
92+
``getStarted``
93+
cluster with the ``srvAddress`` and the :manual:`connection
94+
string
95+
</reference/connection-string/#connection-string-options>`. This
96+
example uses the following to connect to the |service| cluster:
97+
98+
- The connection string returned by the previous command
99+
- The username and password created in this :ref:`tutorial
100+
<mcli-quick-start-atlas>`
101+
102+
.. code-block::
103+
104+
mongo "mongodb+srv://getstarted.example.mongodb.net" --username User1 --password ChangeThisPasswordToSomethingSecure
105+
106+
.. tab:: Specify Template Using a File
107+
:tabid: template-file
108+
109+
For example, consider the following file named ``template.tmpl``:
110+
111+
.. code-block:: shell
112+
113+
Projects: {{range .Results}}{{.ID}} {{end}}
114+
115+
The following command uses the ``template.tmpl`` file to retrieve
116+
the IDs of the projects in the specified organization using the
117+
{+default-profile+}:
118+
119+
.. code-block:: shell
120+
121+
mongocli iam projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template-file="template.tmpl"
122+
123+
The preceding command returns the following output:
124+
125+
.. code-block:: shell
126+
:copyable: false
127+
128+
Projects: 5e2211c17a3e5a48f5497de3 5f455b39749bea5fb575dccc
129+
130+
.. _json-path-output-option:
131+
132+
``json-path`` Output Type
133+
-------------------------
134+
135+
.. default-domain:: mongodb
136+
137+
.. contents:: On this page
138+
:local:
139+
:backlinks: none
140+
:depth: 1
141+
:class: singlecol
142+
143+
The ``json-path`` output type limits the results of a command to the
144+
fields you specify.
145+
146+
Usage
147+
~~~~~
148+
149+
When you add the ``--output`` option to a command, you can specify the
150+
type ``json-path``. You must provide ``json-path`` with an expression
151+
to evaluate against your results, which means you must be aware of the
152+
``json`` fields returned by your command.
153+
154+
Syntax
155+
~~~~~~
156+
157+
.. code-block:: shell
158+
:copyable: false
159+
160+
<command> --output|-o json-path='$<expression>'
161+
162+
``json-path`` expressions refer to the |json| element that a {+mcli+}
163+
command returns. The ``$`` character represents the root element, which
164+
is usually an object or an array.
165+
166+
For a list of valid characters and their functions, see `JSONPath expressions <https://goessner.net/articles/JsonPath/index.html#e2>`__.
167+
168+
Examples
169+
~~~~~~~~
170+
171+
Return the description of the first |api| key in a list
172+
```````````````````````````````````````````````````````
173+
174+
In the following example, a user retrieves their |api| keys with
175+
:ref:`mongocli-iam-organizations-apiKeys-list`. The ``json-path``
176+
expression limits the output to the ``desc`` field of the first key,
177+
rather than returning the entire list of keys.
178+
179+
.. code-block:: shell
180+
:copyable: false
181+
182+
mongocli iam organization apikeys list --output json-path='$[0].desc'
183+
> owner_key
184+
185+
Running the same command with ``--output json`` returns the full |json|
186+
element from the |api|. It's important to understand the |json|
187+
structure returned by a command in order to operate on it with
188+
``json-path``.
189+
190+
Using the the following full |json| output for reference, the
191+
``--output json-path`` with the expression ``$[0].desc`` finds and
192+
returns only the value ``"owner_key"``:
193+
194+
.. code-block:: json
195+
:copyable: false
196+
:emphasize-lines: 1-2, 4
197+
198+
[ //``$`` represents the outer array.
199+
{ // ``[0]`` refers to the first element in the array (using a 0-based index).
200+
"id": "60e736a95d585d2c9ccf2d19",
201+
"desc": "owner_key", //``.desc`` refers to the ``desc`` field of that element.
202+
"roles": [
203+
{
204+
"orgId": "5d961a949ccf64b4e7f53bac",
205+
"roleName": "ORG_OWNER"
206+
}
207+
],
208+
"privateKey": "********-****-****-c4e26334754f",
209+
"publicKey": "xtfmtguk"
210+
},
211+
{
212+
"id": "d2c9ccf2d1960e736a95d585",
213+
"desc": "member_key",
214+
"roles": [
215+
{
216+
"orgId": "5d961a949ccf64b4e7f53bac",
217+
"roleName": "ORG_MEMBER"
218+
}
219+
],
220+
"privateKey": "********-****-****-c4e26334754f",
221+
"publicKey": "vfgcttku"
222+
},
223+
]
224+
225+
Return the description of a specific |api| key in a list
226+
````````````````````````````````````````````````````````
227+
228+
In the following example, a user retrieves their |api| keys with
229+
:ref:`mongocli-iam-organizations-apiKeys-list`. The ``json-path``
230+
expression limits the output to the ``desc`` field of the specific
231+
|json| object with ``id`` ``d2c9ccf2d1960e736a95d585``.
232+
233+
.. code-block:: shell
234+
:copyable: false
235+
236+
mongocli iam organization apikeys list --output json-path='$[? @.id=="d2c9ccf2d1960e736a95d585"].desc'
237+
> member_key
238+
239+
Return the status of a private endpoint
240+
```````````````````````````````````````
241+
242+
In the following example, a user retrieves information for a
243+
private endpoint with
244+
:ref:`mongocli-atlas-privateEndpoints-aws-describe`. The ``json-path``
245+
expression limits the output to the ``status`` field of the root
246+
element.
247+
248+
.. code-block:: shell
249+
:copyable: false
250+
251+
mongocli atlas privateendpoint aws describe 601a4044da900269480a2533 --output json-path='$.status'
252+
> WAITING_FOR_USER

source/configure/go-template-output.txt

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)