-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validate-format verb with placeholder for future validation (#577)
* Wire up a validate-format verb that runs the format validation logic for a specified file. This is a hidden verb that I imagine being used during development, and in future by DRI to explore an SBOM or troubleshoot a validation failure. * Fix PR comment - removing duplicated parameter validation code.
- Loading branch information
1 parent
29bc8fb
commit 1d832e0
Showing
10 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/Microsoft.Sbom.Api/Config/Args/FormatValidationArgs.cs
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,23 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Sbom.Api.Utils; | ||
using Microsoft.Sbom.Extensions.Entities; | ||
using PowerArgs; | ||
|
||
namespace Microsoft.Sbom.Api.Config.Args; | ||
|
||
/// <summary> | ||
/// The command line arguments provided for the validate action in ManifestTool. | ||
/// </summary> | ||
public class FormatValidationArgs : CommonArgs | ||
{ | ||
/// <summary> | ||
/// Gets or sets the file path of the SBOM to validate. | ||
/// </summary> | ||
[ArgShortcut("sp")] | ||
[ArgDescription("The file path of the SBOM to validate.")] | ||
public string? SbomPath { get; set; } | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Sbom.Api; | ||
using Microsoft.Sbom.Api.Output.Telemetry; | ||
using Microsoft.Sbom.Common.Config; | ||
|
||
namespace Microsoft.Sbom.Tool; | ||
|
||
public class FormatValidationService : IHostedService | ||
{ | ||
private readonly IConfiguration config; | ||
private readonly IRecorder recorder; | ||
private readonly IHostApplicationLifetime hostApplicationLifetime; | ||
|
||
public FormatValidationService(IConfiguration config, | ||
IRecorder recorder, | ||
IHostApplicationLifetime hostApplicationLifetime) | ||
{ | ||
this.config = config; | ||
this.recorder = recorder; | ||
this.hostApplicationLifetime = hostApplicationLifetime; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
Console.WriteLine($"Format validation service called for {config.SbomPath.Value}"); | ||
|
||
await recorder.FinalizeAndLogTelemetryAsync(); | ||
Environment.ExitCode = true ? (int)ExitCode.Success : (int)ExitCode.ValidationError; | ||
} | ||
catch (Exception e) | ||
{ | ||
var message = e.InnerException != null ? e.InnerException.Message : e.Message; | ||
Console.WriteLine($"Encountered error while running format validation. Error: {message}"); | ||
Environment.ExitCode = (int)ExitCode.GeneralError; | ||
} | ||
|
||
hostApplicationLifetime.StopApplication(); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
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,52 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Sbom.Api; | ||
using Microsoft.Sbom.Api.Output.Telemetry; | ||
using Microsoft.Sbom.Common.Config; | ||
|
||
namespace Microsoft.Sbom.Tool; | ||
|
||
public class FormatValidationService : IHostedService | ||
{ | ||
private readonly IConfiguration config; | ||
private readonly IRecorder recorder; | ||
private readonly IHostApplicationLifetime hostApplicationLifetime; | ||
|
||
public FormatValidationService(IConfiguration config, | ||
IRecorder recorder, | ||
IHostApplicationLifetime hostApplicationLifetime) | ||
{ | ||
this.config = config; | ||
this.recorder = recorder; | ||
this.hostApplicationLifetime = hostApplicationLifetime; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
Console.WriteLine($"Format validation service called for {config.SbomPath.Value}"); | ||
|
||
await recorder.FinalizeAndLogTelemetryAsync(); | ||
Environment.ExitCode = true ? (int)ExitCode.Success : (int)ExitCode.ValidationError; | ||
} | ||
catch (Exception e) | ||
{ | ||
var message = e.InnerException != null ? e.InnerException.Message : e.Message; | ||
Console.WriteLine($"Encountered error while running format validation. Error: {message}"); | ||
Environment.ExitCode = (int)ExitCode.GeneralError; | ||
} | ||
|
||
hostApplicationLifetime.StopApplication(); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
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