Skip to content

Commit 794a5d8

Browse files
committed
add exec command
1 parent 8d99674 commit 794a5d8

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

exec.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.IO;
2+
using System.Diagnostics;
3+
using ILeoConsole;
4+
using ILeoConsole.Plugin;
5+
using ILeoConsole.Core;
6+
7+
namespace LeoConsole_externalScripts
8+
{
9+
public class Exec : ICommand
10+
{
11+
public string Name { get { return "exec"; } }
12+
public string Description { get { return "execute arbitrary command or program"; } }
13+
public Action CommandFunktion { get { return () => Command(); } }
14+
private string[] _InputProperties;
15+
public string[] InputProperties { get { return _InputProperties; } set { _InputProperties = value; } }
16+
public IData data = new ConsoleData();
17+
18+
public void Command()
19+
{
20+
if (_InputProperties.Length < 2){
21+
Console.WriteLine("you need to provide the command to run");
22+
return;
23+
}
24+
string command = _InputProperties[1];
25+
string args = "";
26+
for (int i = 2; i < _InputProperties.Length; i++) {
27+
args = args + " " + _InputProperties[i];
28+
}
29+
Console.WriteLine("executing " + command + " " + args + "...");
30+
if (!runProcess(command, args, data.SavePath)) {
31+
Console.WriteLine("error executing " + command);
32+
}
33+
}
34+
35+
// run a process with parameters and wait for it to finish
36+
private bool runProcess(string name, string args, string pwd) {
37+
try {
38+
Process p = new Process();
39+
p.StartInfo.FileName = name;
40+
p.StartInfo.Arguments = args;
41+
p.StartInfo.WorkingDirectory = pwd;
42+
p.Start();
43+
p.WaitForExit();
44+
if (p.ExitCode != 0) {
45+
return false;
46+
}
47+
} catch (Exception e) {
48+
Console.WriteLine("error: " + e.Message);
49+
return false;
50+
}
51+
return true;
52+
}
53+
54+
}
55+
}
56+
57+
// vim: tabstop=2 softtabstop=2 shiftwidth=2 expandtab

plugin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public void PluginMain()
3434
_Commands = new List<ICommand>();
3535
_Commands.Add(new Script());
3636
_Commands.Add(new ListScripts());
37+
_Commands.Add(new Exec());
3738

39+
// TODO SavePath is not set yet, see https://github.com/BoettcherDasOriginal/LeoConsole/issues/12
3840
if (!Directory.Exists(Path.Join(_data.SavePath, "scripts"))) {
3941
Console.WriteLine("creating scripts directory...");
4042
try {

0 commit comments

Comments
 (0)