在Groovy中循环浏览文件以创建JSON模板

时间:2019-01-27 18:58:58

标签: json groovy

我正在尝试为Openshift中的服务构造一个JSON模板。我需要设置一个Groovy脚本,该脚本将遍历包含多个端口的文本文件,以便创建JSON模板的port部分。

这是包含端口的文件的样子。

cat ports.txt
  9000
  8090
  7010
  6012

这是我创建模板的常规脚本。该脚本包含端口9000的硬编码文本。

def builder = new groovy.json.JsonBuilder()
builder.apiVersion {
  apiVersion 'v1'
  kind 'Service'
  metadata {
    name 'apache'
    labels {
     app "apache"
     name "apache"
 }
}
spec {
selector {
  app 'apache'
 }
  ports {
  name "9000-tcp"
  protocol "TCP"
  port "9000"
  targetPort "9000"
  }
}
}
println builder.toPrettyString()

运行脚本将显示以下内容:

{
"apiVersion": {
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "name": "apache",
        "labels": {
            "app": "apache",
            "name": "apache"
        }
    },
    "spec": {
        "selector": {
            "app": "apache"
        },
        "ports": {
            "name": "80-tcp",
            "protocol": "TCP",
            "port": "8081"
        }
    }
}

}

我希望最终产品看起来像这样

{
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "name": "apache",
        "labels": {
            "app": "apache",
            "name": "apache"
        }
    },
    "spec": {
        "selector": {
            "app": "apache"
        },
        "ports": {
            "name": "80-tcp",
            "protocol": "TCP",
            "port": "8081"
        }
        "ports": {
            "name": "8090-tcp",
            "protocol": "TCP",
            "port": "8090",
            "targetPort": "8090"
        }
        "ports": {
            "name": "7010-tcp",
            "protocol": "TCP",
            "port": "7010",
            "targetPort": "7010"
        }
        "ports": {
            "name": "6012-tcp",
            "protocol": "TCP",
            "port": "6012",
            "targetPort": "6012"
        }
    }
}
}

如何在此模板中嵌入循环以添加与file.txt文件中的端口一样多的端口。

1 个答案:

答案 0 :(得分:2)

您建议的输出不是有效的JSON ...您不能在与显示的级别相同的水平上拥有多个ports对象...但是您可以将ports制成对象列表,例如所以:

def builder = new groovy.json.JsonBuilder()
builder {
    apiVersion 'v1'
    kind 'Service'
    metadata {
        name 'apache'
        labels {
            app "apache"
            name "apache"
        }
    }
    spec {
        selector {
            app 'apache'
        }
        ports new File('ports.txt').readLines()*.trim().collect { p ->
            [name: "$p-tcp", protocol: "TCP", port: "$p", targetPort: "$p"]
        }
    }
}

println builder.toPrettyString()

输出是:

    {
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {
            "name": "apache",
            "labels": {
                "app": "apache",
                "name": "apache"
            }
        },
        "spec": {
            "selector": {
                "app": "apache"
            },
            "ports": [
                {
                    "name": "9000-tcp",
                    "protocol": "TCP",
                    "port": "9000",
                    "targetPort": "9000"
                },
                {
                    "name": "8090-tcp",
                    "protocol": "TCP",
                    "port": "8090",
                    "targetPort": "8090"
                },
                {
                    "name": "7010-tcp",
                    "protocol": "TCP",
                    "port": "7010",
                    "targetPort": "7010"
                },
                {
                    "name": "6012-tcp",
                    "protocol": "TCP",
                    "port": "6012",
                    "targetPort": "6012"
                }
            ]
        }
    }