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

Add -version and -help #349

Merged
merged 3 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we specify version and help it shows the version. Should this be the behaviour? Just thinking out loud.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe include the version in the help output? I recall seeing some tools behave like that, but at the moment I'm not finding any examples.

At a minimum, maybe help should take precedence over version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was modeling this behavior after what jenkins.war does. If you run java -jar jenkins.war --help --version it prints the version. We can definitely switch the priority.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with being consistent with jenkins.war.

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