Question

How can I convert this Perl code to Groovy?

How to bypass confirmation prompts of an external process?

I am trying to convert a Perl script to Groovy. The program is loading/delete maestro (job scheduling) jobs automatically. The problem is the delete command will prompt for confirmation (Y/N) on every single job that it finds. I tried the process execute in groovy but will stop at the prompts. The Perl script is writing bunch of Ys to the stream and print it to the handler( if I understood it correctly) to avoid stopping. I am wondering how to do the same thing in Groovy ?

Or any other approach to execute a command and somehow write Y on every confirmation prompt.

Perl Script:

 $maestrostring="";
 while ($x < 1500) {
    $maestrostring .= "y\n";
    $x++;
 }

  # delete the jobs
  open(MAESTRO_CMD, "|ssh mserver /bin/composer delete job=pserver#APPA@") 
  print MAESTRO_CMD $maestrostring;
  close(MAESTRO_CMD);

This is my working groovy code:

    def deleteMaestroJobs (){
            ...
    def commandSched ="ssh $maestro_server /bin/composer delete sched=$primary_server#$app_acronym$app_level@"
    def commandJobs  ="ssh $maestro_server /bin/composer delete job=$primary_server#$app_acronym$app_level@"

    try {
        executeCommand commandJobs
    }
    catch (Exception ex ){
        throw new Exception("Error executing the Maestro Composer [DELETE]")
    }

    try {
        executeCommand commandSched
    }
    catch (Exception ex ){
        throw new Exception("Error executing the Maestro Composer [DELETE]")
    }
}


    def executeCommand(command){
        def process = command.execute()
        process.withWriter { writer ->
          writer.print('y\n' * 1500)
        }
       process.consumeProcessOutput(System.out, System.err)
       process.waitFor()
}
Was it helpful?

Solution

A direct translation would be:

'ssh mserver /bin/composer delete job=pserver#APPA@'.execute().withWriter { writer ->
    writer.print('y\n' * 1500)
}

OTHER TIPS

An other option is to use yes:

system( qq(ssh mserver "yes y | /bin/composer delete job=pserver#APPA\@") );

The standard Unix program yes is made for this sort of thing.

yes | ssh mserver /bin/composer delete job=pserver#APPA@

Note that yes will output an infinite amount given the chance. But when used with a pipe like this, it just stays one step ahead of the other end of the pipe, and waits for it to catch up reading, and finally quits when the OS gives it a SIGPIPE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top