Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Gui Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
J0seph0 committed Mar 9, 2024
2 parents bc28674 + 2777f52 commit c1133c5
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion ActiveDirectoryQuerier.Tests/AppConsoleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task ClearConsole_ClearsConsole_SuccessfullyCleared()
var (appConsole, _) = await ExecuteCommandAsync(command);

// Act
appConsole.ClearConsole();
appConsole.Clear();

// Assert
Assert.Empty(appConsole.ConsoleOutput);
Expand Down
3 changes: 2 additions & 1 deletion ActiveDirectoryQuerier/AppConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed class AppConsole : INotifyPropertyChanged
/// <important>
/// Do not set this property directly. Use the Append method instead. It's public for data binding purposes.
/// </important>
/// TODO: Find a way to make the set of this property private.
public string ConsoleOutput
{
get => _consoleOutput;
Expand All @@ -33,7 +34,7 @@ public string ConsoleOutput
/// <summary>
/// Clears the console output.
/// </summary>
public void ClearConsole()
public void Clear()
{
ConsoleOutput = string.Empty;
}
Expand Down
18 changes: 9 additions & 9 deletions ActiveDirectoryQuerier/ComboBoxParameterViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ namespace ActiveDirectoryQuerier;
public sealed class ComboBoxParameterViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
private ObservableCollection<string> _possibleParameterList = new();
private ObservableCollection<string> _possibleParameters = new();
private string _selectedParameter = string.Empty;

/// <summary>
/// Initializes a new instance of the ComboBoxParameterViewModel class.
/// </summary>
/// <param name="possibleParameterList">Initial list of possible parameters for the ComboBox.</param>
public ComboBoxParameterViewModel(ObservableCollection<string> possibleParameterList)
/// <param name="possibleParameters">Initial list of possible parameters for the ComboBox.</param>
public ComboBoxParameterViewModel(ObservableCollection<string> possibleParameters)
{
PossibleParameterList = possibleParameterList ?? throw new ArgumentNullException(nameof(possibleParameterList));
PossibleParameters = possibleParameters ?? throw new ArgumentNullException(nameof(possibleParameters));
}

/// <summary>
Expand All @@ -40,19 +40,19 @@ public string SelectedParameter
/// <summary>
/// Gets or sets the list of possible parameters that can be selected for a command.
/// </summary>
public ObservableCollection<string> PossibleParameterList
public ObservableCollection<string> PossibleParameters
{
get => _possibleParameterList;
get => _possibleParameters;
set {
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

if (_possibleParameterList != value)
if (_possibleParameters != value)
{
_possibleParameterList = value;
OnPropertyChanged(nameof(PossibleParameterList));
_possibleParameters = value;
OnPropertyChanged(nameof(PossibleParameters));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions ActiveDirectoryQuerier/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private void ClearConsoleOutput(object _)
// If the user selects yes, clear the console
if (result == MessageBoxResult.Yes)
{
PowerShellOutput.ClearConsole();
PowerShellOutput.Clear();
}
}

Expand Down Expand Up @@ -460,7 +460,7 @@ private async Task LoadCommandParametersAsync(Command? selectedCommand)
// Update the possible properties of the ComboBoxParameterViewModels.
foreach (ComboBoxParameterViewModel comboBoxParameterViewModel in DynamicParametersCollection)
{
comboBoxParameterViewModel.PossibleParameterList = PossibleCommandParametersList;
comboBoxParameterViewModel.PossibleParameters = PossibleCommandParametersList;
}
}

Expand Down
20 changes: 10 additions & 10 deletions ActiveDirectoryQuerier/PowerShell/ActiveDirectoryCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ public static class ActiveDirectoryCommands
/// </exception>
public static async Task<ObservableCollection<Command>> GetActiveDirectoryCommands()
{
Command command = new("Get-Command");
command.Parameters.Add("Module", "ActiveDirectory");
Command powerShellCommand = new("Get-Command");
powerShellCommand.Parameters.Add("Module", "ActiveDirectory");
PowerShellExecutor powerShellExecutor = new();
ObservableCollection<Command> commandList = new();
ReturnValues commandListTemp = await powerShellExecutor.ExecuteAsync(command);
ObservableCollection<Command> activeDirectoryCommands = new();
ReturnValues powerShellOutput = await powerShellExecutor.ExecuteAsync(powerShellCommand);

// NOTE: This is more of an internal error...
// TODO: Provide a more detailed error message, possibly to the user and to a log file.
if (commandListTemp.HadErrors)
if (powerShellOutput.HadErrors)
{
MessageBox.Show(string.Join(" ", commandListTemp.StdErr),
MessageBox.Show(string.Join(" ", powerShellOutput.StdErr),
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
throw new InvalidPowerShellStateException(
$"An error occurred while retrieving the ActiveDirectory commands: " +
$"{string.Join(" ", commandListTemp.StdErr)}");
$"{string.Join(" ", powerShellOutput.StdErr)}");
}

foreach (string cmd in commandListTemp.StdOut)
foreach (string cmd in powerShellOutput.StdOut)
{
commandList.Add(new Command(cmd));
activeDirectoryCommands.Add(new Command(cmd));
}

return commandList;
return activeDirectoryCommands;
}
}
28 changes: 15 additions & 13 deletions ActiveDirectoryQuerier/PowerShell/CommandParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CommandParameters : ICommandParameters
/// <note>
/// I realize that this isn't a very clean way of doing things, but it's the best I could come up with at the time.
/// </note>
/// TODO: Refactor this to be more clean and less error-prone.
public ObservableCollection<string> PossibleParameters
{
get {
Expand All @@ -35,34 +36,35 @@ public ObservableCollection<string> PossibleParameters
/// <summary>
/// Retrieves the parameters available for a given command asynchronously.
/// </summary>
/// <param name="commandObject">The command object to retrieve parameters for.</param>
public async Task LoadCommandParametersAsync(Command? commandObject)
/// <param name="powerShellCommand">The command object to retrieve parameters for.</param>
public async Task LoadCommandParametersAsync(Command? powerShellCommand)
{
await LoadCommandParametersInternal(commandObject, true);
await LoadCommandParametersInternal(powerShellCommand, true);
}

/// <summary>
/// Retrieves the parameters available for a given command synchronously.
/// </summary>
/// <param name="commandObject">The command object to retrieve parameters for.</param>
void ICommandParameters.LoadCommandParameters(Command? commandObject)
/// <param name="powerShellCommand">The command to retrieve parameters for.</param>
void ICommandParameters.LoadCommandParameters(Command? powerShellCommand)
{
LoadCommandParametersInternal(commandObject, false).Wait();
LoadCommandParametersInternal(powerShellCommand, false).Wait();
}

/// <summary>
/// Retrieves the parameters available for a given command.
/// </summary>
/// <param name="commandObject">The command object to retrieve parameters for.</param>
/// <param name="powerShellCommand">The command to retrieve parameters for.</param>
/// <param name="isAsync">Determines whether the operation should be asynchronous or not.</param>
private async Task LoadCommandParametersInternal(Command? commandObject, bool isAsync)
private async Task LoadCommandParametersInternal(Command? powerShellCommand, bool isAsync)
{
// commandObject can be null if the user attempts to select an ActiveDirectory command that doesn't exist.
// powerShellCommand can be null if the user attempts to select an ActiveDirectory command that doesn't exist.
// More specifically, if the entered command doesn't exist in the ActiveDirectoryCommandsList in
// MainWindowViewModel.cs, commandObject will be null, causing an exception to be thrown, crashing the program.
if (commandObject is null)
// MainWindowViewModel.cs, powerShellCommand will be null, causing an exception to be thrown, crashing the
// program.
if (powerShellCommand is null)
{
Trace.WriteLine("Error: commandObject is null");
Trace.WriteLine("Error: command is null");
_possibleParameters.Add("No valid command provided");
return;
}
Expand All @@ -71,7 +73,7 @@ private async Task LoadCommandParametersInternal(Command? commandObject, bool is
{
using var powerShell = System.Management.Automation.PowerShell.Create();
string commandString =
$"Get-Command {commandObject.CommandText} | Select -ExpandProperty Parameters | ForEach-Object {{ $_.Keys }}";
$"Get-Command {powerShellCommand.CommandText} | Select -ExpandProperty Parameters | ForEach-Object {{ $_.Keys }}";

powerShell.Commands.Clear();
powerShell.AddScript(commandString);
Expand Down
2 changes: 1 addition & 1 deletion ActiveDirectoryQuerier/PowerShell/CustomQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public void SerializeCommand(Command? cmnd, string queryName, string queryDescri
}
/// <summary>
/// This method Loads the string from the saved file "CustomQueries.dat" then gives it to the Queries List
/// TODO: Fix any and all warnings about possible null values.
/// </summary>
/// TODO: Fix any and all warnings about possible null values.
public void LoadData()
{
try
Expand Down
2 changes: 1 addition & 1 deletion ActiveDirectoryQuerier/PowerShell/ICommandParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace ActiveDirectoryQuerier.PowerShell;

public interface ICommandParameters
{
void LoadCommandParameters(Command? commandObject);
void LoadCommandParameters(Command? powerShellCommand);
}
31 changes: 14 additions & 17 deletions ActiveDirectoryQuerier/PowerShell/PowerShellExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ public PowerShellExecutor()
/// <param name="command">The command to be prepared for execution.</param>
private void PrepareCommand(Command? command)
{
// TODO: Might change to just return if command is null...
if (command is null)
{
throw new ArgumentNullException(nameof(command));
}
// TODO: Add a test to identify if throwing an exception is necessary, or a return value is better.
ArgumentNullException.ThrowIfNull(command);

_powerShell.Commands.AddCommand(command.CommandText);
foreach (var parameter in command.Parameters)
Expand Down Expand Up @@ -91,18 +88,18 @@ public async Task<ReturnValues> ExecuteAsync(Command? command)
/// <summary>
/// Handles exceptions that occur during the execution of a PowerShell command.
/// </summary>
/// <param name="ex">The exception that occurred during the execution of the PowerShell command.</param>
/// <param name="exception">The exception that occurred during the execution of the PowerShell command.</param>
/// <returns>A ReturnValues object containing the exception details.</returns>
private ReturnValues ExecutionExceptionHandler(Exception ex)
private ReturnValues ExecutionExceptionHandler(Exception exception)
{
StringBuilder sb = new();
StringBuilder errorMessage = new();

sb.AppendLine("An error occurred while executing the PowerShell command:");
sb.AppendLine(ex.Message);
sb.AppendLine(ex.StackTrace);
Debug.WriteLine(sb.ToString());
errorMessage.AppendLine("An error occurred while executing the PowerShell command:");
errorMessage.AppendLine(exception.Message);
errorMessage.AppendLine(exception.StackTrace);
Debug.WriteLine(errorMessage.ToString());

return new ReturnValues { StdErr = { sb.ToString() } };
return new ReturnValues { StdErr = { errorMessage.ToString() } };
}

/// <summary>
Expand All @@ -112,24 +109,24 @@ private ReturnValues ExecutionExceptionHandler(Exception ex)
/// <returns>A ReturnValues object containing the processed results.</returns>
private ReturnValues ProcessPowerShellResults(IEnumerable<PSObject> results)
{
ReturnValues returnValues = new();
ReturnValues powerShellOutput = new();

if (_powerShell.HadErrors)
{
foreach (var error in _powerShell.Streams.Error)
{
returnValues.StdErr.Add($"Error: {error}");
powerShellOutput.StdErr.Add($"Error: {error}");
}
}
else
{
foreach (var result in results)
{
returnValues.StdOut.Add(result.ToString());
powerShellOutput.StdOut.Add(result.ToString());
}
}

return returnValues;
return powerShellOutput;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions ActiveDirectoryQuerier/RelayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace ActiveDirectoryQuerier;

/// <summary>
/// A command whose sole purpose is to relay its functionality to other objects by invoking delegates.
/// TODO: Remove any unused methods, after some investigation.
/// </summary>
/// <note>
/// I'm unsure if I'm dealing with the nullability warnings correctly, in this situation...
/// </note>
/// TODO: Remove any unused methods, after some investigation.
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
Expand All @@ -19,7 +19,6 @@ public class RelayCommand : ICommand
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
/// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
public RelayCommand(Action<object> execute, Predicate<object?>? canExecute = null)
{
_execute = execute;
Expand All @@ -44,6 +43,7 @@ public bool CanExecute(object? parameter)
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
/// </param>
/// TODO: Perform null checking before calling the _execute property.
public void Execute(object? parameter)
{
// For now, we are ignoring the warning about the parameter being null.
Expand Down

0 comments on commit c1133c5

Please sign in to comment.