diff --git a/src/main/java/hudson/remoting/Launcher.java b/src/main/java/hudson/remoting/Launcher.java index 6a252af57..bbaf7ced1 100644 --- a/src/main/java/hudson/remoting/Launcher.java +++ b/src/main/java/hudson/remoting/Launcher.java @@ -270,12 +270,31 @@ public void setNoCertificateCheck(boolean ignored) throws NoSuchAlgorithmExcepti depends = "-workDir") @Nonnull public boolean failIfWorkDirIsMissing = WorkDirManager.DEFAULT_FAIL_IF_WORKDIR_IS_MISSING; + + /** + * Shows help message and then exits + * @since 3.36 + */ + @Option(name="-help",usage="Show this help message") + public boolean showHelp = false; + + /** + * Shows version information and then exits + * @since 3.36 + */ + @Option(name="-version",usage="Shows the version of the remoting jar and then exits") + public boolean showVersion = false; + public static void main(String... args) throws Exception { Launcher launcher = new Launcher(); CmdLineParser parser = new CmdLineParser(launcher); try { parser.parseArgument(args); + if (launcher.showHelp && !launcher.showVersion) { + parser.printUsage(System.out); + return; + } launcher.run(); } catch (CmdLineException e) { System.err.println(e.getMessage()); @@ -287,6 +306,13 @@ public static void main(String... args) throws Exception { @edu.umd.cs.findbugs.annotations.SuppressFBWarnings("DM_DEFAULT_ENCODING") // log file, just like console output, should be in platform default encoding public void run() throws Exception { + if (showVersion) { + String version = Util.getVersion(); + if(version != null) { + System.out.println(version); + } + return; + } // Create and verify working directory and logging // TODO: The pass-through for the JNLP mode has been added in JENKINS-39817. But we still need to keep this parameter in diff --git a/src/main/java/hudson/remoting/Util.java b/src/main/java/hudson/remoting/Util.java index 92dde7405..8878f5725 100644 --- a/src/main/java/hudson/remoting/Util.java +++ b/src/main/java/hudson/remoting/Util.java @@ -22,6 +22,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Base64; +import java.util.Enumeration; +import java.util.jar.JarFile; +import java.util.jar.Manifest; /** * Misc. I/O utilities @@ -160,4 +163,25 @@ static void mkdirs(@Nonnull File file) throws IOException { Files.createDirectories(PathUtils.fileToPath(file)); } + static public String getVersion() { + String version = "unknown"; + try { + Enumeration resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME); + while (resEnum.hasMoreElements()) { + URL url = (URL) resEnum.nextElement(); + try(InputStream is = url.openStream()) { + if (is != null) { + Manifest manifest = new Manifest(is); + version = manifest.getMainAttributes().getValue("Version"); + if(version != null) { + break; + } + } + } + } + } catch (IOException e) { + System.out.println("Could not access manifest"); + } + return version; + } } diff --git a/src/main/java/hudson/remoting/jnlp/Main.java b/src/main/java/hudson/remoting/jnlp/Main.java index 22d7d7d3e..b65ec8dd3 100644 --- a/src/main/java/hudson/remoting/jnlp/Main.java +++ b/src/main/java/hudson/remoting/jnlp/Main.java @@ -32,6 +32,7 @@ import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import hudson.remoting.Util; import org.jenkinsci.remoting.engine.WorkDirManager; import org.jenkinsci.remoting.util.PathUtils; import org.kohsuke.args4j.Option; @@ -202,6 +203,20 @@ public class Main { usage="Specify the remoting protocols to attempt when instanceIdentity is provided.") public List protocols = new ArrayList<>(); + /** + * Shows help message and then exits + * @since 3.36 + */ + @Option(name="-help",usage="Show this help message") + public boolean showHelp = false; + + /** + * Shows version information and then exits + * @since 3.36 + */ + @Option(name="-version",usage="Shows the version of the remoting jar and then exits") + public boolean showVersion = false; + /** * Two mandatory parameters: secret key, and agent name. */ @@ -239,6 +254,13 @@ public static void _main(String[] args) throws IOException, InterruptedException Main m = new Main(); CmdLineParser p = new CmdLineParser(m); p.parseArgument(args); + if (m.showHelp && !m.showVersion) { + p.printUsage(System.out); + return; + } else if(m.showVersion) { + System.out.println(Util.getVersion()); + return; + } if(m.args.size()!=2) { throw new CmdLineException(p, "two arguments required, but got " + m.args, null); }