-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate to
Spectre.Console.Cli
(#518)
Co-authored-by: Justin Perez <[email protected]>
- Loading branch information
1 parent
69888f0
commit 498c786
Showing
56 changed files
with
1,306 additions
and
1,049 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
25 changes: 0 additions & 25 deletions
25
src/Microsoft.ComponentDetection.Contracts/VerbosityMode.cs
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
26 changes: 0 additions & 26 deletions
26
src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BaseArguments.cs
This file was deleted.
Oops, something went wrong.
49 changes: 0 additions & 49 deletions
49
src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeArguments.cs
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/BcdeDevArguments.cs
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/IDetectionArguments.cs
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/IListDetectionArgs.cs
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/IScanArguments.cs
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
src/Microsoft.ComponentDetection.Orchestrator/ArgumentSets/ListDetectionArgs.cs
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
src/Microsoft.ComponentDetection.Orchestrator/CommandLineArgumentsExporter.cs
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/Microsoft.ComponentDetection.Orchestrator/Commands/BaseSettings.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,49 @@ | ||
namespace Microsoft.ComponentDetection.Orchestrator.Commands; | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using Serilog.Events; | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
/// <summary> | ||
/// Base settings for all commands. | ||
/// </summary> | ||
public abstract class BaseSettings : CommandSettings | ||
{ | ||
[Description("Wait for debugger on start")] | ||
[CommandOption("--Debug")] | ||
public bool Debug { get; init; } | ||
|
||
[Description("Used to output all telemetry events to the console.")] | ||
[CommandOption("--DebugTelemetry")] | ||
public bool DebugTelemetry { get; set; } | ||
|
||
[Description("Identifier used to correlate all telemetry for a given execution. If not provided, a new GUID will be generated.")] | ||
[CommandOption("--CorrelationId")] | ||
public Guid CorrelationId { get; set; } | ||
|
||
[Description("Flag indicating what level of logging to output to console during execution. Options are: Verbose, Debug, Information, Warning, Error, or Fatal.")] | ||
[DefaultValue(LogEventLevel.Information)] | ||
[CommandOption("--LogLevel")] | ||
public LogEventLevel LogLevel { get; set; } | ||
|
||
[Description("An integer representing the time limit (in seconds) before detection is cancelled")] | ||
[CommandOption("--Timeout")] | ||
public int? Timeout { get; set; } | ||
|
||
[Description("Output path for log files. Defaults to %TMP%")] | ||
[CommandOption("--Output")] | ||
public string Output { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public override ValidationResult Validate() | ||
{ | ||
if (this.Timeout is <= 0) | ||
{ | ||
return ValidationResult.Error($"{nameof(this.Timeout)} must be a positive integer"); | ||
} | ||
|
||
return base.Validate(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/Microsoft.ComponentDetection.Orchestrator/Commands/Interceptor.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,53 @@ | ||
namespace Microsoft.ComponentDetection.Orchestrator.Commands; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Microsoft.ComponentDetection.Common.Telemetry; | ||
using Serilog.Core; | ||
using Spectre.Console.Cli; | ||
|
||
/// <summary> | ||
/// Intercepts all commands before they are executed. | ||
/// </summary> | ||
public class Interceptor : ICommandInterceptor | ||
{ | ||
/// <summary> | ||
/// The minimum logging level to use. This will dynamically change based on the | ||
/// <see cref="BaseSettings.LogLevel"/> setting. | ||
/// </summary> | ||
public static readonly LoggingLevelSwitch LogLevel = new(); | ||
|
||
private readonly ITypeResolver typeResolver; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Interceptor"/> class. | ||
/// </summary> | ||
/// <param name="typeResolver">The type resolver.</param> | ||
public Interceptor(ITypeResolver typeResolver) => this.typeResolver = typeResolver; | ||
|
||
/// <inheritdoc /> | ||
public void Intercept(CommandContext context, CommandSettings settings) | ||
{ | ||
if (settings is BaseSettings baseSettings) | ||
{ | ||
LogLevel.MinimumLevel = baseSettings.LogLevel; | ||
LoggingEnricher.Path = GetLogFilePath(baseSettings.Output); | ||
|
||
// This is required so TelemetryRelay can be accessed via it's static singleton | ||
// It should be refactored out at a later date | ||
TelemetryRelay.Instance.Init(this.typeResolver.Resolve(typeof(IEnumerable<ITelemetryService>)) as IEnumerable<ITelemetryService>); | ||
TelemetryRelay.Instance.SetTelemetryMode(baseSettings.DebugTelemetry ? TelemetryMode.Debug : TelemetryMode.Production); | ||
} | ||
|
||
if (settings is ScanSettings scanSettings) | ||
{ | ||
LoggingEnricher.PrintStderr = scanSettings.PrintManifest; | ||
} | ||
} | ||
|
||
private static string GetLogFilePath(string output = null) => | ||
Path.Combine( | ||
string.IsNullOrEmpty(output) ? Path.GetTempPath() : output, | ||
$"GovCompDisc_Log_{DateTime.Now:yyyyMMddHHmmssfff}_{Environment.ProcessId}.log"); | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Microsoft.ComponentDetection.Orchestrator/Commands/ListDetectorsCommand.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,44 @@ | ||
namespace Microsoft.ComponentDetection.Orchestrator.Commands; | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.ComponentDetection.Contracts; | ||
using Spectre.Console; | ||
using Spectre.Console.Cli; | ||
|
||
/// <summary> | ||
/// Lists available detectors. | ||
/// </summary> | ||
public sealed class ListDetectorsCommand : Command<ListDetectorsSettings> | ||
{ | ||
private readonly IEnumerable<IComponentDetector> detectors; | ||
private readonly IAnsiConsole console; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ListDetectorsCommand"/> class. | ||
/// </summary> | ||
/// <param name="detectors">The detectors.</param> | ||
/// <param name="console">The console.</param> | ||
public ListDetectorsCommand( | ||
IEnumerable<IComponentDetector> detectors, | ||
IAnsiConsole console) | ||
{ | ||
this.detectors = detectors; | ||
this.console = console; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override int Execute(CommandContext context, ListDetectorsSettings settings) | ||
{ | ||
var table = new Table(); | ||
table.AddColumn("Name"); | ||
|
||
foreach (var detector in this.detectors) | ||
{ | ||
table.AddRow(detector.Id); | ||
} | ||
|
||
this.console.Write(table); | ||
|
||
return (int)ProcessingResultCode.Success; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Microsoft.ComponentDetection.Orchestrator/Commands/ListDetectorsSettings.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,8 @@ | ||
namespace Microsoft.ComponentDetection.Orchestrator.Commands; | ||
|
||
/// <summary> | ||
/// Settings for the ListDetectors command. | ||
/// </summary> | ||
public class ListDetectorsSettings : BaseSettings | ||
{ | ||
} |
Oops, something went wrong.