namespace octo_fiesta.Services.Validation;
///
/// Base class for startup validators providing common functionality
///
public abstract class BaseStartupValidator : IStartupValidator
{
protected readonly HttpClient _httpClient;
protected BaseStartupValidator(HttpClient httpClient)
{
_httpClient = httpClient;
}
///
/// Gets the name of the service being validated
///
public abstract string ServiceName { get; }
///
/// Validates the service configuration and connectivity
///
public abstract Task ValidateAsync(CancellationToken cancellationToken);
///
/// Writes a status line to the console with colored output
///
protected static void WriteStatus(string label, string value, ConsoleColor valueColor)
{
Console.Write($" {label}: ");
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = valueColor;
Console.WriteLine(value);
Console.ForegroundColor = originalColor;
}
///
/// Writes a detail line to the console in dark gray
///
protected static void WriteDetail(string message)
{
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine($" -> {message}");
Console.ForegroundColor = originalColor;
}
///
/// Masks a secret string for display, showing only the first few characters
///
protected static string MaskSecret(string secret)
{
if (string.IsNullOrEmpty(secret))
{
return "(empty)";
}
const int visibleChars = 4;
if (secret.Length <= visibleChars)
{
return new string('*', secret.Length);
}
return secret[..visibleChars] + new string('*', Math.Min(secret.Length - visibleChars, 8));
}
///
/// Handles common HTTP exceptions and returns appropriate validation result
///
protected static ValidationResult HandleException(Exception ex, string fieldName)
{
return ex switch
{
TaskCanceledException => ValidationResult.Failure("TIMEOUT",
"Could not reach service within timeout period", ConsoleColor.Yellow),
HttpRequestException httpEx => ValidationResult.Failure("UNREACHABLE",
httpEx.Message, ConsoleColor.Yellow),
_ => ValidationResult.Failure("ERROR", ex.Message, ConsoleColor.Red)
};
}
///
/// Writes validation result to console
///
protected void WriteValidationResult(string fieldName, ValidationResult result)
{
WriteStatus(fieldName, result.Status, result.StatusColor);
if (!string.IsNullOrEmpty(result.Details))
{
WriteDetail(result.Details);
}
}
}