-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add show/hide validation event filtering to CLI
The --show-validators and --hide-validators can now be used in the CLI anywhere that --severity was being used. This allows validation events to be hidden/show explicitly based on a comma separated list of heirarchical validation event IDs (i.e., dot based prefix event matching just like suppressions).
- Loading branch information
Showing
7 changed files
with
256 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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 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 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
76 changes: 0 additions & 76 deletions
76
smithy-cli/src/main/java/software/amazon/smithy/cli/commands/SeverityOption.java
This file was deleted.
Oops, something went wrong.
This file contains 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
177 changes: 177 additions & 0 deletions
177
smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidatorOptions.java
This file contains 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,177 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.cli.commands; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import software.amazon.smithy.cli.ArgumentReceiver; | ||
import software.amazon.smithy.cli.CliError; | ||
import software.amazon.smithy.cli.HelpPrinter; | ||
import software.amazon.smithy.cli.StandardOptions; | ||
import software.amazon.smithy.model.validation.Severity; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
|
||
/** | ||
* Add validation specific options. | ||
*/ | ||
final class ValidatorOptions implements ArgumentReceiver { | ||
|
||
static final String SEVERITY = "--severity"; | ||
static final String SHOW_VALIDATORS = "--show-validators"; | ||
static final String HIDE_VALIDATORS = "--hide-validators"; | ||
|
||
private Severity severity; | ||
private List<String> showValidators = Collections.emptyList(); | ||
private List<String> hideValidators = Collections.emptyList(); | ||
|
||
@Override | ||
public void registerHelp(HelpPrinter printer) { | ||
printer.param(SEVERITY, null, "SEVERITY", "Set the minimum reported validation severity (one of NOTE, " | ||
+ "WARNING [default setting], DANGER, ERROR)."); | ||
printer.param(SHOW_VALIDATORS, null, "VALIDATORS", "Comma-separated list of hierarchical validation event " | ||
+ "IDs to show in the output of the command, " | ||
+ "hiding the rest."); | ||
printer.param(HIDE_VALIDATORS, null, "VALIDATORS", "Comma-separated list of hierarchical validation event " | ||
+ "IDs to hide in the output of the command, " | ||
+ "showing the rest."); | ||
} | ||
|
||
@Override | ||
public Consumer<String> testParameter(String name) { | ||
switch (name) { | ||
case SEVERITY: | ||
return value -> { | ||
severity(Severity.fromString(value).orElseThrow(() -> { | ||
return new CliError("Invalid severity level: " + value); | ||
})); | ||
}; | ||
case SHOW_VALIDATORS: | ||
return value -> { | ||
if (!hideValidators.isEmpty()) { | ||
throw new CliError(SHOW_VALIDATORS + " and " + HIDE_VALIDATORS + " are mutually exclusive"); | ||
} | ||
showValidators(parseIds(value)); | ||
}; | ||
case HIDE_VALIDATORS: | ||
return value -> { | ||
if (!showValidators.isEmpty()) { | ||
throw new CliError(SHOW_VALIDATORS + " and " + HIDE_VALIDATORS + " are mutually exclusive"); | ||
} | ||
hideValidators(parseIds(value)); | ||
}; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
private List<String> parseIds(String value) { | ||
List<String> result = new ArrayList<>(); | ||
for (String id : value.split("\\s*,\\s*")) { | ||
id = id.trim(); | ||
if (id.isEmpty()) { | ||
throw new CliError("Invalid validation event ID"); | ||
} | ||
result.add(id); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Set the severity. | ||
* | ||
* @param severity Severity to set. | ||
*/ | ||
void severity(Severity severity) { | ||
this.severity = severity; | ||
} | ||
|
||
/** | ||
* Get the severity level, taking into account standard options that affect the default. | ||
* | ||
* @param options Standard options to query if no severity is explicitly set. | ||
* @return Returns the resolved severity option. | ||
*/ | ||
Severity severity(StandardOptions options) { | ||
if (severity != null) { | ||
return severity; | ||
} else if (options.quiet()) { | ||
return Severity.DANGER; | ||
} else { | ||
return Severity.WARNING; | ||
} | ||
} | ||
|
||
/** | ||
* Set the list of validators to show, but hide everything else. | ||
* | ||
* @param validators Validators to show. | ||
*/ | ||
void showValidators(List<String> validators) { | ||
this.showValidators = validators; | ||
} | ||
|
||
/** | ||
* Get the list of validators to show. | ||
* | ||
* @return Validator event IDs. | ||
*/ | ||
List<String> showValidators() { | ||
return showValidators; | ||
} | ||
|
||
/** | ||
* Get the list of validators to hide. | ||
* | ||
* @return Validator event IDs. | ||
*/ | ||
List<String> hideValidators(String csv) { | ||
return hideValidators; | ||
} | ||
|
||
/** | ||
* Set the list of validators to hide. | ||
* | ||
* @param validators Validators to hide. | ||
*/ | ||
void hideValidators(List<String> validators) { | ||
this.hideValidators = validators; | ||
} | ||
|
||
/** | ||
* Check if the given validation event matches the show/hide settings. | ||
* | ||
* <p>A severity must be set before calling this method. | ||
* | ||
* @param event Event to check. | ||
* @return Return true if the event can be seen. | ||
*/ | ||
boolean isVisible(ValidationEvent event) { | ||
if (event.getSeverity().ordinal() < severity.ordinal()) { | ||
return false; | ||
} | ||
|
||
if (!showValidators.isEmpty()) { | ||
for (String show : showValidators) { | ||
if (event.containsId(show)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
if (!hideValidators.isEmpty()) { | ||
for (String hide : hideValidators) { | ||
if (event.containsId(hide)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.