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

Commit

Permalink
Several changes:
Browse files Browse the repository at this point in the history
- Theoretically can now execute command with parameters
- Fix naming scheme of many elements
- Format with clang-format
  • Loading branch information
StrangeRanger committed Feb 16, 2024
1 parent 5fbe208 commit 57a7253
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 77 deletions.
8 changes: 4 additions & 4 deletions FAFB-PowerShell-Tool.Tests/CommandParametersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void PossibleParameters_LoadCommandParametersAsyncNotPopulated_ThrowInval
{
// Arrange
CommandParameters commandParameters = new();

// Assert
Assert.Throws<InvalidOperationException>(() => commandParameters.PossibleParameters);
}
Expand All @@ -21,11 +21,11 @@ public async Task LoadCommandParametersAsync_PopulatesPossibleParameters_IsNotEm
// Arrange
CommandParameters commandParameters = new();
Command command = new("Get-Process");

// Act
await commandParameters.LoadCommandParametersAsync(command);

// Assert
Assert.NotEmpty(commandParameters.PossibleParameters);
}
}
}
2 changes: 1 addition & 1 deletion FAFB-PowerShell-Tool.Tests/ReturnValuesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public void NoErrors_StdOutHasEntries_ReturnsFalse()
Assert.Empty(returnValues.StdErr);
Assert.NotEmpty(returnValues.StdOut);
}
}
}
31 changes: 15 additions & 16 deletions FAFB-PowerShell-Tool/ComboBoxParameterViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@ namespace FAFB_PowerShell_Tool;
public sealed class ComboBoxParameterViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

private ObservableCollection<string> _possibleParameterList = new();
private string _selectedParameter = string.Empty;
private string _selectedParameterValue = string.Empty;

/// <summary>
/// Empty constructor to initialize a new ComboBoxParameterViewModel.
/// </summary>
public ComboBoxParameterViewModel()
{ }

/// <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)
{
PossibleParameterList = possibleParameterList ?? throw new ArgumentNullException(nameof(possibleParameterList));
}

/// <summary>
/// Sets a unique selected value for each combo box
Expand Down Expand Up @@ -65,21 +79,6 @@ public ObservableCollection<string> PossibleParameterList
}
}

/// <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)
{
PossibleParameterList = possibleParameterList ?? throw new ArgumentNullException(nameof(possibleParameterList));
}

/// <summary>
/// Empty constructor Intializes a new ComboxParameterViewModel
/// </summary>
public ComboBoxParameterViewModel()
{ }

/// <summary>
/// Invokes the PropertyChanged event for the specified property name.
/// </summary>
Expand Down
86 changes: 49 additions & 37 deletions FAFB-PowerShell-Tool/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ public sealed class MainWindowViewModel : INotifyPropertyChanged
private readonly PowerShellExecutor _powerShellExecutor;
private Command _selectedCommand;
private string _powerShellOutput;
private string _QueryDescription;
private string _QueryName;
private CustomQueries.query CurrentQuery;
private ObservableCollection<Button> _buttons;
private CustomQueries cq = new CustomQueries();
private CustomQueries.query _currentQuery;
private CustomQueries _customQuery = new();

// ----------------- Properties ----------------- //

Expand All @@ -34,26 +32,14 @@ public sealed class MainWindowViewModel : INotifyPropertyChanged
public ObservableCollection<Command> ActiveDirectoryCommandList { get; private set; }

/// <summary>
/// todo
/// The name of the query.
/// </summary>
public string QueryDescription
{
get => _QueryDescription;
set {
_QueryDescription = value;
}
}
public string QueryName { get; set; }

/// <summary>
/// todo
/// The description of the query.
/// </summary>
public string QueryName
{
get => _QueryName;
set {
_QueryName = value;
}
}
public string QueryDescription { get; set; }

/// <summary>
/// This property creates a collection of buttons to be added to the stack panel for custom queries
Expand All @@ -62,7 +48,10 @@ public ObservableCollection<Button> ButtonStackPanel
{
get {
if (_buttons == null)
{
_buttons = new ObservableCollection<Button>();
}

return _buttons;
}
}
Expand All @@ -74,6 +63,7 @@ public ObservableCollection<Button> ButtonStackPanel

