Home > other >  groovy output in a map
groovy output in a map

Time:03-12

I have below groovy,

def terraformOutputLocal(Map params) {
terraformOutputAttributes = "terraform output".execute().text
}

def terraformOutputValues = terraformOutputLocal()
println terraformOutputValues

Above is going to give me output as,

billing_mode = "PAY_PER_REQUEST"
name = "testing-table"
stream_view_type = "NEW_AND_OLD_IMAGES"

I would like to get this output as a map as below,

[billing_mode:PAY_PER_REQUEST, name:testing-pipeline-test-table, stream_view_type:NEW_AND_OLD_IMAGES]

Thank you

CodePudding user response:

You can split on newlines, then split each line on = and collect them back into a map:

def terraformOutputLocal(Map params) {
    terraformOutputAttributes = "terraform output".execute().text
        .split('\n')*.split(' = ').collectEntries()
}

To remove double quotes from the strings, you can do:

def terraformOutputLocal(Map params) {
    terraformOutputAttributes = "terraform output".execute().text
        .split('\n')*.split(' = ')*.toList()
        .collectEntries { a, b -> [a, b[0] == '"' ? b[1..-2] : b] }
}
  • Related