128 lines
4.5 KiB
C#
128 lines
4.5 KiB
C#
using bts.api.core;
|
|
using bts.api.diagnostic;
|
|
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using System.Reactive.Linq;
|
|
using System.Reactive;
|
|
using System.Linq;
|
|
using CommandLine;
|
|
|
|
namespace sparkcmd {
|
|
[Verb("script", HelpText="Remote control for shell diagnostic host")]
|
|
public class ScriptClientOptions {
|
|
[Option('s', "server", HelpText="Run a script server for testing.")]
|
|
public bool server { get; set; }
|
|
|
|
[Option('h', "host", HelpText="Connect to {host}")]
|
|
public string host { get; set; }
|
|
|
|
[Option('f', "fromfile", HelpText="Read commands from file instead of stdin")]
|
|
public string file { get; set; }
|
|
|
|
}
|
|
public class ScriptClientCommand {
|
|
bool _server = false;
|
|
string _host;
|
|
string _logfile;
|
|
|
|
public static int Run(ScriptClientOptions o) {
|
|
if (o.server) {
|
|
ScriptClient.runserver();
|
|
}
|
|
else {
|
|
if (string.IsNullOrEmpty(o.file) && File.Exists(o.file)) {
|
|
ScriptClient.start(o.host, File.OpenRead(o.file));
|
|
}
|
|
else {
|
|
ScriptClient.start(o.host, Console.OpenStandardInput());
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
internal static class ScriptClient {
|
|
internal static void start(string qualifier, Stream stream) {
|
|
using (var ctx = new ServiceContext()) {
|
|
ShellDiagnosticService diag = ctx.locate<ShellDiagnosticService>(qualifier);
|
|
var script_id = diag.create(null);
|
|
ShellScriptHost script = ctx.locate<ShellScriptHost>(script_id.Id);
|
|
try {
|
|
using (var sub = script.subscribei_output().Subscribe(msg => script_output(msg))) {
|
|
ScriptResult status = ScriptResult.Complete;
|
|
using (var reader = new StreamReader(stream)) {
|
|
do {
|
|
write_prompt(status);
|
|
var line = reader.ReadLine();
|
|
if (line != "#q") {
|
|
var result = script.run(new ScriptLines { Lines = { line } });
|
|
status = result.Res;
|
|
if (status == ScriptResult.Invalid) {
|
|
Console.WriteLine($"Syntax error: '{result.Message}'");
|
|
}
|
|
else if (status == ScriptResult.RunError) {
|
|
Console.WriteLine($"Exception: {result.Message}");
|
|
}
|
|
else if (status == ScriptResult.Complete && !string.IsNullOrEmpty(result.Message)) {
|
|
Console.WriteLine(result.Message);
|
|
}
|
|
}
|
|
else {
|
|
return;
|
|
}
|
|
} while (true);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
Console.WriteLine($"Done. {e.Message}");
|
|
}
|
|
finally {
|
|
try {
|
|
script.dispose(null);
|
|
script.Dispose();
|
|
}
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
}
|
|
|
|
static void script_output(Update<ScriptLinesImmutable, int> update) {
|
|
foreach (var lines in update.added) {
|
|
foreach (var line in lines.Lines) {
|
|
Console.WriteLine(line);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void write_prompt(ScriptResult status) {
|
|
switch (status) {
|
|
case ScriptResult.Complete:
|
|
Console.Write(">>> ");
|
|
break;
|
|
case ScriptResult.Incomplete:
|
|
Console.Write("... ");
|
|
break;
|
|
case ScriptResult.Invalid:
|
|
Console.Write("?>> ");
|
|
break;
|
|
case ScriptResult.RunError:
|
|
Console.Write("!>> ");
|
|
break;
|
|
}
|
|
}
|
|
|
|
public static void runserver() {
|
|
// for debugging
|
|
using (var ctx = new ServiceContext()) {
|
|
var assembly = Assembly.Load("spark.exe");
|
|
var stype = assembly.GetType("bts.api.diagnostic.ShellDiagnosticsServiceServer");
|
|
var server = Activator.CreateInstance(stype, ctx);
|
|
while (true) System.Threading.Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
} |