How to call a function more than once in a lambda Expression?

Asked

Viewed 30 times

1

In Jenkins I’m trying to read a file with groovy and for each line check if it contains any of the information I need.

Given the following content of arquivo:

amarelo
verde
vermelho
azul

The code I have is this:

    new File(arquivo).eachLine {
        linha ->
            contemInformacao(linha, "azul")
            contemInformacao(linha, "vermelho")
            contemInformacao(linha, "verde")
            contemInformacao(linha, "amarelo")
    }

    def contemInformação(linha, informacao) {
        if (linha.contains(informacao)) {
            echo "SIM"
        }    
    }

The expected result would be:

SIM
SIM
SIM
SIM

But it’s printed SIM only once.

I think the function contemInformacao is being called only the first time.

Does this actually happen? If yes, how can I get her to be called again for each line?

1 answer

1


Unfortunately Jenkins does not support all groovy options. Especially in older versions.

I’m on 2.241 and the code down worked:

// usa o readFile fornecido pelo Jenkins
def file_text = readFile arquivo_path 

// o .each{} tem funcionado bem ultimamente, mas antigamente nao
file_text.readLines().each { linha ->
        contemInformacao(linha, "azul")
        contemInformacao(linha, "vermelho")
        contemInformacao(linha, "verde")
        contemInformacao(linha, "amarelo")
    }
}

If that doesn’t work, I’m afraid you’ll have to do "the old-fashioned way". Something like that:

def linhas = file_text.tokenize('\r\n')
for (int i=0; i < linhas.size(); ++i) {
   def linha = linhas[i]
   ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.