-
Notifications
You must be signed in to change notification settings - Fork 49
/
RunCommand.cs
61 lines (51 loc) · 2.06 KB
/
RunCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using PSRule.CommandLine.Models;
using PSRule.Configuration;
using PSRule.Pipeline;
using PSRule.Pipeline.Dependencies;
namespace PSRule.CommandLine.Commands;
/// <summary>
/// Execute features of the <c>run</c> command through the CLI.
/// </summary>
public sealed class RunCommand
{
private const string PUBLISHER = "CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US";
/// <summary>
/// A generic error.
/// </summary>
private const int ERROR_GENERIC = 1;
/// <summary>
/// One or more failures occurred.
/// </summary>
private const int ERROR_BREAK_ON_FAILURE = 100;
/// <summary>
/// Call <c>run</c>.
/// </summary>
public static int Run(RunOptions operationOptions, ClientContext clientContext)
{
var exitCode = 0;
var file = LockFile.Read(null);
var inputPath = operationOptions.InputPath == null || operationOptions.InputPath.Length == 0 ?
[Environment.GetWorkingPath()] : operationOptions.InputPath;
if (operationOptions.Path != null)
clientContext.Option.Include.Path = operationOptions.Path;
if (operationOptions.Outcome != null && operationOptions.Outcome.Value != Rules.RuleOutcome.None)
clientContext.Option.Output.Outcome = operationOptions.Outcome;
// Build command
var builder = CommandLineBuilder.Assert(operationOptions.Module ?? [], clientContext.Option, clientContext.Host, file);
builder.Baseline(BaselineOption.FromString(operationOptions.Baseline));
builder.InputPath(inputPath);
builder.UnblockPublisher(PUBLISHER);
using var pipeline = builder.Build();
if (pipeline != null)
{
pipeline.Begin();
pipeline.Process(null);
pipeline.End();
if (pipeline.Result.HadFailures)
exitCode = ERROR_BREAK_ON_FAILURE;
}
return clientContext.Host.HadErrors || pipeline == null ? ERROR_GENERIC : exitCode;
}
}