Skip to content

Commit

Permalink
ref - No need to query the command list twice
Browse files Browse the repository at this point in the history
---

Type: ref
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Jun 11, 2024
1 parent be4e91c commit 5838e5e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
15 changes: 4 additions & 11 deletions public/Nitrocid/Shell/ShellBase/Commands/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ internal static void StartCommandThread(CommandExecutorParameters ThreadParams)
}
}

internal static void ExecuteCommand(CommandExecutorParameters ThreadParams) =>
ExecuteCommand(ThreadParams, CommandManager.GetCommands(ThreadParams.ShellType));

private static void ExecuteCommand(CommandExecutorParameters ThreadParams, CommandInfo[] TargetCommands)
internal static void ExecuteCommand(CommandExecutorParameters ThreadParams)
{
var RequestedCommand = ThreadParams.RequestedCommand;
var RequestedCommandInfo = ThreadParams.RequestedCommandInfo;
Expand Down Expand Up @@ -189,20 +186,16 @@ private static void ExecuteCommand(CommandExecutorParameters ThreadParams, Comma
string StrArgsOrig = ArgumentInfo.ArgumentsTextOrig;
bool containsSetSwitch = SwitchManager.ContainsSwitch(Switches, "-set");
string variable = "";
var cmdInfo =
TargetCommands.Any((ci) => ci.Command == Command) ?
TargetCommands.Single((ci) => ci.Command == Command) :
RequestedCommandInfo;

// Check to see if a requested command is obsolete
if (cmdInfo.Flags.HasFlag(CommandFlags.Obsolete))
if (RequestedCommandInfo.Flags.HasFlag(CommandFlags.Obsolete))
{
DebugWriter.WriteDebug(DebugLevel.I, "The command requested {0} is obsolete", Command);
TextWriterColor.Write(Translate.DoTranslation("This command is obsolete and will be removed in a future release."));
}

// If there are enough arguments provided, execute. Otherwise, fail with not enough arguments.
var ArgInfos = cmdInfo.CommandArgumentInfo;
var ArgInfos = RequestedCommandInfo.CommandArgumentInfo;
for (int i = 0; i < ArgInfos.Length; i++)
{
argSatisfied = true;
Expand Down Expand Up @@ -247,7 +240,7 @@ private static void ExecuteCommand(CommandExecutorParameters ThreadParams, Comma

// Now, get the base command and execute it
DebugWriter.WriteDebug(DebugLevel.I, "Really executing command {0} with args {1}", Command, StrArgs);
var CommandBase = cmdInfo.CommandBase;
var CommandBase = RequestedCommandInfo.CommandBase;
string value = "";
CancellationHandlers.cts = new CancellationTokenSource();
#pragma warning disable SYSLIB0046
Expand Down
6 changes: 3 additions & 3 deletions public/Nitrocid/Shell/ShellBase/Shells/ShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ internal static void GetLine(string FullCommand, string OutputPath = "", string
TextWriters.Write(Translate.DoTranslation("Failed to start \"{0}\": {1}"), true, KernelColorType.Error, commandName, ex.Message);
DebugWriter.WriteDebugStackTrace(ex);
if (ex is KernelException kex)
UESHVariables.SetVariable("UESHErrorCode", $"{10000 + (int)kex.ExceptionType}");
UESHVariables.SetVariable("UESHErrorCode", $"{KernelExceptionTools.GetErrorCode(kex)}");
else
UESHVariables.SetVariable("UESHErrorCode", $"{ex.GetHashCode()}");
}
Expand All @@ -575,7 +575,7 @@ internal static void GetLine(string FullCommand, string OutputPath = "", string
TextWriters.Write(Translate.DoTranslation("Error trying to execute script: {0}"), true, KernelColorType.Error, ex.Message);
DebugWriter.WriteDebugStackTrace(ex);
if (ex is KernelException kex)
UESHVariables.SetVariable("UESHErrorCode", $"{10000 + (int)kex.ExceptionType}");
UESHVariables.SetVariable("UESHErrorCode", $"{KernelExceptionTools.GetErrorCode(kex)}");
else
UESHVariables.SetVariable("UESHErrorCode", $"{ex.GetHashCode()}");
}
Expand All @@ -599,7 +599,7 @@ internal static void GetLine(string FullCommand, string OutputPath = "", string
DebugWriter.WriteDebugStackTrace(ex);
TextWriters.Write(Translate.DoTranslation("Error trying to execute command.") + CharManager.NewLine + Translate.DoTranslation("Error {0}: {1}"), true, KernelColorType.Error, ex.GetType().FullName, ex.Message);
if (ex is KernelException kex)
UESHVariables.SetVariable("UESHErrorCode", $"{10000 + (int)kex.ExceptionType}");
UESHVariables.SetVariable("UESHErrorCode", $"{KernelExceptionTools.GetErrorCode(kex)}");
else
UESHVariables.SetVariable("UESHErrorCode", $"{ex.GetHashCode()}");
}
Expand Down
2 changes: 1 addition & 1 deletion public/Nitrocid/Shell/Shells/UESH/Commands/Sudo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override int Execute(CommandParameters parameters, ref string variableVal
UserManagement.UnlockUser("root");
}
}
return failed ? 10000 + (int)KernelExceptionType.ShellOperation : 0;
return failed ? KernelExceptionTools.GetErrorCode(KernelExceptionType.ShellOperation) : 0;
}

}
Expand Down

0 comments on commit 5838e5e

Please sign in to comment.