更新时间:2023-10-16 GMT+08:00

旧输出参数cli-output-rows,cli-output-cols,cli-output-num如何使用?

通过KooCLI调用云服务API,默认会返回json格式的调用结果。KooCLI支持使用“--cli-output-rows”,“--cli-output-cols”,“--cli-output-num”参数,以table格式输出,方便提炼调用结果中的关键信息,如下:

以默认的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"
    }
  ]
}

以table输出调用结果时,“--cli-output-rows”指定json结构体的层级,即表格的数据来源;“--cli-output-cols”指定表格的列名,需要与json结构体中的字段相对应;“--cli-output-num”指定是否打印表格行号,默认值为true,如下图所示:

hcloud ECS NovaListServers --cli-region="ap-southeast-1" --project_id="0dd8cb******************b5a84546" --cli-output-rows="servers" --cli-output-cols="name,id"

使用“--cli-output-rows”,“--cli-output-cols”,“--cli-output-num”参数也可以用于系统命令中,如下所示:

hcloud configure list --cli-output-rows="profiles[]"

hcloud configure list --cli-output-rows="profiles[0]" --cli-output-cols="name,accessKeyId,secretAccessKey,projectId,region"

使用“--cli-output-rows”,“--cli-output-cols”,“--cli-output-num”进行table输出时的其他注意事项如下所示