Execute commands defined in apps.json

This commit is contained in:
loki
2019-12-15 19:36:22 +01:00
parent 20f5f90588
commit b7eab0883c
9 changed files with 194 additions and 105 deletions

View File

@@ -6,9 +6,6 @@
#include <filesystem>
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "nvhttp.h"
#include "stream.h"
#include "config.h"
@@ -18,85 +15,9 @@ extern "C" {
#include <rs.h>
}
using namespace std::literals;
namespace pt = boost::property_tree;
std::optional<proc::proc_t> parse(const std::string& file_name) {
pt::ptree tree;
try {
pt::read_json(file_name, tree);
auto &apps_node = tree.get_child("apps"s);
auto &env_vars = tree.get_child("env"s);
boost::process::environment env = boost::this_process::environment();
std::unordered_map<std::string, proc::ctx_t> apps;
for(auto &[_,app_node] : apps_node) {
proc::ctx_t ctx;
auto &prep_nodes = app_node.get_child("prep-cmd"s);
auto name = app_node.get<std::string>("name"s);
auto output = app_node.get_optional<std::string>("output"s);
auto cmd = app_node.get<std::string>("cmd"s);
std::vector<proc::cmd_t> prep_cmds;
prep_cmds.reserve(prep_nodes.size());
for(auto &[_, prep_node] : prep_nodes) {
auto do_cmd = prep_node.get<std::string>("do"s);
auto undo_cmd = prep_node.get_optional<std::string>("undo"s);
if(undo_cmd) {
prep_cmds.emplace_back(std::move(do_cmd), std::move(*undo_cmd));
}
else {
prep_cmds.emplace_back(std::move(do_cmd));
}
}
if(output) {
ctx.output = std::move(*output);
}
ctx.cmd = std::move(cmd);
ctx.prep_cmds = std::move(prep_cmds);
apps.emplace(std::move(name), std::move(ctx));
}
for(auto &[_,env_var] : env_vars) {
for(auto &[name,val] : env_var) {
env[name] = val.get_value<std::string>();
}
}
return proc::proc_t {
std::move(env), std::move(apps)
};
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
return std::nullopt;
}
int main(int argc, char *argv[]) {
auto proc_opt = parse(SUNSHINE_ASSETS_DIR "/apps.json");
if(!proc_opt) {
return 7;
}
auto &proc = *proc_opt;
proc.execute("echo-2");
std::this_thread::sleep_for(50ms);
proc.execute("echo-1");
std::this_thread::sleep_for(50ms);
return proc.running();
if(argc > 1) {
if(!std::filesystem::exists(argv[1])) {
std::cout << "Error: Couldn't find configuration file ["sv << argv[1] << ']' << std::endl;
@@ -106,6 +27,12 @@ int main(int argc, char *argv[]) {
config::parse_file(argv[1]);
}
auto proc_opt = proc::parse(SUNSHINE_ASSETS_DIR "/apps.json");
if(!proc_opt) {
return 7;
}
proc::proc = std::move(*proc_opt);
reed_solomon_init();
std::thread httpThread { nvhttp::start };