Skip to content

Commit

Permalink
Default to current version, undo reordering of commands, and consolid…
Browse files Browse the repository at this point in the history
…ate writes while transforming
  • Loading branch information
jameswinkler committed Jan 26, 2021
1 parent d537617 commit 405c18d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Sarif.Driver/Sdk/CommonOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class CommonOptionsBase
HelpText =
"The SARIF version of the output log file. Valid values are OneZeroZero and Current",
Default = SarifVersion.Current)]
public SarifVersion SarifOutputVersion { get; set; }
public SarifVersion SarifOutputVersion { get; set; } = SarifVersion.Current;

[Option(
"threads",
Expand Down
25 changes: 9 additions & 16 deletions src/Sarif.Multitool.Library/RewriteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,11 @@ public int Run(RewriteOptions options)

string inputVersion = SniffVersion(options.InputFilePath);
if (!inputVersion.Equals(SarifUtilities.StableSarifVersion))
{
// Transform file and write to output path. Then continue rewriting as before.
// TODO: Consolidate this so only a single write is necessary.
TransformFileToVersionTwo(options, inputVersion, actualOutputPath);
// We wrote to the output path, so read from it again.
actualLog = ReadSarifFile<SarifLog>(_fileSystem, actualOutputPath);
{
actualLog = TransformFileToVersionTwo(options.InputFilePath, inputVersion);
}
else
{
// Read from the original input path.
actualLog = ReadSarifFile<SarifLog>(_fileSystem, options.InputFilePath);
}

Expand Down Expand Up @@ -123,25 +118,23 @@ private string SniffVersion(string sarifPath)
return null;
}

private void TransformFileToVersionTwo(SingleFileOptionsBase options, string inputVersion, string outputFilePath)
private SarifLog TransformFileToVersionTwo(string inputFilePath, string inputVersion)
{
if (inputVersion == "1.0.0")
{
// Converting version 1 to version 2
SarifLogVersionOne actualLog = ReadSarifFile<SarifLogVersionOne>(_fileSystem, options.InputFilePath, SarifContractResolverVersionOne.Instance);
SarifLogVersionOne actualLog = ReadSarifFile<SarifLogVersionOne>(_fileSystem, inputFilePath, SarifContractResolverVersionOne.Instance);
var visitor = new SarifVersionOneToCurrentVisitor();
visitor.VisitSarifLogVersionOne(actualLog);
WriteSarifFile(_fileSystem, visitor.SarifLog, outputFilePath, options.Minify);
return visitor.SarifLog;
}
else
{
// Converting prerelease version 2 to version 2
PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(
_fileSystem.FileReadAllText(options.InputFilePath),
formatting: options.Formatting,
out string sarifText);

_fileSystem.FileWriteAllText(outputFilePath, sarifText);
return PrereleaseCompatibilityTransformer.UpdateToCurrentVersion(
_fileSystem.FileReadAllText(inputFilePath),
formatting: Formatting.None,
out string _);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif.Multitool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public static int Main(string[] args)
(ExportValidationConfigurationOptions options) => new ExportValidationConfigurationCommand().Run(options),
(ExportValidationRulesMetadataOptions options) => new ExportValidationRulesMetadataCommand().Run(options),
(FileWorkItemsOptions fileWorkItemsOptions) => new FileWorkItemsCommand().Run(fileWorkItemsOptions),
(ResultMatchingOptions baselineOptions) => new ResultMatchingCommand().Run(baselineOptions),
(MergeOptions mergeOptions) => new MergeCommand().Run(mergeOptions),
(PageOptions pageOptions) => new PageCommand().Run(pageOptions),
(QueryOptions queryOptions) => new QueryCommand().Run(queryOptions),
(RebaseUriOptions rebaseOptions) => new RebaseUriCommand().Run(rebaseOptions),
(ResultMatchingOptions baselineOptions) => new ResultMatchingCommand().Run(baselineOptions),
(RewriteOptions rewriteOptions) => new RewriteCommand().Run(rewriteOptions),
(ValidateOptions validateOptions) => new ValidateCommand().Run(validateOptions),
_ => HandleParseError(args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ private SarifLog ExecuteTest(string path)
{
InputFilePath = path,
OutputFilePath = path,
Force = true,
SarifOutputVersion = SarifVersion.Current
Force = true
};

// Verify command returned success
Expand Down

0 comments on commit 405c18d

Please sign in to comment.