Skip to content

Commit

Permalink
Do not wipe options with default values when using --arguments-file (#…
Browse files Browse the repository at this point in the history
…635)

Co-authored-by: Daniel Beck <daniel-beck@users.noreply.github.com>
  • Loading branch information
daniel-beck and daniel-beck authored Aug 12, 2022
1 parent c576284 commit 1d2ea6d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/io/jenkins/update_center/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.util.VersionNumber;
import io.jenkins.lib.support_log_formatter.SupportLogFormatter;
import io.jenkins.update_center.args4j.Default;
import io.jenkins.update_center.args4j.LevelOptionHandler;
import io.jenkins.update_center.json.PlatformPluginsRoot;
import io.jenkins.update_center.json.RecentReleasesRoot;
Expand Down Expand Up @@ -136,9 +137,11 @@ public class Main {
public boolean prettyPrint;

@Option(name = "--id", usage = "Uniquely identifies this update center. We recommend you use a dot-separated name like \"com.sun.wts.jenkins\". This value is not exposed to users, but instead internally used by Jenkins.")
@Default(value = "default")
@CheckForNull public String id = "default";

@Option(name = "--connection-check-url", usage = "Specify an URL of the 'always up' server for performing connection check.")
@Default(value = "https://www.google.com/")
@CheckForNull public String connectionCheckUrl = "https://www.google.com/";


Expand Down Expand Up @@ -210,13 +213,21 @@ private void resetArguments(Object... optionHolders) {
if (field.getAnnotation(Option.class) != null && !Modifier.isStatic(field.getModifiers())) {
if (Object.class.isAssignableFrom(field.getType())) {
try {
field.set(o, null);
if (field.getAnnotation(Default.class) != null) {
field.set(o, field.getAnnotation(Default.class).value());
} else {
field.set(o, null);
}
} catch (IllegalAccessException e) {
LOGGER.log(Level.WARNING, "Failed to reset argument", e);
}
} else if (boolean.class.isAssignableFrom(field.getType())) {
try {
field.set(o, false);
if (field.getAnnotation(Default.class) != null) {
field.set(o, Boolean.parseBoolean(field.getAnnotation(Default.class).value()));
} else {
field.set(o, false);
}
} catch (IllegalAccessException e) {
LOGGER.log(Level.WARNING, "Failed to reset boolean option", e);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/jenkins/update_center/args4j/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.jenkins.update_center.args4j;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* Provide a way to define default values for {@link org.kohsuke.args4j.Option}s
* that cannot be reset in {@link io.jenkins.update_center.Main#run} when taking
* an arguments file.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Default {
String value();
}

0 comments on commit 1d2ea6d

Please sign in to comment.