|
25 | 25 | import com.beust.jcommander.Parameters; |
26 | 26 | import com.google.common.annotations.VisibleForTesting; |
27 | 27 | import com.google.common.collect.ImmutableSet; |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.util.Properties; |
28 | 31 | import java.util.List; |
29 | 32 | import java.util.Set; |
30 | 33 | import org.apache.commons.logging.LogFactory; |
@@ -73,6 +76,11 @@ public class Main extends Configured implements Tool { |
73 | 76 | description = "Set a configuration property (format: key=value). Can be specified multiple times.") |
74 | 77 | private List<String> confProperties; |
75 | 78 |
|
| 79 | + @Parameter( |
| 80 | + names = {"--config-file"}, |
| 81 | + description = "Path to a properties configuration file containing key=value pairs.") |
| 82 | + private String configFilePath; |
| 83 | + |
76 | 84 | @VisibleForTesting |
77 | 85 | @Parameter(names = "--dollar-zero", description = "A way for the runtime path to be passed in", hidden = true) |
78 | 86 | String programName = DEFAULT_PROGRAM_NAME; |
@@ -172,6 +180,18 @@ public int run(String[] args) throws Exception { |
172 | 180 | // If the command does not support the configs, it would simply be ignored. |
173 | 181 | if (command instanceof Configurable) { |
174 | 182 | Configuration merged = new Configuration(getConf()); |
| 183 | + |
| 184 | + if (configFilePath != null) { |
| 185 | + try (InputStream in = new FileInputStream(configFilePath)) { |
| 186 | + Properties props = new Properties(); |
| 187 | + props.load(in); |
| 188 | + props.forEach((key, value) -> merged.set(key.toString(), value.toString())); |
| 189 | + console.debug("Loaded configuration from file: {}", configFilePath); |
| 190 | + } catch (Exception e) { |
| 191 | + throw new IllegalArgumentException("Failed to load config file '" + configFilePath + "': " + e.getMessage(), e); |
| 192 | + } |
| 193 | + } |
| 194 | + |
175 | 195 | if (confProperties != null) { |
176 | 196 | for (String prop : confProperties) { |
177 | 197 | String[] parts = prop.split("=", 2); |
|
0 commit comments