Skip to content

Commit

Permalink
Move "dump config" flags to a more sensible place
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed May 20, 2020
1 parent 4a2559f commit a0cd316
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
Expand Up @@ -20,16 +20,15 @@
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.internal.DefaultConsole;

import com.google.common.collect.Sets;
import org.openqa.selenium.cli.CliCommand;
import org.openqa.selenium.grid.config.HasRoles;
import org.openqa.selenium.grid.config.AnnotatedConfig;
import org.openqa.selenium.grid.config.CompoundConfig;
import org.openqa.selenium.grid.config.ConcatenatingConfig;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigFlags;
import org.openqa.selenium.grid.config.EnvConfig;
import org.openqa.selenium.grid.config.HasRoles;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.server.HelpFlags;

Expand Down Expand Up @@ -84,7 +83,7 @@ public final Executable configure(PrintStream out, PrintStream err, String... ar

Config config = new CompoundConfig(allConfigs.toArray(new Config[0]));

if (helpFlags.dumpConfig(config, out)) {
if (configFlags.dumpConfig(config, out)) {
return;
}

Expand Down
33 changes: 33 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/ConfigFlags.java
Expand Up @@ -19,15 +19,25 @@

import com.beust.jcommander.Parameter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.openqa.selenium.json.Json;

import java.io.PrintStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class ConfigFlags {

private static final ImmutableSet<String> IGNORED_SECTIONS = ImmutableSet.of("java", "lc", "term");

@Parameter(names = "--config", description = "Config file to read from (may be specified more than once)")
private List<Path> configFiles;

@Parameter(names = "--dump-config", description = "Dump the config of the server as JSON.", hidden = true)
private boolean dumpConfig;

public Config readConfigFiles() {
if (configFiles == null || configFiles.isEmpty()) {
return new MapConfig(ImmutableMap.of());
Expand All @@ -38,4 +48,27 @@ public Config readConfigFiles() {
.map(Configs::from)
.toArray(Config[]::new));
}

public boolean dumpConfig(Config config, PrintStream dumpTo) {
if (!dumpConfig) {
return false;
}

Map<String, Map<String, Object>> toOutput = new TreeMap<>();
for (String section : config.getSectionNames()) {
if (section.isEmpty() || IGNORED_SECTIONS.contains(section)) {
continue;
}

config.getOptions(section).forEach(option ->
config.get(section, option).ifPresent(value ->
toOutput.computeIfAbsent(section, ignored -> new TreeMap<>()).put(option, value)
)
);
}

dumpTo.print(new Json().toJson(toOutput));

return true;
}
}
27 changes: 0 additions & 27 deletions java/server/src/org/openqa/selenium/grid/server/HelpFlags.java
Expand Up @@ -30,16 +30,12 @@

public class HelpFlags {

private static final ImmutableSet<String> IGNORED_SECTIONS = ImmutableSet.of("java", "lc", "term");
@Parameter(names = {"-h", "-help", "--help", "/?"}, help = true, hidden = true)
private boolean help;

@Parameter(names = "--version", description = "Displays the version and exits.")
private boolean version;

@Parameter(names = "--dump-config", description = "Dump the config of the server as JSON.", hidden = true)
private boolean dumpConfig;

public boolean displayHelp(JCommander commander, PrintStream outputTo) {
if (version) {
BuildInfo info = new BuildInfo();
Expand All @@ -60,27 +56,4 @@ public boolean displayHelp(JCommander commander, PrintStream outputTo) {

return false;
}

public boolean dumpConfig(Config config, PrintStream dumpTo) {
if (!dumpConfig) {
return false;
}

Map<String, Map<String, Object>> toOutput = new TreeMap<>();
for (String section : config.getSectionNames()) {
if (section.isEmpty() || IGNORED_SECTIONS.contains(section)) {
continue;
}

config.getOptions(section).forEach(option ->
config.get(section, option).ifPresent(value ->
toOutput.computeIfAbsent(section, ignored -> new TreeMap<>()).put(option, value)
)
);
}

dumpTo.print(new Json().toJson(toOutput));

return true;
}
}

0 comments on commit a0cd316

Please sign in to comment.