|
| 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 |
0 commit comments