更新时间:2022-07-07 GMT+08:00

旧输出参数cli-json-filter如何使用?

通过KooCLI调用云服务API,默认会返回json格式的调用结果。KooCLI支持使用“--cli-json-filter”对json结果执行JMESPath查询,方便提炼其中的关键信息,如下:

以默认的json格式输出原调用结果:
hcloud ECS NovaListServers --cli-region="ap-southeast-1" --project_id="0dd8cb****************19b5a84546"
{
  "servers": [
    {
      "name": "ecs-a6b4",
      "links": [
        {
          "rel": "self",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/v2.1/0dd8cb****************19b5a84546/servers/4f06****-****-****-****-****04dd856a"
        },
        {
          "rel": "bookmark",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/servers/4f06****-****-****-****-****04dd856a"
        }
      ],
      "id": "4f06****-****-****-****-****04dd856a"
    },
    {
      "name": "hdn-docker",
      "links": [
        {
          "rel": "self",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/v2.1/0dd8cb****************19b5a84546/servers/6731****-****-****-****-****0bc463f0"
        },
        {
          "rel": "bookmark",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/servers/6731****-****-****-****-****0bc463f0"
        }
      ],
      "id": "6731****-****-****-****-****0bc463f0"
    },
    {
      "name": "ecs-8f88",
      "links": [
        {
          "rel": "self",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/v2.1/0dd8cb****************19b5a84546/servers/06a2****-****-****-****-****c79a1a26"
        },
        {
          "rel": "bookmark",
          "href": "https://ecs.ap-southeast-1.myhuaweicloud.com/0dd8cb****************19b5a84546/servers/06a2****-****-****-****-****c79a1a26"
        }
      ],
      "id": "06a2****-****-****-****-****c79a1a26"
    }
  ]
}

使用“--cli-json-filter”对原json结果的内容执行JMESPath查询,获取每个servers元素的“id”和“name”,并将其重命名为“EcsID”和“EcsName”,如下示例所示:

hcloud ECS NovaListServers --cli-region="ap-southeast-1" --project_id="0dd8cb****************19b5a84546" --cli-json-filter="servers[].{EcsID:id,EcsName:name}"
[
  {
    "EcsID": "4f06****-****-****-****-****04dd856a",
    "EcsName": "ecs-a6b4"
  },
  {
    "EcsID": "6731****-****-****-****-****0bc463f0",
    "EcsName": "hdn-docker"
  },
  {
    "EcsID": "06a2****-****-****-****-****c79a1a26",
    "EcsName": "ecs-8f88"
  }
]

“--cli-json-filter”也可以用于系统命令,例如查询名称为test的配置项的所有custom参数:

hcloud configure list --cli-custom=true --cli-json-filter="profiles[?name=='test'].custom"
[
  {
    "password": {
      "isEncrypted": true,
      "value": "****"
    },
    "projectId": {
      "isEncrypted": false,
      "value": "06810000000000000000000000f89d2e"
    }
  }
]

使用“--cli-json-filter”时的注意事项如下所示