Sanjay M. P.
Sanjay M. P.

Reputation: 1009

Groovy DSL to compare string

I'm trying to dynamically load Job into Jenkins, there is a file with Job Name and GHE URL:

GHE file urls:

options A
https://github.com/I/A/build.groovy

options B
https://github.com/I/B/build.groovy

options C
https://github.com/I/C/build.groovy

with the below bash script I could create a new Repo (A,B,C) dir and use GHE URL in the pipeline scm, how can i achieve this in groovy dsl :

while read -r line; do
    case "$line" in
        options*)
            jenkins_dir=$(echo "$line" | cut -d ' ')
            mkdir -p ${jenkins_dir}
            ;;
        http://github*)
            wget -N $line -P ${jenkins_dir}
            ;;
        *)
            echo "$line"
            ;;
    esac
done < jenkins-ghe.urls

Groovy DSL version:

def String[] loadJobURL(String basePath) {
    def url = []
    new File('/path/to/file').eachLine { line ->
    switch("$line") {            
         
        case "options*)":
            
        case "http://github*)":
           
}

there are couple of failures, not very sure of the syntax wrt groovy dsl, want the dsl script to recognize both the lines. Kindly suggest, Thanks !

Upvotes: 0

Views: 270

Answers (1)

Matias Bjarland
Matias Bjarland

Reputation: 4482

Not entirely sure what you would like to accomplish here but one way to do the parsing and handling in groovy would be something like this:

new File('data.txt').readLines().inject(null) { last, line -> 
  switch(line.trim()) {
    case '':
      return last
    case ~/options.*/: 
      def dir = new File(line)
      if (!dir.mkdirs()) throw new RuntimeException("failed to create dir $dir")
      return dir
    case ~/http.*/:
      new File(last, 'data.txt').text = line.toURL().text
      return null
    default: throw new RuntimeException("invalid data at $line")
  }
}

which, when run against a data file that looks like this:

options A
https://raw.githubusercontent.com/apache/groovy/master/build.gradle

options B
https://raw.githubusercontent.com/apache/groovy/master/benchmark/bench.groovy

options C
https://raw.githubusercontent.com/apache/groovy/master/buildSrc/build.gradle

creates the following directory structure:

─➤ tree
.
├── data.txt
├── options A
│   └── data.txt
├── options B
│   └── data.txt
├── options C
│   └── data.txt
└── solution.groovy

where the data.txt files contain the data loaded from the corresponding urls.

I'm using the somewhat more advanced inject construct to make the parsing a tad more robust while staying "functional" (i.e. not reverting to for loops, each etc). The code might have ended up somewhat easier to read by not doing that but it's the best fit I could think of when perusing the groovy collections/iterable/list methods.

Inject iterates over the lines, one at a time (where line gets the String representing the current line) while retaining the result from the previous iteration in the last variable. This is useful as we can save the directory created on the option line so that we can use it on the url line. The switch is using regex matching.

Upvotes: 1

Related Questions