Gymboard/gymboard-cli/source/cli.d

80 lines
2.5 KiB
D
Raw Permalink Normal View History

module cli;
import std.stdio;
import std.string;
import std.uni;
2023-03-26 11:33:10 +00:00
import std.typecons;
import consolecolors;
2023-05-17 10:40:01 +00:00
import command.base;
import services;
class ServiceCommand : CliCommand {
private ServiceManager serviceManager;
public this(ServiceManager serviceManager) {
this.serviceManager = serviceManager;
}
void handle(string[] args) {
if (args.length == 0) {
2023-03-26 11:33:10 +00:00
cwriteln("Missing subcommand.".red);
return;
}
string subcommand = args[0];
if (subcommand == "status") {
auto statuses = serviceManager.getStatus();
if (statuses.length == 0) {
2023-03-26 11:33:10 +00:00
cwriteln("No services running.".orange);
}
foreach (status; statuses) {
writefln!"%s: Running = %s, Exit code = %s"(status.name, status.running, status.exitCode);
}
} else if (subcommand == "start") {
2023-03-26 11:33:10 +00:00
auto info = validateServiceNameArg(args);
if (!info.isNull) serviceManager.startService(info.get);
} else if (subcommand == "stop") {
2023-03-26 11:33:10 +00:00
auto info = validateServiceNameArg(args);
if (!info.isNull) serviceManager.stopService(info.get);
} else if (subcommand == "logs") {
auto info = validateServiceNameArg(args);
if (!info.isNull) serviceManager.showLogs(info.get);
} else if (subcommand == "follow") {
auto info = validateServiceNameArg(args);
if (!info.isNull) serviceManager.follow(info.get);
} else {
2023-03-26 11:33:10 +00:00
cwriteln("Unknown subcommand.".red);
}
}
2023-05-17 10:40:01 +00:00
string name() const {
return "Service";
}
string description() const {
return "bleh";
}
2023-03-26 11:33:10 +00:00
/**
* Validates that a service command contains as its second argument a valid
* service name.
* Params:
* args = The arguments.
* Returns: The service name, or null if none was found.
*/
private Nullable!(const(ServiceInfo)) validateServiceNameArg(string[] args) {
import std.string : toLower, strip;
if (args.length < 2) {
cwriteln("Missing required service name as argument to the service subcommand.".red);
return Nullable!(const(ServiceInfo)).init;
}
string serviceName = args[1].strip().toLower();
Nullable!(const(ServiceInfo)) info = getServiceByName(serviceName);
if (info.isNull) {
cwritefln("There is no service named %s.".red, serviceName.orange);
}
2023-03-26 11:33:10 +00:00
return info;
}
}