mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-05-02 03:34:06 +02:00
Move methods that convert enums and discriminated unions to sentences
This commit is contained in:
parent
3a17eee8d0
commit
339b958e45
Agent/Phantom.Agent.Services/Backups
Common/Phantom.Common.Data
Backups
Instance
Replies
Controller
Phantom.Controller.Minecraft
Phantom.Controller.Services/Users
Web/Phantom.Web
@ -102,7 +102,7 @@ sealed class BackupManager : IDisposable {
|
||||
|
||||
private void LogBackupResult(BackupCreationResult result) {
|
||||
if (result.Kind != BackupCreationResultKind.Success) {
|
||||
logger.Warning("Backup failed: {Reason}", result.Kind.ToSentence());
|
||||
logger.Warning("Backup failed: {Reason}", DescribeResult(result.Kind));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -114,5 +114,19 @@ sealed class BackupManager : IDisposable {
|
||||
logger.Information("Backup finished successfully.");
|
||||
}
|
||||
}
|
||||
|
||||
private static string DescribeResult(BackupCreationResultKind kind) {
|
||||
return kind switch {
|
||||
BackupCreationResultKind.Success => "Backup created successfully.",
|
||||
BackupCreationResultKind.InstanceNotRunning => "Instance is not running.",
|
||||
BackupCreationResultKind.BackupCancelled => "Backup cancelled.",
|
||||
BackupCreationResultKind.BackupAlreadyRunning => "A backup is already being created.",
|
||||
BackupCreationResultKind.BackupFileAlreadyExists => "Backup with the same name already exists.",
|
||||
BackupCreationResultKind.CouldNotCreateBackupFolder => "Could not create backup folder.",
|
||||
BackupCreationResultKind.CouldNotCopyWorldToTemporaryFolder => "Could not copy world to temporary folder.",
|
||||
BackupCreationResultKind.CouldNotCreateWorldArchive => "Could not create world archive.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,18 +20,4 @@ public static class BackupCreationResultSummaryExtensions {
|
||||
kind != BackupCreationResultKind.BackupAlreadyRunning &&
|
||||
kind != BackupCreationResultKind.BackupFileAlreadyExists;
|
||||
}
|
||||
|
||||
public static string ToSentence(this BackupCreationResultKind kind) {
|
||||
return kind switch {
|
||||
BackupCreationResultKind.Success => "Backup created successfully.",
|
||||
BackupCreationResultKind.InstanceNotRunning => "Instance is not running.",
|
||||
BackupCreationResultKind.BackupCancelled => "Backup cancelled.",
|
||||
BackupCreationResultKind.BackupAlreadyRunning => "A backup is already being created.",
|
||||
BackupCreationResultKind.BackupFileAlreadyExists => "Backup with the same name already exists.",
|
||||
BackupCreationResultKind.CouldNotCreateBackupFolder => "Could not create backup folder.",
|
||||
BackupCreationResultKind.CouldNotCopyWorldToTemporaryFolder => "Could not copy world to temporary folder.",
|
||||
BackupCreationResultKind.CouldNotCreateWorldArchive => "Could not create world archive.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -12,20 +12,3 @@ public enum InstanceLaunchFailReason : byte {
|
||||
CouldNotPrepareMinecraftServerLauncher = 8,
|
||||
CouldNotStartMinecraftServer = 9
|
||||
}
|
||||
|
||||
public static class InstanceLaunchFailReasonExtensions {
|
||||
public static string ToSentence(this InstanceLaunchFailReason reason) {
|
||||
return reason switch {
|
||||
InstanceLaunchFailReason.ServerPortNotAllowed => "Server port not allowed.",
|
||||
InstanceLaunchFailReason.ServerPortAlreadyInUse => "Server port already in use.",
|
||||
InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.",
|
||||
InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.",
|
||||
InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.",
|
||||
InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotConfigureMinecraftServer => "Could not configure Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotPrepareMinecraftServerLauncher => "Could not prepare Minecraft server launcher.",
|
||||
InstanceLaunchFailReason.CouldNotStartMinecraftServer => "Could not start Minecraft server.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -7,16 +7,3 @@ public enum LaunchInstanceResult : byte {
|
||||
InstanceLimitExceeded = 4,
|
||||
MemoryLimitExceeded = 5
|
||||
}
|
||||
|
||||
public static class LaunchInstanceResultExtensions {
|
||||
public static string ToSentence(this LaunchInstanceResult reason) {
|
||||
return reason switch {
|
||||
LaunchInstanceResult.LaunchInitiated => "Launch initiated.",
|
||||
LaunchInstanceResult.InstanceAlreadyLaunching => "Instance is already launching.",
|
||||
LaunchInstanceResult.InstanceAlreadyRunning => "Instance is already running.",
|
||||
LaunchInstanceResult.InstanceLimitExceeded => "Agent does not have any more available instances.",
|
||||
LaunchInstanceResult.MemoryLimitExceeded => "Agent does not have enough available memory.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,3 @@ public enum SendCommandToInstanceResult : byte {
|
||||
UnknownError,
|
||||
Success
|
||||
}
|
||||
|
||||
public static class SendCommandToInstanceResultExtensions {
|
||||
public static string ToSentence(this SendCommandToInstanceResult reason) {
|
||||
return reason switch {
|
||||
SendCommandToInstanceResult.Success => "Command sent.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -5,14 +5,3 @@ public enum StopInstanceResult : byte {
|
||||
InstanceAlreadyStopping = 2,
|
||||
InstanceAlreadyStopped = 3
|
||||
}
|
||||
|
||||
public static class StopInstanceResultExtensions {
|
||||
public static string ToSentence(this StopInstanceResult reason) {
|
||||
return reason switch {
|
||||
StopInstanceResult.StopInitiated => "Stopping initiated.",
|
||||
StopInstanceResult.InstanceAlreadyStopping => "Instance is already stopping.",
|
||||
StopInstanceResult.InstanceAlreadyStopped => "Instance is already stopped.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -37,13 +37,4 @@ public static class JvmArgumentsHelper {
|
||||
XmxNotAllowed,
|
||||
XmsNotAllowed
|
||||
}
|
||||
|
||||
public static string ToSentence(this ValidationError? result) {
|
||||
return result switch {
|
||||
ValidationError.InvalidFormat => "Invalid format.",
|
||||
ValidationError.XmxNotAllowed => "The -Xmx argument must not be specified manually.",
|
||||
ValidationError.XmsNotAllowed => "The -Xms argument must not be specified manually.",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(result), result, null)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -15,15 +15,3 @@ public abstract record AddUserError {
|
||||
|
||||
public sealed record UnknownError : AddUserError;
|
||||
}
|
||||
|
||||
public static class AddUserErrorExtensions {
|
||||
public static string ToSentences(this AddUserError error, string delimiter) {
|
||||
return error switch {
|
||||
AddUserError.NameIsEmpty => "Name cannot be empty.",
|
||||
AddUserError.NameIsTooLong e => "Name cannot be longer than " + e.MaximumLength + " character(s).",
|
||||
AddUserError.NameAlreadyExists => "Name is already occupied.",
|
||||
AddUserError.PasswordIsInvalid e => string.Join(delimiter, e.Violations.Select(static v => v.ToSentence())),
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -11,15 +11,3 @@ public abstract record PasswordRequirementViolation {
|
||||
|
||||
public sealed record DigitRequired : PasswordRequirementViolation;
|
||||
}
|
||||
|
||||
public static class PasswordRequirementViolationExtensions {
|
||||
public static string ToSentence(this PasswordRequirementViolation violation) {
|
||||
return violation switch {
|
||||
PasswordRequirementViolation.TooShort v => "Password must be at least " + v.MinimumLength + " character(s) long.",
|
||||
PasswordRequirementViolation.LowercaseLetterRequired => "Password must contain a lowercase letter.",
|
||||
PasswordRequirementViolation.UppercaseLetterRequired => "Password must contain an uppercase letter.",
|
||||
PasswordRequirementViolation.DigitRequired => "Password must contain a digit.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -11,13 +11,3 @@ public abstract record SetUserPasswordError {
|
||||
|
||||
public sealed record UnknownError : SetUserPasswordError;
|
||||
}
|
||||
|
||||
public static class SetUserPasswordErrorExtensions {
|
||||
public static string ToSentences(this SetUserPasswordError error, string delimiter) {
|
||||
return error switch {
|
||||
SetUserPasswordError.UserNotFound => "User not found.",
|
||||
SetUserPasswordError.PasswordIsInvalid e => string.Join(delimiter, e.Violations.Select(static v => v.ToSentence())),
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ else {
|
||||
await AuditLog.AddInstanceLaunchedEvent(InstanceGuid);
|
||||
}
|
||||
else {
|
||||
lastError = result.ToSentence(LaunchInstanceResultExtensions.ToSentence);
|
||||
lastError = result.ToSentence(Messages.ToSentence);
|
||||
}
|
||||
} finally {
|
||||
isLaunchingInstance = false;
|
||||
|
@ -46,7 +46,7 @@
|
||||
form.SubmitModel.StopSubmitting();
|
||||
}
|
||||
else {
|
||||
form.SubmitModel.StopSubmitting(result.ToSentence(SendCommandToInstanceResultExtensions.ToSentence));
|
||||
form.SubmitModel.StopSubmitting(result.ToSentence(Messages.ToSentence));
|
||||
}
|
||||
|
||||
await commandInputElement.FocusAsync(preventScroll: true);
|
||||
|
@ -63,7 +63,7 @@
|
||||
form.SubmitModel.StopSubmitting();
|
||||
}
|
||||
else {
|
||||
form.SubmitModel.StopSubmitting(result.ToSentence(StopInstanceResultExtensions.ToSentence));
|
||||
form.SubmitModel.StopSubmitting(result.ToSentence(Messages.ToSentence));
|
||||
}
|
||||
}
|
||||
|
||||
|
87
Web/Phantom.Web/Utils/Messages.cs
Normal file
87
Web/Phantom.Web/Utils/Messages.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using Phantom.Common.Data.Instance;
|
||||
using Phantom.Common.Data.Replies;
|
||||
using Phantom.Controller.Minecraft;
|
||||
using Phantom.Controller.Services.Users;
|
||||
|
||||
namespace Phantom.Web.Utils;
|
||||
|
||||
static class Messages {
|
||||
public static string ToSentences(this AddUserError error, string delimiter) {
|
||||
return error switch {
|
||||
AddUserError.NameIsEmpty => "Name cannot be empty.",
|
||||
AddUserError.NameIsTooLong e => "Name cannot be longer than " + e.MaximumLength + " character(s).",
|
||||
AddUserError.NameAlreadyExists => "Name is already occupied.",
|
||||
AddUserError.PasswordIsInvalid e => string.Join(delimiter, e.Violations.Select(static v => v.ToSentence())),
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToSentences(this SetUserPasswordError error, string delimiter) {
|
||||
return error switch {
|
||||
SetUserPasswordError.UserNotFound => "User not found.",
|
||||
SetUserPasswordError.PasswordIsInvalid e => string.Join(delimiter, e.Violations.Select(static v => v.ToSentence())),
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToSentence(this PasswordRequirementViolation violation) {
|
||||
return violation switch {
|
||||
PasswordRequirementViolation.TooShort v => "Password must be at least " + v.MinimumLength + " character(s) long.",
|
||||
PasswordRequirementViolation.LowercaseLetterRequired => "Password must contain a lowercase letter.",
|
||||
PasswordRequirementViolation.UppercaseLetterRequired => "Password must contain an uppercase letter.",
|
||||
PasswordRequirementViolation.DigitRequired => "Password must contain a digit.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToSentence(this JvmArgumentsHelper.ValidationError? result) {
|
||||
return result switch {
|
||||
JvmArgumentsHelper.ValidationError.InvalidFormat => "Invalid format.",
|
||||
JvmArgumentsHelper.ValidationError.XmxNotAllowed => "The -Xmx argument must not be specified manually.",
|
||||
JvmArgumentsHelper.ValidationError.XmsNotAllowed => "The -Xms argument must not be specified manually.",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(result), result, null)
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToSentence(this LaunchInstanceResult reason) {
|
||||
return reason switch {
|
||||
LaunchInstanceResult.LaunchInitiated => "Launch initiated.",
|
||||
LaunchInstanceResult.InstanceAlreadyLaunching => "Instance is already launching.",
|
||||
LaunchInstanceResult.InstanceAlreadyRunning => "Instance is already running.",
|
||||
LaunchInstanceResult.InstanceLimitExceeded => "Agent does not have any more available instances.",
|
||||
LaunchInstanceResult.MemoryLimitExceeded => "Agent does not have enough available memory.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToSentence(this InstanceLaunchFailReason reason) {
|
||||
return reason switch {
|
||||
InstanceLaunchFailReason.ServerPortNotAllowed => "Server port not allowed.",
|
||||
InstanceLaunchFailReason.ServerPortAlreadyInUse => "Server port already in use.",
|
||||
InstanceLaunchFailReason.RconPortNotAllowed => "Rcon port not allowed.",
|
||||
InstanceLaunchFailReason.RconPortAlreadyInUse => "Rcon port already in use.",
|
||||
InstanceLaunchFailReason.JavaRuntimeNotFound => "Java runtime not found.",
|
||||
InstanceLaunchFailReason.CouldNotDownloadMinecraftServer => "Could not download Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotConfigureMinecraftServer => "Could not configure Minecraft server.",
|
||||
InstanceLaunchFailReason.CouldNotPrepareMinecraftServerLauncher => "Could not prepare Minecraft server launcher.",
|
||||
InstanceLaunchFailReason.CouldNotStartMinecraftServer => "Could not start Minecraft server.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToSentence(this SendCommandToInstanceResult reason) {
|
||||
return reason switch {
|
||||
SendCommandToInstanceResult.Success => "Command sent.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToSentence(this StopInstanceResult reason) {
|
||||
return reason switch {
|
||||
StopInstanceResult.StopInitiated => "Stopping initiated.",
|
||||
StopInstanceResult.InstanceAlreadyStopping => "Instance is already stopping.",
|
||||
StopInstanceResult.InstanceAlreadyStopped => "Instance is already stopped.",
|
||||
_ => "Unknown error."
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user