using System.Management.Automation;
using PSMinIO.Utils;
namespace PSMinIO.Cmdlets
{
///
/// Tests whether a MinIO bucket exists
///
[Cmdlet(VerbsDiagnostic.Test, "MinIOBucketExists", SupportsShouldProcess = false)]
[OutputType(typeof(bool))]
public class TestMinIOBucketExistsCmdlet : MinIOBaseCmdlet
{
///
/// Name of the bucket to test
///
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
[Alias("Bucket")]
public string BucketName { get; set; } = string.Empty;
///
/// Return detailed information instead of just true/false
///
[Parameter]
public SwitchParameter Detailed { get; set; }
///
/// Processes the cmdlet
///
protected override void ProcessRecord()
{
ValidateConnection();
ValidateBucketName(BucketName);
ExecuteOperation("TestBucketExists", () =>
{
MinIOLogger.WriteVerbose(this, "Checking if bucket '{0}' exists", BucketName);
var exists = Client.BucketExists(BucketName);
if (Detailed.IsPresent)
{
// Return detailed information
var result = new PSObject();
result.Properties.Add(new PSNoteProperty("BucketName", BucketName));
result.Properties.Add(new PSNoteProperty("Exists", exists));
result.Properties.Add(new PSNoteProperty("Endpoint", Configuration.Endpoint));
result.Properties.Add(new PSNoteProperty("CheckedAt", System.DateTime.UtcNow));
if (exists)
{
try
{
// Try to get additional bucket information
var allBuckets = Client.ListBuckets();
var bucket = allBuckets.Find(b =>
string.Equals(b.Name, BucketName, System.StringComparison.OrdinalIgnoreCase));
if (bucket != null)
{
result.Properties.Add(new PSNoteProperty("Created", bucket.Created));
result.Properties.Add(new PSNoteProperty("Region", bucket.Region));
}
}
catch (System.Exception ex)
{
MinIOLogger.WriteVerbose(this,
"Could not retrieve additional bucket information: {0}", ex.Message);
}
}
WriteObject(result);
}
else
{
// Return simple boolean result
WriteObject(exists);
}
MinIOLogger.WriteVerbose(this,
"Bucket '{0}' exists: {1}", BucketName, exists);
}, $"Bucket: {BucketName}");
}
}
///
/// Custom output type for detailed bucket existence information
///
public class BucketExistenceInfo
{
///
/// Name of the bucket that was tested
///
public string BucketName { get; set; } = string.Empty;
///
/// Whether the bucket exists
///
public bool Exists { get; set; }
///
/// MinIO endpoint that was checked
///
public string Endpoint { get; set; } = string.Empty;
///
/// When the check was performed
///
public System.DateTime CheckedAt { get; set; }
///
/// Creation date of the bucket (if it exists and information is available)
///
public System.DateTime? Created { get; set; }
///
/// Region of the bucket (if it exists and information is available)
///
public string? Region { get; set; }
///
/// Creates a new BucketExistenceInfo instance
///
public BucketExistenceInfo()
{
CheckedAt = System.DateTime.UtcNow;
}
///
/// Creates a new BucketExistenceInfo instance with specified values
///
/// Name of the bucket
/// Whether the bucket exists
/// MinIO endpoint
public BucketExistenceInfo(string bucketName, bool exists, string endpoint)
{
BucketName = bucketName ?? string.Empty;
Exists = exists;
Endpoint = endpoint ?? string.Empty;
CheckedAt = System.DateTime.UtcNow;
}
///
/// Returns a string representation of the bucket existence info
///
public override string ToString()
{
return $"Bucket '{BucketName}' exists: {Exists} (checked at {CheckedAt:yyyy-MM-dd HH:mm:ss} UTC)";
}
}
}