-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExec.cs
More file actions
66 lines (57 loc) · 2.37 KB
/
Exec.cs
File metadata and controls
66 lines (57 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using CommandLine;
using InEngine.Core.Exceptions;
namespace InEngine.Core.Commands;
public class Exec : AbstractCommand
{
[Option('e', "executable", Required = true, HelpText = "The name of the CLI program/command to run.")]
public string Executable { get; set; }
[Option('a', "args", HelpText = "Arguments for the CLI program/command.")]
public string Arguments { get; set; }
[Option('t', "timeout", DefaultValue = 900,
HelpText = "The number of seconds to wait before killing the running process.")]
public int Timeout { get; set; }
public IDictionary<string, string> ExecWhitelist { get; set; }
public override async Task RunAsync()
{
ExecWhitelist ??= InEngineSettings.Make().ExecWhitelist;
if (!ExecWhitelist.ContainsKey(Executable))
throw new CommandFailedException("Executable is not whitelisted.");
var fileName = ExecWhitelist[Executable];
if (!File.Exists(fileName))
{
var message = $"Cannot run {fileName}. It either does not exist or is inaccessible. Exiting...";
throw new CommandFailedException(message);
}
var process = new Process
{
StartInfo = new ProcessStartInfo(fileName, Arguments)
{
UseShellExecute = false,
ErrorDialog = false,
RedirectStandardError = false,
RedirectStandardInput = false,
RedirectStandardOutput = false,
}
};
var commandWithArguments = $"{fileName} {Arguments}";
process.Start();
var timeoutSignal = new CancellationTokenSource(TimeSpan.FromSeconds(Timeout));
try
{
await process.WaitForExitAsync(timeoutSignal.Token).ConfigureAwait(false);
} catch (OperationCanceledException)
{
Error($"The command ({commandWithArguments}) has timed out and is about to be killed...");
process.Kill();
Error($"The command ({commandWithArguments}) has been killed.");
var message = $"The command ({commandWithArguments}) timed out after {Timeout} second(s).";
throw new CommandFailedException(message);
}
}
}