Add IsExternalInit type, BaseCmdlet class, and SessionStatus enum
All checks were successful
CI/CD Pipeline / Build and Test (push) Successful in 1m3s
CI/CD Pipeline / Package Module (push) Has been skipped
CI/CD Pipeline / Create Release (push) Has been skipped
CI/CD Pipeline / Build and Test (pull_request) Successful in 1m7s
CI/CD Pipeline / Package Module (pull_request) Has been skipped
CI/CD Pipeline / Create Release (pull_request) Has been skipped

Introduce the IsExternalInit type for C# 9 records compatibility in .NET Standard 2.1. Implement BaseCmdlet class to provide common functionality for cmdlets, including methods for writing results and handling async operations. Add SessionStatus enum to represent API session states.
This commit is contained in:
2026-01-19 17:25:41 +03:00
parent f56784f2aa
commit bc86ad2965
3 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
namespace PS.IPAM.Cmdlets;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using PS.IPAM.Helpers;
/// <summary>
/// Base class for all PS.IPAM cmdlets providing common functionality.
/// </summary>
public abstract class BaseCmdlet : PSCmdlet
{
/// <summary>
/// Writes the result to the pipeline, handling both single objects and collections.
/// </summary>
/// <param name="result">The result to write to the pipeline.</param>
protected void WriteResult(object? result)
{
if (result == null)
{
return;
}
if (result is IEnumerable enumerable && result is not string)
{
foreach (var item in enumerable)
{
WriteObject(item);
}
}
else
{
WriteObject(result);
}
}
/// <summary>
/// Converts custom fields from various formats to a dictionary.
/// </summary>
/// <param name="customFields">The custom fields object to convert.</param>
/// <returns>A dictionary of custom field names and values.</returns>
protected static Dictionary<string, object> ConvertCustomFields(object? customFields)
{
if (customFields == null)
{
return new Dictionary<string, object>();
}
if (customFields is PSObject psobj)
{
var dict = new Dictionary<string, object>();
foreach (var prop in psobj.Properties)
{
dict[prop.Name] = prop.Value ?? string.Empty;
}
return dict;
}
if (customFields is Dictionary<string, object> dictObj)
{
return dictObj;
}
if (customFields is IDictionary genericDict)
{
var result = new Dictionary<string, object>();
foreach (DictionaryEntry entry in genericDict)
{
result[entry.Key?.ToString() ?? string.Empty] = entry.Value ?? string.Empty;
}
return result;
}
return new Dictionary<string, object>();
}
/// <summary>
/// Executes an async operation synchronously and handles errors.
/// </summary>
/// <typeparam name="T">The return type of the operation.</typeparam>
/// <param name="operation">The async operation to execute.</param>
/// <param name="errorId">The error ID for error reporting.</param>
/// <returns>The result of the operation, or default if an error occurred.</returns>
protected T? ExecuteAsync<T>(Func<System.Threading.Tasks.Task<T>> operation, string errorId)
{
try
{
return operation().GetAwaiter().GetResult();
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, errorId, ErrorCategory.InvalidOperation, null));
return default;
}
}
/// <summary>
/// Executes an async operation synchronously and handles errors.
/// </summary>
/// <param name="operation">The async operation to execute.</param>
/// <param name="errorId">The error ID for error reporting.</param>
protected void ExecuteAsync(Func<System.Threading.Tasks.Task> operation, string errorId)
{
try
{
operation().GetAwaiter().GetResult();
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, errorId, ErrorCategory.InvalidOperation, null));
}
}
}