131 lines
3.3 KiB
C#
131 lines
3.3 KiB
C#
namespace StellaOps.Cli.Plugins.Vex;
|
|
|
|
public static class VexCliValidation
|
|
{
|
|
public static bool TryResolveTargetImage(
|
|
string? image,
|
|
string? check,
|
|
out string targetImage,
|
|
out string errorMessage)
|
|
{
|
|
targetImage = string.Empty;
|
|
errorMessage = string.Empty;
|
|
|
|
var hasImage = !string.IsNullOrWhiteSpace(image);
|
|
var hasCheck = !string.IsNullOrWhiteSpace(check);
|
|
|
|
if (!hasImage && !hasCheck)
|
|
{
|
|
errorMessage = "Either --image or --check must be specified.";
|
|
return false;
|
|
}
|
|
|
|
if (hasImage && hasCheck)
|
|
{
|
|
errorMessage = "--image and --check are mutually exclusive.";
|
|
return false;
|
|
}
|
|
|
|
targetImage = hasImage ? image! : check!;
|
|
return true;
|
|
}
|
|
|
|
public static bool TryValidateMin(string name, int value, int minInclusive, out string errorMessage)
|
|
{
|
|
errorMessage = string.Empty;
|
|
|
|
if (value < minInclusive)
|
|
{
|
|
errorMessage = $"{name} must be >= {minInclusive}.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool TryValidateMin(string name, double value, double minInclusive, out string errorMessage)
|
|
{
|
|
errorMessage = string.Empty;
|
|
|
|
if (value < minInclusive)
|
|
{
|
|
errorMessage = $"{name} must be >= {minInclusive}.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool TryValidateRange(
|
|
string name,
|
|
double value,
|
|
double minInclusive,
|
|
double maxInclusive,
|
|
out string errorMessage)
|
|
{
|
|
errorMessage = string.Empty;
|
|
|
|
if (value < minInclusive || value > maxInclusive)
|
|
{
|
|
errorMessage = $"{name} must be between {minInclusive} and {maxInclusive}.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool TryValidateOutputPath(string? outputPath, out string errorMessage)
|
|
{
|
|
errorMessage = string.Empty;
|
|
|
|
if (string.IsNullOrWhiteSpace(outputPath))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
try
|
|
{
|
|
var fullPath = Path.GetFullPath(outputPath);
|
|
if (Directory.Exists(fullPath))
|
|
{
|
|
errorMessage = "Output path must be a file, not a directory.";
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errorMessage = $"Output path is invalid: {ex.Message}";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static bool TryValidateServerUrl(string? serverUrl, out Uri? uri, out string errorMessage)
|
|
{
|
|
uri = null;
|
|
errorMessage = string.Empty;
|
|
|
|
if (string.IsNullOrWhiteSpace(serverUrl))
|
|
{
|
|
errorMessage = "Server URL is required.";
|
|
return false;
|
|
}
|
|
|
|
if (!Uri.TryCreate(serverUrl, UriKind.Absolute, out var parsed))
|
|
{
|
|
errorMessage = "Server URL must be an absolute URI.";
|
|
return false;
|
|
}
|
|
|
|
if (parsed.Scheme != Uri.UriSchemeHttp && parsed.Scheme != Uri.UriSchemeHttps)
|
|
{
|
|
errorMessage = "Server URL must use http or https.";
|
|
return false;
|
|
}
|
|
|
|
uri = parsed;
|
|
return true;
|
|
}
|
|
}
|