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
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:
114
classlib/Cmdlets/BaseCmdlet.cs
Normal file
114
classlib/Cmdlets/BaseCmdlet.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
classlib/IsExternalInit.cs
Normal file
23
classlib/IsExternalInit.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// Licensed to the .NET Foundation under one or more agreements.
|
||||||
|
// The .NET Foundation licenses this file to you under the MIT license.
|
||||||
|
|
||||||
|
// This file provides the IsExternalInit type required for using C# 9 records
|
||||||
|
// in projects targeting .NET Standard 2.1 or earlier.
|
||||||
|
|
||||||
|
#if !NET5_0_OR_GREATER
|
||||||
|
|
||||||
|
namespace System.Runtime.CompilerServices
|
||||||
|
{
|
||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reserved to be used by the compiler for tracking metadata.
|
||||||
|
/// This class should not be used by developers in source code.
|
||||||
|
/// </summary>
|
||||||
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
|
internal static class IsExternalInit
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
22
classlib/enum/SessionStatus.cs
Normal file
22
classlib/enum/SessionStatus.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
namespace PS.IPAM;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents the current status of an API session.
|
||||||
|
/// </summary>
|
||||||
|
public enum SessionStatus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// No session exists or no token is available.
|
||||||
|
/// </summary>
|
||||||
|
NoSession,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The session token has expired.
|
||||||
|
/// </summary>
|
||||||
|
Expired,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The session is valid and ready for use.
|
||||||
|
/// </summary>
|
||||||
|
Valid
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user