Skip to content

Commit

Permalink
Use FailableOptional in ConfigRunConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
georgetayqy committed Mar 4, 2024
1 parent fdc2148 commit b9aecd4
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/main/java/reposense/model/ConfigRunConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -13,6 +16,7 @@
import reposense.parser.exceptions.InvalidCsvException;
import reposense.parser.exceptions.InvalidHeaderException;
import reposense.system.LogsManager;
import reposense.util.function.FailableOptional;

/**
* Represents RepoSense run configured by config files.
Expand All @@ -38,33 +42,25 @@ public ConfigRunConfiguration(CliArguments cliArguments) {
public List<RepoConfiguration> getRepoConfigurations()
throws IOException, InvalidCsvException, InvalidHeaderException {
List<RepoConfiguration> repoConfigs = new RepoConfigCsvParser(cliArguments.getRepoConfigFilePath()).parse();
List<AuthorConfiguration> authorConfigs;
List<GroupConfiguration> groupConfigs;

Path authorConfigFilePath = cliArguments.getAuthorConfigFilePath();
Path groupConfigFilePath = cliArguments.getGroupConfigFilePath();
// parse the author config file path
FailableOptional.of(cliArguments.getAuthorConfigFilePath())
.filter(Files::exists)
.map(x -> new AuthorConfigCsvParser(cliArguments.getAuthorConfigFilePath()).parse())
.ifPresent(x -> RepoConfiguration.merge(repoConfigs, x))
.ifPresent(() -> RepoConfiguration.setHasAuthorConfigFileToRepoConfigs(repoConfigs, true))
.ifFailOfType(Arrays.asList(IOException.class, InvalidCsvException.class))
.ifFail(x -> logger.log(Level.WARNING, x.getMessage(), x))
.orElse(Collections.emptyList());


if (authorConfigFilePath != null && Files.exists(authorConfigFilePath)) {
try {
authorConfigs = new AuthorConfigCsvParser(cliArguments.getAuthorConfigFilePath()).parse();
RepoConfiguration.merge(repoConfigs, authorConfigs);
RepoConfiguration.setHasAuthorConfigFileToRepoConfigs(repoConfigs, true);
} catch (IOException | InvalidCsvException e) {
// for all IO and invalid csv exceptions, log the error and continue
logger.log(Level.WARNING, e.getMessage(), e);
}
}

if (groupConfigFilePath != null && Files.exists(groupConfigFilePath)) {
try {
groupConfigs = new GroupConfigCsvParser(cliArguments.getGroupConfigFilePath()).parse();
RepoConfiguration.setGroupConfigsToRepos(repoConfigs, groupConfigs);
} catch (IOException | InvalidCsvException e) {
// for all IO and invalid csv exceptions, log the error and continue
logger.log(Level.WARNING, e.getMessage(), e);
}
}
// parse the group config file path
FailableOptional.of(cliArguments.getGroupConfigFilePath())
.filter(Files::exists)
.map(x -> new GroupConfigCsvParser(x).parse())
.ifPresent(x -> RepoConfiguration.setGroupConfigsToRepos(repoConfigs, x))
.ifFailOfType(Arrays.asList(IOException.class, InvalidCsvException.class))
.ifFail(x -> logger.log(Level.WARNING, x.getMessage(), x))
.orElse(Collections.emptyList());

return repoConfigs;
}
Expand Down

0 comments on commit b9aecd4

Please sign in to comment.