Skip to content

Commit

Permalink
refactor: migrate to Spectre.Console.Cli (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Perez <[email protected]>
  • Loading branch information
JamieMagee and melotic committed Sep 15, 2023
1 parent 69888f0 commit 498c786
Show file tree
Hide file tree
Showing 56 changed files with 1,306 additions and 1,049 deletions.
6 changes: 5 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
<PackageVersion Include="Polly" Version="7.2.4" />
<PackageVersion Include="SemanticVersioning" Version="2.0.2" />
<PackageVersion Include="Serilog" Version="3.0.1" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="7.0.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageVersion Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageVersion Include="Serilog.Sinks.Map" Version="1.0.2" />
<PackageVersion Include="Spectre.Console" Version="0.47.1-preview.0.18" />
<PackageVersion Include="Spectre.Console.Cli" Version="0.47.0" />
<PackageVersion Include="Spectre.Console.Cli.Extensions.DependencyInjection" Version="0.1.0" />
<PackageVersion Include="Spectre.Console.Testing" Version="0.47.0" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.507" />
<PackageVersion Include="System.Memory" Version="4.5.5" />
<PackageVersion Include="System.Reactive" Version="6.0.0" />
Expand Down
25 changes: 0 additions & 25 deletions src/Microsoft.ComponentDetection.Contracts/VerbosityMode.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ namespace Microsoft.ComponentDetection.Orchestrator;
using System.Collections.Generic;
using System.Linq;
using CommandLine;
using Microsoft.ComponentDetection.Orchestrator.ArgumentSets;
using Microsoft.ComponentDetection.Orchestrator.Commands;

public class ArgumentHelper : IArgumentHelper
{
private readonly IEnumerable<IScanArguments> argumentSets;
private readonly IEnumerable<ScanSettings> argumentSets;

public ArgumentHelper(IEnumerable<IScanArguments> argumentSets) => this.argumentSets = argumentSets;
public ArgumentHelper(IEnumerable<ScanSettings> argumentSets) => this.argumentSets = argumentSets;

public static IDictionary<string, string> GetDetectorArgs(IEnumerable<string> detectorArgsList)
{
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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();
}
}
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");
}
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;
}
}
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
{
}
Loading

0 comments on commit 498c786

Please sign in to comment.