Add support for global prep commands (#977)
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
|
||||
#include "config.h"
|
||||
#include "main.h"
|
||||
@@ -426,6 +428,7 @@ sunshine_t sunshine {
|
||||
{}, // cmd args
|
||||
47989,
|
||||
platf::appdata().string() + "/sunshine.log", // log file
|
||||
{}, // prep commands
|
||||
};
|
||||
|
||||
bool endline(char ch) {
|
||||
@@ -759,6 +762,27 @@ void list_string_f(std::unordered_map<std::string, std::string> &vars, const std
|
||||
input.emplace_back(begin, pos);
|
||||
}
|
||||
}
|
||||
void list_prep_cmd_f(std::unordered_map<std::string, std::string> &vars, const std::string &name, std::vector<prep_cmd_t> &input) {
|
||||
std::string string;
|
||||
string_f(vars, name, string);
|
||||
|
||||
std::stringstream jsonStream;
|
||||
|
||||
// We need to add a wrapping object to make it valid JSON, otherwise ptree cannot parse it.
|
||||
jsonStream << "{\"prep_cmd\":" << string << "}";
|
||||
|
||||
boost::property_tree::ptree jsonTree;
|
||||
boost::property_tree::read_json(jsonStream, jsonTree);
|
||||
|
||||
for(auto &[_, prep_cmd] : jsonTree.get_child("prep_cmd"s)) {
|
||||
auto do_cmd = prep_cmd.get<std::string>("do"s);
|
||||
auto undo_cmd = prep_cmd.get<std::string>("undo"s);
|
||||
|
||||
input.emplace_back(
|
||||
std::move(do_cmd),
|
||||
std::move(undo_cmd));
|
||||
}
|
||||
}
|
||||
|
||||
void list_int_f(std::unordered_map<std::string, std::string> &vars, const std::string &name, std::vector<int> &input) {
|
||||
std::vector<std::string> list;
|
||||
@@ -902,6 +926,7 @@ void apply_config(std::unordered_map<std::string, std::string> &&vars) {
|
||||
string_f(vars, "external_ip", nvhttp.external_ip);
|
||||
list_string_f(vars, "resolutions"s, nvhttp.resolutions);
|
||||
list_int_f(vars, "fps"s, nvhttp.fps);
|
||||
list_prep_cmd_f(vars, "global_prep_cmd", config::sunshine.prep_cmds);
|
||||
|
||||
string_f(vars, "audio_sink", audio.sink);
|
||||
string_f(vars, "virtual_sink", audio.virtual_sink);
|
||||
|
||||
@@ -118,6 +118,13 @@ enum flag_e : std::size_t {
|
||||
};
|
||||
}
|
||||
|
||||
struct prep_cmd_t {
|
||||
prep_cmd_t(std::string &&do_cmd, std::string &&undo_cmd) : do_cmd(std::move(do_cmd)), undo_cmd(std::move(undo_cmd)) {}
|
||||
explicit prep_cmd_t(std::string &&do_cmd) : do_cmd(std::move(do_cmd)) {}
|
||||
std::string do_cmd;
|
||||
std::string undo_cmd;
|
||||
};
|
||||
|
||||
struct sunshine_t {
|
||||
int min_log_level;
|
||||
std::bitset<flag::FLAG_SIZE> flags;
|
||||
@@ -137,6 +144,8 @@ struct sunshine_t {
|
||||
|
||||
std::uint16_t port;
|
||||
std::string log_file;
|
||||
|
||||
std::vector<prep_cmd_t> prep_cmds;
|
||||
};
|
||||
|
||||
extern video_t video;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "crypto.h"
|
||||
#include "main.h"
|
||||
#include "platform/common.h"
|
||||
@@ -457,19 +458,30 @@ std::optional<proc::proc_t> parse(const std::string &file_name) {
|
||||
for(auto &[_, app_node] : apps_node) {
|
||||
proc::ctx_t ctx;
|
||||
|
||||
auto prep_nodes_opt = app_node.get_child_optional("prep-cmd"s);
|
||||
auto detached_nodes_opt = app_node.get_child_optional("detached"s);
|
||||
auto output = app_node.get_optional<std::string>("output"s);
|
||||
auto name = parse_env_val(this_env, app_node.get<std::string>("name"s));
|
||||
auto cmd = app_node.get_optional<std::string>("cmd"s);
|
||||
auto image_path = app_node.get_optional<std::string>("image-path"s);
|
||||
auto working_dir = app_node.get_optional<std::string>("working-dir"s);
|
||||
auto prep_nodes_opt = app_node.get_child_optional("prep-cmd"s);
|
||||
auto detached_nodes_opt = app_node.get_child_optional("detached"s);
|
||||
auto exclude_global_prep = app_node.get_optional<bool>("exclude-global-prep-cmd"s);
|
||||
auto output = app_node.get_optional<std::string>("output"s);
|
||||
auto name = parse_env_val(this_env, app_node.get<std::string>("name"s));
|
||||
auto cmd = app_node.get_optional<std::string>("cmd"s);
|
||||
auto image_path = app_node.get_optional<std::string>("image-path"s);
|
||||
auto working_dir = app_node.get_optional<std::string>("working-dir"s);
|
||||
|
||||
std::vector<proc::cmd_t> prep_cmds;
|
||||
if(!exclude_global_prep.value_or(false)) {
|
||||
prep_cmds.reserve(config::sunshine.prep_cmds.size());
|
||||
for(auto &prep_cmd : config::sunshine.prep_cmds) {
|
||||
auto do_cmd = parse_env_val(this_env, prep_cmd.do_cmd);
|
||||
auto undo_cmd = parse_env_val(this_env, prep_cmd.undo_cmd);
|
||||
|
||||
prep_cmds.emplace_back(std::move(do_cmd), std::move(undo_cmd));
|
||||
}
|
||||
}
|
||||
|
||||
if(prep_nodes_opt) {
|
||||
auto &prep_nodes = *prep_nodes_opt;
|
||||
|
||||
prep_cmds.reserve(prep_nodes.size());
|
||||
prep_cmds.reserve(prep_cmds.size() + prep_nodes.size());
|
||||
for(auto &[_, prep_node] : prep_nodes) {
|
||||
auto do_cmd = parse_env_val(this_env, prep_node.get<std::string>("do"s));
|
||||
auto undo_cmd = prep_node.get_optional<std::string>("undo"s);
|
||||
|
||||
@@ -12,20 +12,13 @@
|
||||
|
||||
#include <boost/process.hpp>
|
||||
|
||||
#include "config.h"
|
||||
#include "utility.h"
|
||||
|
||||
namespace proc {
|
||||
using file_t = util::safe_ptr_v2<FILE, int, fclose>;
|
||||
|
||||
struct cmd_t {
|
||||
cmd_t(std::string &&do_cmd, std::string &&undo_cmd) : do_cmd(std::move(do_cmd)), undo_cmd(std::move(undo_cmd)) {}
|
||||
explicit cmd_t(std::string &&do_cmd) : do_cmd(std::move(do_cmd)) {}
|
||||
|
||||
std::string do_cmd;
|
||||
|
||||
// Executed when proc_t has finished running, meant to reverse 'do_cmd' if applicable
|
||||
std::string undo_cmd;
|
||||
};
|
||||
typedef config::prep_cmd_t cmd_t;
|
||||
/*
|
||||
* pre_cmds -- guaranteed to be executed unless any of the commands fail.
|
||||
* detached -- commands detached from Sunshine
|
||||
|
||||
Reference in New Issue
Block a user