beterraba

commit 438ded4048ae231ff93594a8ae42c0d0cc30779b

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

socket: improve service's boot executor a bit more

 cmd/beterrabad/executor.ha | 28 ++++++++++++++++++++++------
 cmd/beterrabad/socket.ha | 19 ++++++++++++++-----


diff --git a/cmd/beterrabad/executor.ha b/cmd/beterrabad/executor.ha
index 6917b14606d569697df145ae27dfaefc507709b6..ecad7703296c18d1f53dd3e3b682bb99f0a49f29 100644
--- a/cmd/beterrabad/executor.ha
+++ b/cmd/beterrabad/executor.ha
@@ -4,7 +4,9 @@ use os;
 use os::exec;
 use beterraba;
 
-fn start_service(name: str) beterraba::service = {
+type booterror = !(exec::error | exec::nocmd);
+
+fn start_service(cmdargs: str) str = {
 	let dummy_serv: beterraba::servdef = beterraba::servdef {
 		name = "Dummy",
 		desc = "Dummy",
@@ -21,9 +23,12 @@ 		status = beterraba::status::STOPPED,
 		...
 	};
 
-	boot(&service);
-
-	return service;
+	match (boot(&service)) {
+	case let err: booterror =>
+		return bootstrerror(err);
+	case void =>
+		return "Started successfully";
+	};
 };
 
 // Check if the process is still alive and bail out if it's ok
@@ -55,7 +60,9 @@ fn recover(service: *beterraba::service) void = {
 	fmt::printfln("Implement!")!;
 };
 
-fn boot(service: *beterraba::service) void = {
+// XXX: This method should yield a booterror which we should eventually
+// translate it back to a str, the same goes to other methods
+fn boot(service: *beterraba::service) (void | booterror) = {
 	log::printfln("Booting up {}", service.name);
 
 	match (exec::fork()) {
@@ -64,7 +71,7 @@ 			service.status = beterraba::status::STARTED;
 			service.process = childpid;
 			log::printfln("Starting process {}", service.name);
 	case let err: exec::error =>
-		log::fatal("Couldn't fork {}, error", exec::strerror(err));
+		return err;
 	case void =>
 		let cmd = exec::cmd("bash", "-c", "~/test.sh");
 
@@ -74,6 +81,7 @@ 			exec::exec(&cmddef);
 		case exec::nocmd =>
 			fmt::printfln("Couldn't build cmd {}", service.definition.cmd)!;
 		case exec::error =>
+			service.status = beterraba::status::CRASHED;
 			fmt::printfln("Couldn't execute cmd {}, error", service.definition.cmd)!;
 		case =>
 			fmt::println("Something went terrible wrong, good luck!")!;
@@ -82,5 +90,13 @@ 	};
 
 	//let cmd = exec::cmd(service.definition.cmd, service.definition.args);
 	let cmd = exec::cmd("bash", "-c", "~/test.sh");
+};
 
+fn bootstrerror(err: booterror) const str = {
+	match (err) {
+	case let err: exec::error =>
+		return exec::strerror(err);
+	case =>
+		return "Something awful wen't wrong due to unknown causes";
+	};
 };




diff --git a/cmd/beterrabad/socket.ha b/cmd/beterrabad/socket.ha
index 7c110b56ced62a2d3914c71d462db2802750625f..8b72c28ae9cb3d5172e56eca6321b38f8eae98cf 100644
--- a/cmd/beterrabad/socket.ha
+++ b/cmd/beterrabad/socket.ha
@@ -165,7 +165,7 @@ 	case io::error =>
 		disconnect_client(client);
 	};
 
-	let line = match (strings::fromutf8(bufline as []u8)) {
+	let line = match (strings::try_fromutf8(bufline as []u8)) {
 	case let s: str =>
 		yield s;
 	case utf8::invalid =>
@@ -180,15 +180,24 @@ 		log::fatal("Fudeu");
 	};
 };
 
-fn exec(command: str, client: *client) (servererror | void) = {
+fn exec(line: str, client: *client) (servererror | void) = {
 	let buf = bufio::dynamic(io::mode::WRITE);
+	let sline = strings::cut(line, " ");
+	let cmd = sline.0;
+	let args = sline.1;
 
 	// TODO: Flesh out command execution here
-	if (command == "ping") {
-		fmt::fprintf(&buf, "pong\n")?;
+	switch (cmd) {
+	case "start" =>
+		let status = start_service(args);
+		fmt::fprintf(&buf, status)?;
+	case "ping" =>
+		fmt::fprintf(&buf, "pong")?;
+	case =>
+		fmt::fprintf(&buf, "unknown command")?;
 	};
 
-	fmt::fprintln(&buf, "end")?;
+	fmt::fprintln(&buf, "\nend")?;
 	writebuf(client, bufio::buffer(&buf));
 };