using System;
using System.Collections.Generic;
using TGS.Interface;
using TGS.Interface.Components;
namespace TGS.CommandLine
{
///
/// Used for managing the components
///
class ServiceCommand : RootCommand
{
///
/// Construct a
///
public ServiceCommand()
{
Keyword = "service";
Children = new Command[] { new ServiceCreateInstanceCommand(), new ServiceDetachInstanceCommand(), new ServiceEnableInstanceCommand(), new ServiceImportInstanceCommand(), new ServiceListInstancesCommand(), new ServicePythonPathCommand(), new ServiceSetPythonPathCommand(), new ServiceSetRemoteAccessPortCommand(), new ServiceRemoteAccessPortCommand(), new ServiceDisableInstanceCommand(), new ServiceRenameInstanceCommand() };
}
///
public override string GetHelpText()
{
return "Manage service wide settings";
}
}
///
/// Command for calling
///
class ServiceCreateInstanceCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceCreateInstanceCommand()
{
Keyword = "create-instance";
RequiredParameters = 2;
}
///
public override string GetHelpText()
{
return "Creates a new instance at the given path";
}
///
public override string GetArgumentString()
{
return " ";
}
///
protected override ExitCode Run(IList parameters)
{
var res = Interface.GetServiceComponent().CreateInstance(parameters[0], parameters[1]);
if (res != null)
{
OutputProc(res);
return ExitCode.ServerError;
}
return ExitCode.Normal;
}
}
///
/// Command for calling
///
class ServiceListInstancesCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceListInstancesCommand()
{
Keyword = "list-instances";
}
///
public override string GetHelpText()
{
return "Lists all instances";
}
///
protected override ExitCode Run(IList parameters)
{
foreach (var I in Interface.GetServiceComponent().ListInstances())
OutputProc(String.Format("{0} ({1}):\t{2}{3}", I.Name, I.Path, I.Enabled ? "Online" : "Offline", I.Enabled ? String.Format(" ({0})", I.LoggingID) : ""));
return ExitCode.Normal;
}
}
///
/// Command for calling
///
class ServiceDetachInstanceCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceDetachInstanceCommand()
{
Keyword = "detach-instance";
RequiredParameters = 1;
}
///
public override string GetHelpText()
{
return "Detaches an instance";
}
///
public override string GetArgumentString()
{
return "";
}
///
protected override ExitCode Run(IList parameters)
{
var res = Interface.GetServiceComponent().DetachInstance(parameters[0]);
if (res != null)
{
OutputProc(res);
return ExitCode.ServerError;
}
return ExitCode.Normal;
}
}
///
/// Command for calling
///
class ServiceImportInstanceCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceImportInstanceCommand()
{
Keyword = "import-instance";
RequiredParameters = 1;
}
///
public override string GetHelpText()
{
return "Imports an instance (Chat settings will be lost if they are from a different windows installation)";
}
///
public override string GetArgumentString()
{
return "";
}
///
protected override ExitCode Run(IList parameters)
{
var res = Interface.GetServiceComponent().ImportInstance(parameters[0]);
if (res != null)
{
OutputProc(res);
return ExitCode.ServerError;
}
return ExitCode.Normal;
}
}
///
/// Command for calling
///
class ServicePythonPathCommand : ConsoleCommand
{
///
/// Construct a
///
public ServicePythonPathCommand()
{
Keyword = "python";
}
///
public override string GetHelpText()
{
return "Displays configured path the service uses for python";
}
///
protected override ExitCode Run(IList parameters)
{
var res = Interface.GetServiceComponent().PythonPath();
if (res != null)
{
OutputProc(res);
return ExitCode.ServerError;
}
return ExitCode.Normal;
}
}
///
/// Command for calling
///
class ServiceSetPythonPathCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceSetPythonPathCommand()
{
Keyword = "set-python";
RequiredParameters = 1;
}
///
public override string GetHelpText()
{
return "Sets the path to the python installation";
}
///
public override string GetArgumentString()
{
return "";
}
///
protected override ExitCode Run(IList parameters)
{
Interface.GetServiceComponent().SetPythonPath(parameters[0]);
return ExitCode.Normal;
}
}
///
/// Command for calling with a parameter
///
class ServiceEnableInstanceCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceEnableInstanceCommand()
{
Keyword = "enable-instance";
RequiredParameters = 1;
}
///
public override string GetHelpText()
{
return "Enables the specified instance";
}
///
public override string GetArgumentString()
{
return "";
}
///
protected override ExitCode Run(IList parameters)
{
var res = Interface.GetServiceComponent().SetInstanceEnabled(parameters[0], true);
if (res != null)
{
OutputProc(res);
return ExitCode.ServerError;
}
return ExitCode.Normal;
}
}
///
/// Command for calling with a parameter
///
class ServiceDisableInstanceCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceDisableInstanceCommand()
{
Keyword = "disable-instance";
RequiredParameters = 1;
}
///
public override string GetHelpText()
{
return "Disables the specified instance";
}
///
public override string GetArgumentString()
{
return "";
}
///
protected override ExitCode Run(IList parameters)
{
var res = Interface.GetServiceComponent().SetInstanceEnabled(parameters[0], false);
if (res != null)
{
OutputProc(res);
return ExitCode.ServerError;
}
return ExitCode.Normal;
}
}
///
/// Command for calling
///
class ServiceRemoteAccessPortCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceRemoteAccessPortCommand()
{
Keyword = "port";
}
///
public override string GetHelpText()
{
return "Displays the service's remote access port";
}
///
protected override ExitCode Run(IList parameters)
{
OutputProc(Interface.GetServiceComponent().RemoteAccessPort().ToString());
return ExitCode.Normal;
}
}
///
/// Command for calling
///
class ServiceSetRemoteAccessPortCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceSetRemoteAccessPortCommand()
{
Keyword = "set-port";
RequiredParameters = 1;
}
///
public override string GetHelpText()
{
return "Sets the service's remote access port";
}
///
public override string GetArgumentString()
{
return "";
}
///
protected override ExitCode Run(IList parameters)
{
ushort port;
try
{
port = Convert.ToUInt16(parameters[0]);
}
catch
{
OutputProc("Invalid port number!");
return ExitCode.BadCommand;
}
var res = Interface.GetServiceComponent().SetRemoteAccessPort(port);
if (res != null)
{
OutputProc(res);
return ExitCode.ServerError;
}
OutputProc("Change will be applied after service restart");
return ExitCode.Normal;
}
}
///
/// Command for calling
///
class ServiceRenameInstanceCommand : ConsoleCommand
{
///
/// Construct a
///
public ServiceRenameInstanceCommand()
{
Keyword = "rename-instance";
RequiredParameters = 1;
}
///
public override string GetHelpText()
{
return "Renames an instance. Will temporarily disable the instance if it is active";
}
///
public override string GetArgumentString()
{
return " ";
}
///
protected override ExitCode Run(IList parameters)
{
var res = Interface.GetServiceComponent().RenameInstance(parameters[0], parameters[1]);
if (res != null)
{
OutputProc(res);
return ExitCode.ServerError;
}
return ExitCode.Normal;
}
}
}