Skip to content

Commit 5ecb474

Browse files
committed
Rework Proc command to Exec
1 parent d098ff4 commit 5ecb474

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

src/InEngine.Core/Commands/CommandsPlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class CommandPlugin : AbstractPlugin
77
[VerbOption("echo", HelpText= "Echo some text to the console. Useful for end-to-end testing.")]
88
public Echo Echo { get; set; }
99

10-
[VerbOption("proc", HelpText = "Launch an arbitrary process.")]
11-
public SystemProcess SystemProcess { get; set; }
10+
[VerbOption("exec", HelpText = "Execute an external program.")]
11+
public Exec Exec { get; set; }
1212
}
1313
}

src/InEngine.Core/Commands/SystemProcess.cs renamed to src/InEngine.Core/Commands/Exec.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1-
using System.Diagnostics;
1+
using System.Collections.Generic;
2+
using System.Diagnostics;
23
using System.IO;
34
using CommandLine;
45
using InEngine.Core.Exceptions;
56

67
namespace InEngine.Core.Commands
78
{
8-
public class SystemProcess : AbstractCommand
9+
public class Exec : AbstractCommand
910
{
10-
[Option('c', "command", Required = true, HelpText = "The name of the CLI program/command to run.")]
11-
public string Command { get; set; }
11+
[Option('e', "executable", Required = true, HelpText = "The name of the CLI program/command to run.")]
12+
public string Executable { get; set; }
1213

1314
[Option('a', "args", HelpText = "Arguments for the CLI program/command.")]
1415
public string Arguments { get; set; }
1516

16-
[Option('t', "timeout", DefaultValue = 900, HelpText = "The number of seconds to wait before killing the runnin process.")]
17+
[Option('t', "timeout", DefaultValue = 900, HelpText = "The number of seconds to wait before killing the running process.")]
1718
public int Timeout { get; set; }
1819

20+
public IDictionary<string, string> ExecWhitelist { get; set; }
21+
1922
public override void Run()
2023
{
21-
if (!File.Exists(Command))
22-
throw new CommandFailedException($"Cannot run {Command}. It either does not exist or is inaccessible. Exiting...");
24+
if (ExecWhitelist == null)
25+
ExecWhitelist = InEngineSettings.Make().ExecWhitelist;
26+
if (!ExecWhitelist.ContainsKey(Executable))
27+
throw new CommandFailedException("Executable is not whitelisted.");
28+
var fileName = ExecWhitelist[Executable];
29+
if (!File.Exists(fileName))
30+
throw new CommandFailedException($"Cannot run {fileName}. It either does not exist or is inaccessible. Exiting...");
2331
var process = new Process() {
24-
StartInfo = new ProcessStartInfo(Command, Arguments) {
32+
StartInfo = new ProcessStartInfo(fileName, Arguments) {
2533
UseShellExecute = false,
2634
ErrorDialog = false,
2735
RedirectStandardError = false,
2836
RedirectStandardInput = false,
2937
RedirectStandardOutput = false,
3038
}
3139
};
32-
var commandWithArguments = $"{Command} {Arguments}";
40+
var commandWithArguments = $"{fileName} {Arguments}";
3341
process.Start();
3442
if (process.WaitForExit(Timeout * 1000)) {
3543
return;

src/InEngine.Core/InEngineSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using InEngine.Core.Commands;
45
using InEngine.Core.IO;
56
using InEngine.Core.Queuing;
67
using Microsoft.Extensions.Configuration;
@@ -14,6 +15,7 @@ public class InEngineSettings
1415
public List<string> Plugins { get; set; } = new List<string>();
1516
public QueueSettings Queue { get; set; } = new QueueSettings();
1617
public MailSettings Mail { get; set; } = new MailSettings();
18+
public IDictionary<string, string> ExecWhitelist { get; set; } = new Dictionary<string, string>();
1719

1820
public static InEngineSettings Make()
1921
{

src/InEngine/appsettings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"Plugins": [
44
"InEngine.Commands"
55
],
6+
"ExecWhitelist": {
7+
},
68
"Mail": {
79
"Host": "localhost",
810
"Port": 1025,

0 commit comments

Comments
 (0)