Files
spark/src/client/sparkcmd/Cmd/ScriptClientCommand.cs
T

123 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 ManyConsole;
using System.Linq;
namespace sparkcmd {
public class ScriptClientCommand : ConsoleCommand {
bool _server = false;
string _host;
string _logfile;
public ScriptClientCommand() {
IsCommand("script", "Remote control for shell diagnostic host");
HasOption("s|server", "Run a script server for testing.", x => _server = true);
HasOption("h|host", "Connect to {host}", h => _host = h);
HasOption("f|fromfile", "Read commands from file instead of stdin", f => _logfile = f);
}
public override int Run(string[] args) {
if (_server) {
ScriptClient.runserver();
}
else {
var host = string.IsNullOrEmpty(_host) ? args.First() : _host;
if (string.IsNullOrEmpty(_logfile) && File.Exists(_logfile)) {
ScriptClient.start(host, File.OpenRead(_logfile));
}
else {
ScriptClient.start(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);
}
}
}
}