-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add csv output format to validate command
You can now pass `--validation-format text|csv` to the validate command to get CSV or text output. Text continues to be the default when not specified.
- Loading branch information
Showing
4 changed files
with
143 additions
and
2 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
98 changes: 98 additions & 0 deletions
98
...y-cli/src/main/java/software/amazon/smithy/cli/commands/ValidationEventFormatOptions.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,98 @@ | ||
/* | ||
* 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.function.Consumer; | ||
import software.amazon.smithy.cli.ArgumentReceiver; | ||
import software.amazon.smithy.cli.CliError; | ||
import software.amazon.smithy.cli.CliPrinter; | ||
import software.amazon.smithy.cli.HelpPrinter; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.model.validation.ValidationEventFormatter; | ||
|
||
final class ValidationEventFormatOptions implements ArgumentReceiver { | ||
|
||
private static final String VALIDATION_FORMAT = "--validation-format"; | ||
|
||
enum ValidationFormat { | ||
TEXT { | ||
@Override | ||
void print(CliPrinter printer, ValidationEventFormatter formatter, ValidationEvent event) { | ||
printer.println(formatter.format(event)); | ||
} | ||
}, | ||
|
||
CSV { | ||
@Override | ||
void beginPrinting(CliPrinter printer) { | ||
printer.println("severity,id,shape,file,message,hint,suppressionReason"); | ||
} | ||
|
||
@Override | ||
void print(CliPrinter printer, ValidationEventFormatter formatter, ValidationEvent event) { | ||
printer.println( | ||
String.format("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"", | ||
event.getSeverity().toString(), | ||
formatCsv(event.getId()), | ||
event.getShapeId().map(ShapeId::toString).orElse(""), | ||
formatCsv( | ||
event.getSourceLocation().getFilename() | ||
+ ':' + event.getSourceLocation().getLine() | ||
+ ':' + event.getSourceLocation().getColumn()), | ||
formatCsv(event.getMessage()), | ||
formatCsv(event.getHint().orElse("")), | ||
formatCsv(event.getSuppressionReason().orElse("")))); | ||
} | ||
}; | ||
|
||
void beginPrinting(CliPrinter printer) {} | ||
|
||
abstract void print(CliPrinter printer, ValidationEventFormatter formatter, ValidationEvent event); | ||
|
||
void endPrinting(CliPrinter printer) {} | ||
|
||
private static String formatCsv(String value) { | ||
// Replace DQUOTE with DQUOTEDQUOTE and escape newlines. | ||
return value.replace("\"", "\"\"").replace("\n", "\\n"); | ||
} | ||
} | ||
|
||
private ValidationFormat format = ValidationFormat.TEXT; | ||
|
||
@Override | ||
public void registerHelp(HelpPrinter printer) { | ||
printer.param(VALIDATION_FORMAT, null, "text|csv", | ||
"Specifies the format to write validation events (text or csv). Defaults to text."); | ||
} | ||
|
||
@Override | ||
public Consumer<String> testParameter(String name) { | ||
if (name.equals("--validation-format")) { | ||
return s -> { | ||
switch (s) { | ||
case "csv": | ||
format(ValidationFormat.CSV); | ||
break; | ||
case "text": | ||
format(ValidationFormat.TEXT); | ||
break; | ||
default: | ||
throw new CliError("Unexpected " + VALIDATION_FORMAT + ": `" + s + "`"); | ||
} | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
void format(ValidationFormat format) { | ||
this.format = format; | ||
} | ||
|
||
ValidationFormat format() { | ||
return format; | ||
} | ||
} |
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