Skip to content

Commit

Permalink
Merge pull request #349 from slide/master
Browse files Browse the repository at this point in the history
Add -version and -help
  • Loading branch information
jeffret-b committed Nov 2, 2019
2 parents 9099602 + 89328fd commit d1c8a4c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/hudson/remoting/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/hudson/remoting/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
22 changes: 22 additions & 0 deletions src/main/java/hudson/remoting/jnlp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -202,6 +203,20 @@ public class Main {
usage="Specify the remoting protocols to attempt when instanceIdentity is provided.")
public List<String> 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.
*/
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit d1c8a4c

Please sign in to comment.