Skip to content

Commit 7e66197

Browse files
author
Tony Brar
committed
Improve loading config from profiles
1 parent dcf0933 commit 7e66197

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

core/src/main/java/org/teamtators/rotator/config/ConfigLoader.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ public ConfigLoader(@Named("configDir") String configDir) {
2626
this.configDir = configDir;
2727
}
2828

29-
public JsonNode load(String fileName) {
30-
String filePath = configDir + File.separator + fileName;
31-
try (InputStream fileStream = new FileInputStream(filePath)) {
32-
logger.debug("Loading config from path {}", filePath);
33-
return objectMapper.reader().readTree(fileStream);
34-
} catch (IOException e) {
35-
throw new ConfigException(String.format("Error loading config %s", fileName), e);
36-
}
37-
}
38-
3929
/**
4030
* Get a config, modified for the specified profile
4131
*
@@ -44,26 +34,43 @@ public JsonNode load(String fileName) {
4434
* @return Combined config for profile
4535
*/
4636
public JsonNode getProfileConfig(String configName, String profileName) {
47-
// TODO: check if that profile even exists
4837
JsonNode mainConfig = load(configName);
49-
File f = new File(configDir + File.separator + profileName + File.separator + configName);
38+
if (profileName.equals("empty")) {
39+
return mainConfig;
40+
}
41+
File file = new File(configDir + File.separator + profileName + File.separator + configName);
42+
File folder = new File(configDir + File.separator + profileName);
5043
// check if we should just use default config
51-
if (profileName.equals("empty") || !f.exists()) {
44+
if (!file.exists()) {
45+
logger.info("No profile config file exists for {}, using base config", configName);
46+
return mainConfig;
47+
} else if (!folder.exists()) {
48+
logger.warn("No such profile as {} found, base config will be used.", profileName);
5249
return mainConfig;
5350
} else {
5451
JsonNode profileConfig = load(profileName + File.separator + configName);
5552
return mergeNodes(mainConfig, profileConfig);
5653
}
5754
}
5855

56+
private JsonNode load(String fileName) {
57+
String filePath = configDir + File.separator + fileName;
58+
try (InputStream fileStream = new FileInputStream(filePath)) {
59+
logger.debug("Loading config from path {}", filePath);
60+
return objectMapper.reader().readTree(fileStream);
61+
} catch (IOException e) {
62+
throw new ConfigException(String.format("Error loading config %s", fileName), e);
63+
}
64+
}
65+
5966
/**
6067
* Combine two JsonNodes
6168
*
6269
* @param mainNode Node to merge into
6370
* @param updateNode Node to merge with
6471
* @return Combined node
6572
*/
66-
public static JsonNode mergeNodes(JsonNode mainNode, JsonNode updateNode) {
73+
private JsonNode mergeNodes(JsonNode mainNode, JsonNode updateNode) {
6774
Iterator<String> fieldNames = updateNode.fieldNames();
6875
while (fieldNames.hasNext()) {
6976
String fieldName = fieldNames.next();

desktop/src/main/java/org/teamtators/rotator/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void start() {
4545

4646
commandStore.setRobot(robot);
4747

48-
String profileName = configLoader.load("profile.yml").textValue();
48+
String profileName = configLoader.getProfileConfig("profile.yml", "empty").textValue();
4949
logger.debug("Loading configs with profile {}");
5050
ObjectNode commandsConfig = (ObjectNode) configLoader.getProfileConfig("commands.yml", profileName);
5151
ObjectNode simulationConfig = (ObjectNode) configLoader.getProfileConfig("subsystems.yml", profileName);

rio/src/main/java/org/teamtators/rotator/Robot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private void initialize() {
6565
commandStore.setRobot(robot);
6666

6767
logger.debug("Created injector. Loading configs");
68-
String profileName = configLoader.load("profile.yml").textValue();
68+
String profileName = configLoader.getProfileConfig("profile.yml", "empty").textValue();
6969
logger.debug("Currently active config profile: {}", profileName);
7070
ObjectNode commandsConfig = (ObjectNode) configLoader.getProfileConfig("commands.yml", profileName);
7171
ObjectNode subsystemsConfig = (ObjectNode) configLoader.getProfileConfig("subsystems.yml", profileName);

0 commit comments

Comments
 (0)