Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion ast-visual-studio-extension/CxWrapper/CxConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public static class CxConstants
public static string LOG_RUNNING_SCAN_CANCEL_CMD => "Executing 'scan cancel' command using the CLI...";
public static string LOG_RUNNING_PROJECT_SHOW_CMD => "Retrieving details for project id: {0}...";
public static string LOG_RUNNING_TENANT_SETTINGS_CMD => "Getting tenant settings...";

public static string LOG_CLI_COMMAND_EXECUTING => "Executing CLI command: {0}";
public static string LOG_CLI_COMMAND_COMPLETED => "CLI command completed with result length: {0}";
public static string LOG_CLI_COMMAND_ERROR => "CLI command failed: {0}";
/** FILE EXTENSIONS **/
public static string EXTENSION_JSON => ".json";

Expand Down
49 changes: 45 additions & 4 deletions ast-visual-studio-extension/CxWrapper/CxWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,52 @@ public CxWrapper(CxConfig cxConfiguration, Type type)
{
cxConfiguration.Validate();
cxConfig = cxConfiguration;
logger = LogManager.GetLogger(type);

// Subscribe to CLI output events
Execution.OnProcessCompleted += LogCliOutput;
}

logger = LogManager.GetLogger(type);
private void LogCliOutput(List<string> cliOutput)
{
foreach (var line in cliOutput)
{
logger.Info(line);
}
}

private string ExecuteCliCommand(List<string> arguments, Func<string, string> resultHandler)
{
try
{
string commandLine = $"cx.exe {string.Join(" ", arguments)}";
logger.Info($"Executing CLI command: {commandLine}");
var result = Execution.ExecuteCommand(WithConfigArguments(arguments), resultHandler);
return result;
}
catch (Exception ex)
{
logger.Error($"CLI command failed: {ex.Message}", ex);
throw;
}
}

private string ExecuteCliCommand(List<string> arguments, string tempDir, string fileName)
{
try
{
string commandLine = $"cx.exe {string.Join(" ", arguments)}";
logger.Info(string.Format(CxConstants.LOG_CLI_COMMAND_EXECUTING, commandLine));
var result = Execution.ExecuteCommand(WithConfigArguments(arguments), tempDir, fileName);
logger.Info(string.Format(CxConstants.LOG_CLI_COMMAND_COMPLETED, result?.Length ?? 0));
return result;
}
catch (Exception ex)
{
logger.Error(string.Format(CxConstants.LOG_CLI_COMMAND_ERROR, ex.Message), ex);
throw;
}
}

/// <summary>
/// Scan file with ASCA
Expand Down Expand Up @@ -67,7 +108,7 @@ public CxAsca ScanAsca(string fileSource, bool ascaLatestVersion = false, string

AppendAgentToArguments(agent, arguments);

string result = Execution.ExecuteCommand(WithConfigArguments(arguments), Execution.CheckValidJSONString);
string result = ExecuteCliCommand(arguments, Execution.CheckValidJSONString);
return JsonConvert.DeserializeObject<CxAsca>(result);
}

Expand Down Expand Up @@ -109,7 +150,7 @@ public string AuthValidate()
CxConstants.CLI_VALIDATE_CMD
};

return Execution.ExecuteCommand(WithConfigArguments(authValidateArguments), line => line);
return ExecuteCliCommand(authValidateArguments, line => line);
}

/// <summary>
Expand Down Expand Up @@ -184,7 +225,7 @@ public string GetResults(string scanId, ReportFormat reportFormat)
break;
}

return Execution.ExecuteCommand(WithConfigArguments(resultsArguments), tempDir, fileName + extension);
return ExecuteCliCommand(resultsArguments, tempDir, fileName + extension);
}

/// <summary>
Expand Down
27 changes: 22 additions & 5 deletions ast-visual-studio-extension/CxWrapper/Execution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Json;
using System.Text;

namespace ast_visual_studio_extension.CxCLI
{
Expand Down Expand Up @@ -49,21 +50,32 @@ private static string InitProcess(List<string> arguments, Func<string, string> l
{
string outputData = string.Empty;
string errorData = string.Empty;
var cliOutput = new List<string>();

using (var process = new Process
{
StartInfo = GetProcessStartInfo(arguments)
})
{
process.ErrorDataReceived += (s, args) => errorData += string.IsNullOrEmpty(errorData) ? args.Data : Environment.NewLine + args.Data;
process.ErrorDataReceived += (s, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
errorData += string.IsNullOrEmpty(errorData) ? args.Data : Environment.NewLine + args.Data;
cliOutput.Add(args.Data);
}
};

process.OutputDataReceived += (s, args) =>
{
string parsedValue = lineParser.Invoke(args.Data);

if (!string.IsNullOrEmpty(parsedValue))
if (!string.IsNullOrEmpty(args.Data))
{
outputData += string.IsNullOrEmpty(outputData) ? parsedValue : Environment.NewLine + parsedValue;
string parsedValue = lineParser.Invoke(args.Data);
if (!string.IsNullOrEmpty(parsedValue))
{
outputData += string.IsNullOrEmpty(outputData) ? parsedValue : Environment.NewLine + parsedValue;
}
cliOutput.Add(args.Data);
}
};

Expand All @@ -72,6 +84,9 @@ private static string InitProcess(List<string> arguments, Func<string, string> l
process.BeginErrorReadLine();
process.WaitForExit();

// Raise event with collected output
OnProcessCompleted?.Invoke(cliOutput);

if (process.ExitCode != 0)
{
throw new CxException(process.ExitCode, string.IsNullOrEmpty(errorData) ? outputData.Trim() : errorData.Trim());
Expand All @@ -81,6 +96,8 @@ private static string InitProcess(List<string> arguments, Func<string, string> l
}
}

public static event Action<List<string>> OnProcessCompleted;

private static ProcessStartInfo GetProcessStartInfo(List<string> arguments)
{
return new ProcessStartInfo
Expand Down