-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathProgram.cs
86 lines (70 loc) · 2.75 KB
/
Program.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.CommandLine;
using Microsoft.Extensions.Logging;
namespace BinaryToolKit;
public class Program
{
public static readonly Argument<string> TargetDirectory = new("target-directory")
{
Description = "The directory to run the binary tooling on.",
Arity = ArgumentArity.ExactlyOne
};
public static readonly Option<string> OutputReportDirectory = new("--output-directory", "-o")
{
Description = "The directory to output the report to.",
Arity = ArgumentArity.ZeroOrOne,
DefaultValueFactory = _ => Path.Combine(Directory.GetCurrentDirectory(), "binary-report")
};
public static readonly Option<LogLevel> Level = new("--log-level", "-l")
{
Description = "The log level to run the tool in.",
Arity = ArgumentArity.ZeroOrOne,
DefaultValueFactory = _ => LogLevel.Information,
Recursive = true
};
public static readonly Option<string> AllowedBinariesFile = new("--allowed-binaries-file", "-ab")
{
Description = "The file containing the list of allowed binaries that are ignored for cleaning or validating.\n",
Arity = ArgumentArity.ZeroOrOne
};
public static int ExitCode = 0;
public static async Task<int> Main(string[] args)
{
var cleanCommand = CreateCommand("clean", "Clean the binaries in the target directory.");
var validateCommand = CreateCommand("validate", "Detect new binaries in the target directory.");
var rootCommand = new RootCommand("Tool for detecting, validating, and cleaning binaries in the target directory.")
{
Level,
cleanCommand,
validateCommand
};
SetCommandAction(cleanCommand, Modes.Clean);
SetCommandAction(validateCommand, Modes.Validate);
await rootCommand.Parse(args).InvokeAsync();
return ExitCode;
}
private static Command CreateCommand(string name, string description)
{
return new Command(name, description)
{
TargetDirectory,
OutputReportDirectory,
AllowedBinariesFile
};
}
private static void SetCommandAction(Command command, Modes mode)
{
command.SetAction(async (result, CancellationToken) =>
{
Log.Level = result.GetValue(Level);
var binaryTool = new BinaryTool();
ExitCode = await binaryTool.ExecuteAsync(
result.GetValue(TargetDirectory)!,
result.GetValue(OutputReportDirectory)!,
result.GetValue(AllowedBinariesFile),
mode);
});
}
}