/// <summary>
/// This is the command for the edit option on custom queries
/// TODO: Rename this property to match Property naming conventions!!!
/// </summary>
public ICommand _EditCustomQuery { get; }

Expand All @@ -85,12 +75,12 @@ public ObservableCollection<Button> ButtonStackPanel
/// <summary>
/// Collection of ComboBoxParameterViewModels to dynamically handle multiple parameter selections.
/// </summary>
public ObservableCollection<ComboBoxParameterViewModel> DynamicParameterCollection { get; set; }
public ObservableCollection<ComboBoxParameterViewModel> DynamicParameterCollection { get; }

/// <summary>
/// Collection of ComboBoxParameterViewModels to dynamically handle multiple parameter value selections.
/// </summary>
public ObservableCollection<ComboBoxParameterViewModel> DynamicParameterValuesCollection { get; set; }
public ObservableCollection<ComboBoxParameterViewModel> DynamicParameterValuesCollection { get; }

/// <summary>
/// Command to execute the selected PowerShell command.
Expand All @@ -104,11 +94,13 @@ public ObservableCollection<Button> ButtonStackPanel

/// <summary>
/// Command to add a new parameter ComboBox to the UI.
/// TODO: Rename this property to match Property naming conventions!!!
/// </summary>
public ICommand Remove_ParameterComboBox { get; }

/// <summary>
/// Command to have the output send to a text file when executing
/// TODO: Get-only auto-property 'OutputToText' is never assigned!!!
/// </summary>
public ICommand OutputToText { get; }

Expand All @@ -119,8 +111,9 @@ public ObservableCollection<Button> ButtonStackPanel

/// <summary>
/// Command to output to a csv when executing
/// TODO: Get-only auto-property 'AddButton' is never assigned!!!
/// </summary>
public ICommand _AddButton { get; }
public ICommand AddButton { get; }

/// <summary>
/// Gets or sets the currently selected PowerShell command.
Expand Down Expand Up @@ -163,7 +156,7 @@ public MainWindowViewModel()
SavedQueries = new RelayCommand(PerformSavedQueries);

Check warning on line 156 in FAFB-PowerShell-Tool/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Nullability of reference types in type of parameter 'commandParameter' of 'void MainWindowViewModel.PerformSavedQueries(object commandParameter)' doesn't match the target delegate 'Action<object?>' (possibly because of nullability attributes).
_EditCustomQuery = new RelayCommand(EditCustomQuery);

Check warning on line 157 in FAFB-PowerShell-Tool/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build (x64)

Nullability of reference types in type of parameter 'sender' of 'void MainWindowViewModel.EditCustomQuery(object sender)' doesn't match the target delegate 'Action<object?>' (possibly because of nullability attributes).

CurrentQuery = new CustomQueries.query();
_currentQuery = new CustomQueries.query();

DynamicParameterCollection = new ObservableCollection<ComboBoxParameterViewModel>();
DynamicParameterValuesCollection = new ObservableCollection<ComboBoxParameterViewModel>();
Expand All @@ -172,9 +165,9 @@ public MainWindowViewModel()
LoadCustomQueries(); // Calls method to deserialize and load buttons.

// Debug
for (int i = 0; i < ButtonStackPanel.Count; i++)
foreach (Button t in ButtonStackPanel)
{
CustomQueries.query test = (CustomQueries.query)ButtonStackPanel[i].Tag;
CustomQueries.query test = (CustomQueries.query)t.Tag;
Trace.WriteLine(test.commandName);
}
}
Expand All @@ -183,8 +176,9 @@ public MainWindowViewModel()

