Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CLI Projects to Single Entrypoint #476

Merged
merged 23 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,7 @@ MigrationBackup/
.ionide/

# Rider
src/.idea
src/.idea

# Ignore Mac Folder Metadata
**/.DS_Store
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning">
<Version>3.5.119</Version>
<Version>3.5.146</Version>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
Expand Down
88 changes: 5 additions & 83 deletions src/OSSGadget.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,30 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32014.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-download", "oss-download\oss-download.csproj", "{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-defog", "oss-defog\oss-defog.csproj", "{7F44BC11-2DE4-4EC7-8E07-41BD5A3D2734}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-health", "oss-health\oss-health.csproj", "{F05946C7-35E2-49F3-9CD8-DF65FD1E068F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-characteristic", "oss-characteristics\oss-characteristic.csproj", "{5031D8F3-6D61-4939-BE91-A85C716D55E5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-find-source", "oss-find-source\oss-find-source.csproj", "{C88C1268-872C-4E84-99C9-861B3B13CEA6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{057F3869-D776-4404-BB77-444ADF4CFDD6}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-detect-backdoor", "oss-detect-backdoor\oss-detect-backdoor.csproj", "{4F3F5D54-9C66-4366-969B-35ACF18DC03F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-detect-cryptography", "oss-detect-cryptography\oss-detect-cryptography.csproj", "{CD18B51D-21B0-4E1F-B0A2-F41E80D3BBDD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-risk-calculator", "oss-risk-calculator\oss-risk-calculator.csproj", "{84189BD7-D6F4-4E84-83A8-4FA1B811CDDD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-tests", "oss-tests\oss-tests.csproj", "{812DF4A5-057B-4227-A3E8-4DFF67DBE07A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-metadata", "oss-metadata\oss-metadata.csproj", "{7E9A489A-B502-4622-A34C-EF507E18DFEB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-find-squats", "oss-find-squats\oss-find-squats.csproj", "{23D17E08-9E95-4448-B75D-BABAF1F3C854}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-diff", "oss-diff\oss-diff.csproj", "{17C9F321-8A49-4140-B009-AD476F959D42}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-find-domain-squats", "oss-find-domain-squats\oss-find-domain-squats.csproj", "{E8884AC4-638F-4513-A5BD-4650E19CE071}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-reproducible", "oss-reproducible\oss-reproducible.csproj", "{B79FCA22-5A3B-47F5-8FB1-692E3175162D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oss-find-squats-lib", "oss-find-squats-lib\oss-find-squats-lib.csproj", "{182CA72B-6BB1-400E-83FC-B35558928BAB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.Lib", "Shared\Shared.Lib.csproj", "{0023A885-5255-46A0-BAF7-F419FFA00AE4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.CLI", "Shared.CLI\Shared.CLI.csproj", "{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "oss-fresh", "oss-fresh\oss-fresh.csproj", "{5E0E66AB-D141-42A1-B53F-BEE63FBD62FB}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "oss-gadget-cli", "oss-gadget-cli\oss-gadget-cli.csproj", "{9B2B9345-186C-44B7-90C3-0D7A892D464F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEBC05E9-A5A9-4FFE-9023-1248F2DA58D9}.Release|Any CPU.Build.0 = Release|Any CPU
{7F44BC11-2DE4-4EC7-8E07-41BD5A3D2734}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F44BC11-2DE4-4EC7-8E07-41BD5A3D2734}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F44BC11-2DE4-4EC7-8E07-41BD5A3D2734}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F44BC11-2DE4-4EC7-8E07-41BD5A3D2734}.Release|Any CPU.Build.0 = Release|Any CPU
{F05946C7-35E2-49F3-9CD8-DF65FD1E068F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F05946C7-35E2-49F3-9CD8-DF65FD1E068F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F05946C7-35E2-49F3-9CD8-DF65FD1E068F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F05946C7-35E2-49F3-9CD8-DF65FD1E068F}.Release|Any CPU.Build.0 = Release|Any CPU
{5031D8F3-6D61-4939-BE91-A85C716D55E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5031D8F3-6D61-4939-BE91-A85C716D55E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5031D8F3-6D61-4939-BE91-A85C716D55E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5031D8F3-6D61-4939-BE91-A85C716D55E5}.Release|Any CPU.Build.0 = Release|Any CPU
{C88C1268-872C-4E84-99C9-861B3B13CEA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C88C1268-872C-4E84-99C9-861B3B13CEA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C88C1268-872C-4E84-99C9-861B3B13CEA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C88C1268-872C-4E84-99C9-861B3B13CEA6}.Release|Any CPU.Build.0 = Release|Any CPU
{4F3F5D54-9C66-4366-969B-35ACF18DC03F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F3F5D54-9C66-4366-969B-35ACF18DC03F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F3F5D54-9C66-4366-969B-35ACF18DC03F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F3F5D54-9C66-4366-969B-35ACF18DC03F}.Release|Any CPU.Build.0 = Release|Any CPU
{CD18B51D-21B0-4E1F-B0A2-F41E80D3BBDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD18B51D-21B0-4E1F-B0A2-F41E80D3BBDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD18B51D-21B0-4E1F-B0A2-F41E80D3BBDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD18B51D-21B0-4E1F-B0A2-F41E80D3BBDD}.Release|Any CPU.Build.0 = Release|Any CPU
{84189BD7-D6F4-4E84-83A8-4FA1B811CDDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84189BD7-D6F4-4E84-83A8-4FA1B811CDDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84189BD7-D6F4-4E84-83A8-4FA1B811CDDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84189BD7-D6F4-4E84-83A8-4FA1B811CDDD}.Release|Any CPU.Build.0 = Release|Any CPU
{812DF4A5-057B-4227-A3E8-4DFF67DBE07A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{812DF4A5-057B-4227-A3E8-4DFF67DBE07A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{812DF4A5-057B-4227-A3E8-4DFF67DBE07A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E9A489A-B502-4622-A34C-EF507E18DFEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E9A489A-B502-4622-A34C-EF507E18DFEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E9A489A-B502-4622-A34C-EF507E18DFEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E9A489A-B502-4622-A34C-EF507E18DFEB}.Release|Any CPU.Build.0 = Release|Any CPU
{23D17E08-9E95-4448-B75D-BABAF1F3C854}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23D17E08-9E95-4448-B75D-BABAF1F3C854}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23D17E08-9E95-4448-B75D-BABAF1F3C854}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23D17E08-9E95-4448-B75D-BABAF1F3C854}.Release|Any CPU.Build.0 = Release|Any CPU
{17C9F321-8A49-4140-B009-AD476F959D42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17C9F321-8A49-4140-B009-AD476F959D42}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17C9F321-8A49-4140-B009-AD476F959D42}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17C9F321-8A49-4140-B009-AD476F959D42}.Release|Any CPU.Build.0 = Release|Any CPU
{E8884AC4-638F-4513-A5BD-4650E19CE071}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8884AC4-638F-4513-A5BD-4650E19CE071}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8884AC4-638F-4513-A5BD-4650E19CE071}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8884AC4-638F-4513-A5BD-4650E19CE071}.Release|Any CPU.Build.0 = Release|Any CPU
{B79FCA22-5A3B-47F5-8FB1-692E3175162D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B79FCA22-5A3B-47F5-8FB1-692E3175162D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B79FCA22-5A3B-47F5-8FB1-692E3175162D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B79FCA22-5A3B-47F5-8FB1-692E3175162D}.Release|Any CPU.Build.0 = Release|Any CPU
{182CA72B-6BB1-400E-83FC-B35558928BAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{182CA72B-6BB1-400E-83FC-B35558928BAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{182CA72B-6BB1-400E-83FC-B35558928BAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -117,10 +39,10 @@ Global
{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66CE54D2-40AA-41CB-A487-3FE44E38BFE0}.Release|Any CPU.Build.0 = Release|Any CPU
{5E0E66AB-D141-42A1-B53F-BEE63FBD62FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E0E66AB-D141-42A1-B53F-BEE63FBD62FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E0E66AB-D141-42A1-B53F-BEE63FBD62FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E0E66AB-D141-42A1-B53F-BEE63FBD62FB}.Release|Any CPU.Build.0 = Release|Any CPU
{9B2B9345-186C-44B7-90C3-0D7A892D464F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B2B9345-186C-44B7-90C3-0D7A892D464F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B2B9345-186C-44B7-90C3-0D7A892D464F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B2B9345-186C-44B7-90C3-0D7A892D464F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
132 changes: 132 additions & 0 deletions src/Shared.CLI/BaseTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.

namespace Microsoft.CST.OpenSource
{
using CommandLine;
using CommandLine.Text;
using Helpers;
using Microsoft.CST.OpenSource.Shared;
using OssGadget.Options;
using PackageManagers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static Microsoft.CST.OpenSource.Shared.OutputBuilderFactory;

public abstract class BaseTool<T> : OssGadgetLib where T: BaseToolOptions
{
public OutputFormat currentOutputFormat = OutputFormat.text;

public static string ToolName { get => CliHelpers.GetToolName() ?? ""; }
public static string ToolVersion { get => CliHelpers.GetToolVersion() ?? ""; }

public BaseTool(ProjectManagerFactory projectManagerFactory) : base(projectManagerFactory)
{
}

public BaseTool() : base()
{}

public abstract Task<ErrorCode> RunAsync(T opt);

/// <summary>
/// Formulates the help text for each derived tool
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="result"></param>
/// <param name="errs"></param>
protected void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs)
{
HelpText helpText = HelpText.AutoBuild(result, h =>
{
h.AddDashesToOption = true;
h.AutoVersion = true;
h.AdditionalNewLineAfterOption = false;
h.MaximumDisplayWidth = Console.WindowWidth;
h.AddPostOptionsLines(CliHelpers.GetCommonSupportedHelpTextLines());
return HelpText.DefaultParsingErrorsHandler(result, h);
});
Console.Error.Write(helpText);
}

/// <summary>
/// Use the CommandlineParser library to get the cmd line arguments
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="args"></param>
/// <returns>The Action object with the parsed options</returns>
protected ParserResult<T> ParseOptions<T>(string[]? args)
{
Parser parser = new();
ParserResult<T> parserResult = parser.ParseArguments<T>(args);
parserResult.WithNotParsed(errs => DisplayHelp(parserResult, errs));
return parserResult;
}

/// <summary>
/// Restores the output stream to Console, if it was changed to something else.
/// </summary>
protected void RestoreOutput()
{
if (redirectConsole)
{
ConsoleHelper.RestoreConsole();
}
}

/// <summary>
/// Use the OutputBuilder to select the given format and return a output builder. The format
/// should be compatible with one of the enum entries in OutputFormat text format will be
/// chosen, if the format is invalid.
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
protected IOutputBuilder SelectFormat(string format)
{
try
{
currentOutputFormat = GetOutputFormat(format);
return CreateOutputBuilder(currentOutputFormat);
}
catch (ArgumentOutOfRangeException)
{
Logger.Debug("Invalid output format, selecting text");
}

currentOutputFormat = OutputFormat.text;
return CreateDefaultOutputBuilder();
}

/// <summary>
/// Change the tool output from the existing one to the passed in file. If the outputFile is
/// not a valid filename, the output will be switched to Console.
/// </summary>
/// <param name="outputFile"></param>
protected void SelectOutput(string outputFile)
{
// If outputFile is valid, then redirect output there.
if (string.IsNullOrWhiteSpace(outputFile) ||
outputFile.IndexOfAny(Path.GetInvalidPathChars()) < 0 ||
Path.GetFileName(outputFile).IndexOfAny(Path.GetInvalidFileNameChars()) < 0)
{
redirectConsole = true;
if (!ConsoleHelper.RedirectConsole(outputFile))
{
Logger.Debug("Could not switch output from console to file");
// continue with current output
}
}
else
{
redirectConsole = false;
Logger.Debug("Invalid file, {0}, writing to console instead.", outputFile);
}
}

private bool redirectConsole = false;
}
}
11 changes: 11 additions & 0 deletions src/Shared.CLI/ErrorCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.

namespace Microsoft.CST.OpenSource;

public enum ErrorCode
{
Ok,
ProcessingException,
NoTargets,
ErrorParsingOptions
}
Loading