Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify GroovyCommand to mostly remove the '=' file name argument #8729

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 6 additions & 20 deletions core/src/main/java/hudson/cli/GroovyCommand.java
Expand Up @@ -28,7 +28,6 @@
import groovy.lang.GroovyShell;
import hudson.Extension;
import hudson.model.User;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
Expand All @@ -37,7 +36,6 @@
import jenkins.util.ScriptListener;
import org.apache.commons.io.IOUtils;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;

/**
* Executes the specified groovy script.
Expand All @@ -51,13 +49,10 @@
return Messages.GroovyCommand_ShortDescription();
}

@Argument(metaVar = "SCRIPT", usage = "Script to be executed. Only '=' (to represent stdin) is supported.")
public String script;

/**
* Remaining arguments.
*/
@Argument(metaVar = "ARGUMENTS", index = 1, usage = "Command line arguments to pass into script.")
@Argument(metaVar = "ARGUMENTS", usage = "Command line arguments to pass into script. The first argument is ignored if it's exactly '=' for backward compatibility.")
public List<String> remaining = new ArrayList<>();

@Override
Expand All @@ -67,29 +62,20 @@

final String scriptListenerCorrelationId = String.valueOf(System.identityHashCode(this));

if (!remaining.isEmpty() && "=".equals(remaining.get(0))) {

Check warning on line 65 in core/src/main/java/hudson/cli/GroovyCommand.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 65 is only partially covered, 2 branches are missing
remaining.remove(0);
}

Binding binding = new Binding();
binding.setProperty("out", new PrintWriter(new ScriptListener.ListenerWriter(new OutputStreamWriter(stdout, getClientCharset()), GroovyCommand.class, null, scriptListenerCorrelationId, User.current()), true));
binding.setProperty("stdin", stdin);
binding.setProperty("stdout", stdout);
binding.setProperty("stderr", stderr);

GroovyShell groovy = new GroovyShell(Jenkins.get().getPluginManager().uberClassLoader, binding);
String script = loadScript();
String script = IOUtils.toString(stdin);
ScriptListener.fireScriptExecution(script, binding, GroovyCommand.class, null, scriptListenerCorrelationId, User.current());
groovy.run(script, "RemoteClass", remaining.toArray(new String[0]));
return 0;
}

/**
* Loads the script from the argument.
*/
private String loadScript() throws CmdLineException, IOException, InterruptedException {
if (script == null)
throw new CmdLineException(null, "No script is specified");
if (script.equals("="))
return IOUtils.toString(stdin);

checkChannel();
return null; // never called
}
}