Simon
Simon

Reputation: 121

groovy output in a map

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

Upvotes: 0

Views: 235

Answers (2)

daggett
daggett

Reputation: 28564

for this format you could use 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'

Upvotes: 0

tim_yates
tim_yates

Reputation: 171084

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] }
}

Upvotes: 1

Related Questions