Go テンプレートまたは JSON パスを使用して Atlas CLI の出力フィールドと形式をカスタマイズできるため、Atlas CLI の出力に基づいてプロセスを自動化するのが容易になります。
Go templates
Goテンプレートは、Atlas CLI コマンド内または別のファイルを使用して指定できます。Goテンプレートの詳細については、パッケージ テンプレートを参照してください。各応答で使用可能なタイプとプロパティについては、Atlas のタイプ を参照してください。
構文
--outputまたは-oオプションを使用して、 コマンドでテンプレートを指定できます。
--output|-o go-template="{{<template>}}"
あるいは、 --outputまたは-oオプションを使用して、 ファイルを介してテンプレートを指定することもできます。
--output|-o go-template-file="<path-to-template-file>"
例
プロジェクト数の取得
次のコマンドは、テンプレートを使用して、指定された組織内のプロジェクトの数を取得します。
atlas projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template="Count: {{.TotalCount}}"
上記のコマンドは、次の出力を返します。
Count: 2
Atlas クラスター接続文字列を取得する
次のAtlas clusters describestring AtlasgetStartedコマンドは、テンプレートを使用して、 という名前の クラスターの接続 を取得します。Atlas へのアクセスには、デフォルトの プロファイルが使用されます。
atlas clusters describe getStarted -o go-template="Parse: {{.SrvAddress}}"
上記のコマンドは、次のようなstringを返します。
Parse: mongodb+srv://getstarted.example.mongodb.net
You can use the MongoDB Shell, mongosh, to connect to the getStarted cluster with the srvAddress and the connection string. This example uses the connection string returned by the previous command for a user with the username User1.
mongo "mongodb+srv://getstarted.example.mongodb.net" --username User1 --password ChangeThisPasswordToSomethingSecure
たとえば、 template.tmplという名前の次のファイルを考えてみましょう。
Projects: {{range .Results}}{{.ID}} {{end}}
次のコマンドは、 template.tmplファイルを使用して、指定された組織内のプロジェクトの ID を検索します。
atlas projects ls --orgId 5ab5cedf5g5h5i5j5kl12mn4 -o go-template-file="template.tmpl"
上記のコマンドは、次の出力を返します。
Projects: 5e2211c17a3e5a48f5497de3 5f455b39749bea5fb575dccc
json-path 出力タイプ
json-path出力タイプは、指定したフィールドにコマンドの結果を制限します。
使用法
コマンドに--outputオプションを追加する場合、 json-pathタイプを指定できます。 結果を評価するには、 json-pathに 式 を指定する必要があります。つまり、コマンドによって返されるjsonフィールドに注意する必要があります。
構文
<command> --output|-o json-path='$<expression>'
json-path 式は Atlas CLI コマンドが返す JSON 要素を指します。$ 文字はルート要素を表します。ルート要素は通常オブジェクトまたは配列です。
有効な文字とその関数のリストについては、JSONPath 式を参照してください。
例
リスト内の最初のAPIキーの説明を返します
次の例では、ユーザーが自分の API キーを Atlas 組織 apiKeys list を使用して検索します。json-path 式は、キーのリスト全体を返すのではなく、出力を最初のキーの desc フィールドに制限します。
atlas organizations apikeys list --output json-path='$[0].desc' owner_key
で同じコマンドを実行すると、 --output jsonAPI から完全な JSON 要素が返されます。json-path を使用して操作するためには、コマンドから返される JSON 構造を理解する必要があります。
次の完全な JSON 出力を参照として使用すると、式 $[0].desc を使用する --output json-path は値 "owner_key" のみを検索して返します。
[ //``$`` represents the outer array. { // ``[0]`` refers to the first element in the array (using a 0-based index). "id": "60e736a95d585d2c9ccf2d19", "desc": "owner_key", //``.desc`` refers to the ``desc`` field of that element. "roles": [ { "orgId": "5d961a949ccf64b4e7f53bac", "roleName": "ORG_OWNER" } ], "privateKey": "********-****-****-c4e26334754f", "publicKey": "xtfmtguk" }, { "id": "d2c9ccf2d1960e736a95d585", "desc": "member_key", "roles": [ { "orgId": "5d961a949ccf64b4e7f53bac", "roleName": "ORG_MEMBER" } ], "privateKey": "********-****-****-c4e26334754f", "publicKey": "vfgcttku" }, ]
リスト内の特定のAPIキーの説明を返します
次の例では、ユーザーが自分の API キーを Atlas 組織 apiKeys list を使用して検索します。json-path 式は、id d2c9ccf2d1960e736a95d585 を持つ特定の JSON オブジェクトの desc フィールドに出力を制限します。
atlas organizations apikeys list --output json-path='$[? @.id=="d2c9ccf2d1960e736a95d585"].desc' member_key
プライベートエンドポイントのステータスを返す
次の例では、ユーザーはAtlas privateEndpoints Amazon Web Services describeを使用してプライベートエンドポイントの情報を検索します。 json-path式は、出力をルート要素のstatusフィールドに制限します。
atlas privateendpoints aws describe 601a4044da900269480a2533 --output json-path='$.status' WAITING_FOR_USER