-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Feat: Add integrity check to cli #13848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
812fa4b
extend CheckIntegrity
palukku 82723a6
add check integrity to jabkit
palukku da299eb
replace writer
palukku ce3a15d
Merge branch 'main' into add-integrity-check-to-cli
palukku efebec9
fix imports
palukku c49f597
fix format
palukku 805cc42
add separator character handling
palukku 2d8c404
remove exception
palukku 2e5b4e2
adapt languages
palukku fd0d992
fix language tests
palukku 228838c
fix language tests 2
palukku 743765d
change field ranges in BibtexParser
palukku 244217c
re-add CygWinPathConverter
palukku 8fc9fd6
fix modernizer
palukku 483b3f0
fix modernizer2
palukku c0bf69b
move fieldranges to parser
palukku cf7924a
added changelog entry
palukku 4b9ac21
change HashMap to IdentityHashMap
palukku 764c5ba
remove line
palukku 58180f7
remove unused import
palukku 10231e0
add IntegrityCheckResultWriter
palukku f92d8f6
address comments
palukku 58aee67
add immutable Map#of
palukku fef7e2e
updated option handling and fix check
palukku eff8e17
remove test output
palukku 4bd013e
add comment on constructor handling the writer and close
palukku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| parent: Requirements | ||
| --- | ||
| # CLI | ||
|
|
||
| ## Unified `--input` option across all commands | ||
| `req~jabkit.cli.input-flag~1` | ||
|
|
||
| All `jabkit` commands that need a file input must have the `--input` option to specify the input file. | ||
| See [ADR 45](../decisions/0045-use-input-flag-always-for-input-files.md) for more details. | ||
|
|
||
| Needs: impl | ||
|
|
||
| <!-- markdownlint-disable-file MD022 --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,117 @@ | ||
| package org.jabref.cli; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.OutputStreamWriter; | ||
| import java.io.Writer; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.jabref.cli.converter.CygWinPathConverter; | ||
| import org.jabref.logic.importer.ParserResult; | ||
| import org.jabref.logic.integrity.IntegrityCheck; | ||
| import org.jabref.logic.integrity.IntegrityCheckResultCsvWriter; | ||
| import org.jabref.logic.integrity.IntegrityCheckResultErrorFormatWriter; | ||
| import org.jabref.logic.integrity.IntegrityCheckResultTxtWriter; | ||
| import org.jabref.logic.integrity.IntegrityCheckResultWriter; | ||
| import org.jabref.logic.integrity.IntegrityMessage; | ||
| import org.jabref.logic.journals.JournalAbbreviationLoader; | ||
| import org.jabref.logic.l10n.Localization; | ||
| import org.jabref.model.database.BibDatabaseContext; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import picocli.CommandLine; | ||
|
|
||
| import static picocli.CommandLine.Command; | ||
| import static picocli.CommandLine.Mixin; | ||
| import static picocli.CommandLine.Option; | ||
| import static picocli.CommandLine.Parameters; | ||
|
|
||
| @Command(name = "check-integrity", description = "Check integrity of the database.") | ||
| class CheckIntegrity implements Runnable { | ||
| class CheckIntegrity implements Callable<Integer> { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(CheckIntegrity.class); | ||
|
|
||
| @CommandLine.ParentCommand | ||
| private ArgumentProcessor argumentProcessor; | ||
|
|
||
| @Mixin | ||
| private ArgumentProcessor.SharedOptions sharedOptions = new ArgumentProcessor.SharedOptions(); | ||
|
|
||
| @Parameters(index = "0", converter = CygWinPathConverter.class, description = "BibTeX file to check", arity = "0..1") | ||
| // [impl->req~jabkit.cli.input-flag~1] | ||
| @Option(names = {"--input"}, converter = CygWinPathConverter.class, description = "Input BibTeX file", required = true) | ||
| private Path inputFile; | ||
|
|
||
| @Option(names = {"--input"}, converter = CygWinPathConverter.class, description = "Input BibTeX file") | ||
| private Path inputOption; | ||
| @Option(names = {"--output-format"}, description = "Output format: errorformat, txt or csv", defaultValue = "errorformat") | ||
| private String outputFormat; | ||
|
|
||
| @Option(names = {"--output-format"}, description = "Output format: txt or csv") | ||
| private String outputFormat = "txt"; // FixMe: Default value? | ||
| // in BibTeX it could be preferences.getEntryEditorPreferences().shouldAllowIntegerEditionBibtex() | ||
| @Option(names = {"--allow-integer-edition"}, description = "Allows Integer edition", negatable = true, defaultValue = "true", fallbackValue = "true") | ||
| private boolean allowIntegerEdition; | ||
|
|
||
| @Override | ||
| public void run() { | ||
| public Integer call() { | ||
| Optional<ParserResult> parserResult = ArgumentProcessor.importFile( | ||
| inputFile, | ||
| "bibtex", | ||
| argumentProcessor.cliPreferences, | ||
| sharedOptions.porcelain); | ||
| if (parserResult.isEmpty()) { | ||
| System.out.println(Localization.lang("Unable to open file '%0'.", inputFile)); | ||
| return 2; | ||
palukku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (parserResult.get().isInvalid()) { | ||
| System.out.println(Localization.lang("Input file '%0' is invalid and could not be parsed.", inputFile)); | ||
| return 2; | ||
palukku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (!sharedOptions.porcelain) { | ||
| System.out.println(Localization.lang("Checking integrity of '%0'.", inputFile)); | ||
| System.out.flush(); | ||
| } | ||
|
|
||
| // TODO: Implement integrity checking | ||
| BibDatabaseContext databaseContext = parserResult.get().getDatabaseContext(); | ||
|
|
||
| IntegrityCheck integrityCheck = new IntegrityCheck( | ||
| databaseContext, | ||
| argumentProcessor.cliPreferences.getFilePreferences(), | ||
| argumentProcessor.cliPreferences.getCitationKeyPatternPreferences(), | ||
| JournalAbbreviationLoader.loadRepository(argumentProcessor.cliPreferences.getJournalAbbreviationPreferences()), | ||
| allowIntegerEdition | ||
| ); | ||
|
|
||
| List<IntegrityMessage> messages = databaseContext.getEntries().stream() | ||
| .flatMap(entry -> integrityCheck.checkEntry(entry).stream()) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| messages.addAll(integrityCheck.checkDatabase(databaseContext.getDatabase())); | ||
|
|
||
| Writer writer = new OutputStreamWriter(System.out); | ||
palukku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| IntegrityCheckResultWriter checkResultWriter; | ||
| switch (outputFormat.toLowerCase(Locale.ROOT)) { | ||
| case "errorformat" -> | ||
| checkResultWriter = new IntegrityCheckResultErrorFormatWriter(writer, messages, parserResult.get(), inputFile); | ||
| case "txt" -> | ||
| checkResultWriter = new IntegrityCheckResultTxtWriter(writer, messages); | ||
| case "csv" -> | ||
| checkResultWriter = new IntegrityCheckResultCsvWriter(writer, messages); | ||
| default -> { | ||
| System.out.println(Localization.lang("Unknown output format '%0'.", outputFormat)); | ||
| return 3; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| checkResultWriter.writeFindings(); | ||
| writer.flush(); | ||
| } catch (IOException e) { | ||
| LOGGER.error("Error writing results", e); | ||
| return 2; | ||
| } | ||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.