groovy 在地图中输出
groovy output in a map
我有以下groovy,
def terraformOutputLocal(Map params) {
terraformOutputAttributes = "terraform output".execute().text
}
def terraformOutputValues = terraformOutputLocal()
println terraformOutputValues
以上将给我输出,
billing_mode = "PAY_PER_REQUEST"
name = "testing-table"
stream_view_type = "NEW_AND_OLD_IMAGES"
我想将此输出作为地图,如下所示,
[billing_mode:PAY_PER_REQUEST, name:testing-pipeline-test-table, stream_view_type:NEW_AND_OLD_IMAGES]
谢谢
您可以按换行符拆分,然后按 =
拆分每一行并将它们收集回地图中:
def terraformOutputLocal(Map params) {
terraformOutputAttributes = "terraform output".execute().text
.split('\n')*.split(' = ').collectEntries()
}
要从字符串中删除双引号,您可以这样做:
def terraformOutputLocal(Map params) {
terraformOutputAttributes = "terraform output".execute().text
.split('\n')*.split(' = ')*.toList()
.collectEntries { a, b -> [a, b[0] == '"' ? b[1..-2] : b] }
}
对于这种格式,您可以使用 ConfigSlurper
def terraformOutputValues = '''
billing_mode = "PAY_PER_REQUEST"
name = "testing-table"
stream_view_type = "NEW_AND_OLD_IMAGES"
'''
def c = new ConfigSlurper().parse(terraformOutputValues)
assert c.billing_mode == 'PAY_PER_REQUEST'
我有以下groovy,
def terraformOutputLocal(Map params) {
terraformOutputAttributes = "terraform output".execute().text
}
def terraformOutputValues = terraformOutputLocal()
println terraformOutputValues
以上将给我输出,
billing_mode = "PAY_PER_REQUEST"
name = "testing-table"
stream_view_type = "NEW_AND_OLD_IMAGES"
我想将此输出作为地图,如下所示,
[billing_mode:PAY_PER_REQUEST, name:testing-pipeline-test-table, stream_view_type:NEW_AND_OLD_IMAGES]
谢谢
您可以按换行符拆分,然后按 =
拆分每一行并将它们收集回地图中:
def terraformOutputLocal(Map params) {
terraformOutputAttributes = "terraform output".execute().text
.split('\n')*.split(' = ').collectEntries()
}
要从字符串中删除双引号,您可以这样做:
def terraformOutputLocal(Map params) {
terraformOutputAttributes = "terraform output".execute().text
.split('\n')*.split(' = ')*.toList()
.collectEntries { a, b -> [a, b[0] == '"' ? b[1..-2] : b] }
}
对于这种格式,您可以使用 ConfigSlurper
def terraformOutputValues = '''
billing_mode = "PAY_PER_REQUEST"
name = "testing-table"
stream_view_type = "NEW_AND_OLD_IMAGES"
'''
def c = new ConfigSlurper().parse(terraformOutputValues)
assert c.billing_mode == 'PAY_PER_REQUEST'