You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何验证获取AWS ECR仓库列表的Groovy脚本返回数组值?

验证Groovy脚本返回数组值的方法

你编写的用于从AWS ECR获取仓库名的Groovy脚本如下:

def profile = "ecr"
def region = "us-east-1"
def cmd_output = "aws ecr describe-repositories --profile $profile --region $region --output yaml".execute()
def awk_cmd_output = cmd_output | ['awk', '/repositoryName:/ {print $2}'].execute()
def repo_names = awk_cmd_output.text.tokenize().reverse()
return repo_names

在Linux Shell中执行对应AWS命令,返回的JSON结构示例如下:

root@test:~# aws ecr describe-repositories --profile ecr  --region us-east-1
{
    "repositories": [
        {
            "repositoryArn": "arn23522033165aws:ecr:us-east-1:73522033165:repository/test",
            "registryId": "23522033165",
            "repositoryName": "test",
            "repositoryUri": "23522033165.dkr.ecr.us-east-1.amazonaws.com/test",
            "createdAt": "2023-09-07T22:30:50+00:00",
            "imageTagMutability": "MUTABLE",
            "imageScanningConfiguration": {
                "scanOnPush": false
            },
            "encryptionConfiguration": {
                "encryptionType": "AES256"
            }
        },
        {
            "repositoryArn": "arn:aws:ecr:us-east-1:23522033165:repository/test",
            "registryId": "23522033165",
            "repositoryName": "test-2",
            "repositoryUri": "23522033165.dkr.ecr.us-east-1.amazonaws.com/test",
            "createdAt": "2023-09-09T19:54:04+00:00",
            "imageTagMutability": "MUTABLE",
            "imageScanningConfiguration": {
                "scanOnPush": false
            },
            "encryptionConfiguration": {
                "encryptionType": "AES256"
            }
        }
    ]
}

几种验证脚本返回值是否为数组(Groovy中List即动态数组)的方法:

  • 直接在脚本中添加调试输出
    在return repo_names前添加两行代码,运行脚本后查看控制台输出:

    // 打印返回值的类型
    println "返回值类型:${repo_names.getClass().name}"
    // 打印返回值的具体内容
    println "返回值内容:${repo_names}"
    

    如果输出类型包含ArrayList或List,就说明返回的是数组结构;同时也能直观看到仓库名列表。

  • 用Groovy控制台验证
    将脚本完整复制到Groovy控制台(执行groovyConsole命令启动),执行后在结果面板可以直接看到返回值的类型和内容。

  • 调用脚本后判断类型
    如果你的脚本是作为方法被其他代码调用,可通过类型判断验证:

    def repoList = yourScriptMethod() // 替换为实际调用脚本的方式
    if (repoList instanceof List) {
        println "返回的是数组(List)类型"
    } else {
        println "返回的不是数组类型"
    }
    

额外说明:原脚本中tokenize()方法本身返回List类型,reverse()方法也会返回List,所以理论上脚本返回的肯定是数组结构,验证只是确认行为符合预期。

内容的提问来源于stack exchange,提问作者Peter Penzov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 05:10:33