/// <summary>
/// This method will edit the Query and fill out the field with the desired query and you can edit the query
/// TODO: Add a description to the parameter of this method.
/// </summary>
/// <param name="_"></param>
/// <param name="sender"></param>
private void EditCustomQuery(Object sender)
{
// Get the button that we are editing
Expand All @@ -207,11 +201,11 @@ private void LoadCustomQueries()
{
try
{
cq.LoadData();
_customQuery.LoadData();

// parameter counter
int i = 0;
foreach (CustomQueries.query cQuery in cq.Queries)
foreach (CustomQueries.query cQuery in _customQuery.Queries)
{
// Creates a new button for each query
Button newButton = new() { Height = 48 };
Expand Down Expand Up @@ -304,6 +298,16 @@ private async Task ExecuteSelectedCommand()
{
try
{
SelectedCommand.Parameters.Clear();

// Add selected parameters and their values to the command.
for (int i = 0; i < DynamicParameterCollection.Count; i++)
{
string parameterName = DynamicParameterCollection[i].SelectedParameter;
string parameterValue = DynamicParameterValuesCollection[i].SelectedParameterValue;
SelectedCommand.Parameters.Add(new CommandParameter(parameterName, parameterValue));
}

ReturnValues result = await _powerShellExecutor.ExecuteAsync(SelectedCommand);

if (result.HadErrors)
Expand All @@ -322,7 +326,9 @@ private async Task ExecuteSelectedCommand()

/// <summary>
/// Adds a new parameter and value selection ComboBox to the DynamicParameterCollection.
/// TODO: Add a description to the parameter of this method.
/// </summary>
/// <param name="_"></param>
private void AddParameterComboBox(object _)
{
DynamicParameterCollection.Add(new ComboBoxParameterViewModel(PossibleParameterList));
Expand All @@ -332,14 +338,18 @@ private void AddParameterComboBox(object _)
/// <summary>
/// This will be to Execute a query to a text file
/// TODO: Add a description to the parameter of this method.
/// TODO: Rename this method to match method naming conventions!!!
/// </summary>
/// <param name="_"></param>
private void _OutputToText(object _)
{ }
{
throw new NotImplementedException();
}

/// <summary>
/// This will be to Execute a query to csv
/// TODO: Add a description to the parameter of this method.
/// TODO: Rename this method to match method naming conventions!!!
/// This method is in the working
/// </summary>
/// <param name="_"></param>
Expand Down Expand Up @@ -399,9 +409,9 @@ private void _OutputToCsv(object _)
/// <summary>
/// Writing to a csv file without powershell
/// </summary>
private void csvTemp()
private void CsvTemp()
{
// Need to execute the command per normal but get the return values
throw new NotImplementedException();
}

/// <summary>
Expand Down Expand Up @@ -436,15 +446,15 @@ public void GetCurrentQuery()

try
{
CurrentQuery.queryDescription = QueryDescription;
CurrentQuery.queryName = QueryName;
_currentQuery.queryDescription = QueryDescription;
_currentQuery.queryName = QueryName;

CurrentQuery.commandName = SelectedCommand.CommandText;
_currentQuery.commandName = SelectedCommand.CommandText;

int i = 0;
foreach (CommandParameter CP in SelectedCommand.Parameters)
{
CurrentQuery.commandParameters[i] = (CP.Name);
_currentQuery.commandParameters[i] = (CP.Name);
// CurrentQuery.commandParametersValues[i] = ((string) CP.Value); The values are not quite working yet
i++;
}
Expand All @@ -457,6 +467,7 @@ public void GetCurrentQuery()

/// <summary>
/// Removes the parameter box after adding them
/// TODO: Add a description to the parameter of this method.
/// </summary>
/// <param name="_"></param>
private void RemoveParameterComboBox(object _)
Expand All @@ -469,6 +480,7 @@ private void RemoveParameterComboBox(object _)

/// <summary>
/// This method will serialize the command and add it to the list of buttons.
/// TODO: Add a description to the parameter of this method.
/// </summary>
/// <param name="commandParameter"></param>
private void PerformSavedQueries(object commandParameter)
Expand All @@ -483,7 +495,7 @@ private void PerformSavedQueries(object commandParameter)
SelectedCommand.Parameters.Add(new CommandParameter(comboBoxData.SelectedParameter));
}

cq.SerializeCommand(SelectedCommand, QueryName, QueryDescription);
_customQuery.SerializeCommand(SelectedCommand, QueryName, QueryDescription);

Button newButton = new() { Content = SelectedCommand.CommandText, Height = 48 };
ButtonStackPanel.Add(newButton);
Expand Down
1 change: 1 addition & 0 deletions FAFB-PowerShell-Tool/PowerShell/PowerShellExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public ReturnValues Execute(string commandString)
return ExecuteInternal(commandString);
}

// TODO: Do something with this method...
public ReturnValues ExecutePSCommand(PSCommand executeCommand)
{
_powerShell.Commands = executeCommand;
Expand Down
Loading

0 comments on commit 57a7253

Please sign in to comment.