mirror of
https://github.com/lucaspalomodevelop/autom.git
synced 2026-03-12 23:27:21 +00:00
add first PoC to execute scripts with settings inside
This commit is contained in:
parent
8a554311d6
commit
b52771e53c
121
src/main.cpp
121
src/main.cpp
@ -61,6 +61,113 @@ std::string scriptBuilder(std::string script, std::string args, json script_sett
|
||||
return builded_script;
|
||||
}
|
||||
|
||||
automScript parse(const std::string filecontent)
|
||||
{
|
||||
std::stringstream file(filecontent);
|
||||
|
||||
std::string line, content, script;
|
||||
int braceCount = 0;
|
||||
bool isSettings = true;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
for (char c : line)
|
||||
{
|
||||
if (c == '{')
|
||||
braceCount++;
|
||||
if (c == '}')
|
||||
braceCount--;
|
||||
if (braceCount == 0 && isSettings)
|
||||
{
|
||||
content += c;
|
||||
isSettings = false;
|
||||
continue;
|
||||
}
|
||||
if (isSettings)
|
||||
{
|
||||
content += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
script += c;
|
||||
}
|
||||
}
|
||||
content += '\n';
|
||||
if (!isSettings)
|
||||
{
|
||||
script += '\n';
|
||||
}
|
||||
}
|
||||
|
||||
automScript result;
|
||||
try
|
||||
{
|
||||
result.settings = json::parse(content);
|
||||
}
|
||||
catch (json::parse_error &e)
|
||||
{
|
||||
result.settings = json::parse("{}");
|
||||
}
|
||||
result.script = script;
|
||||
|
||||
std::cout << "settings: " << result.settings << std::endl;
|
||||
std::cout << "script: " << result.script << std::endl;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
automScript parseAndWriteBack(std::string script)
|
||||
{
|
||||
std::ifstream file(script);
|
||||
std::string filecontent;
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
filecontent += line + "\n";
|
||||
}
|
||||
file.close();
|
||||
|
||||
automScript parsed = parse(filecontent);
|
||||
|
||||
std::ofstream file2(script);
|
||||
file2 << parsed.script;
|
||||
file2.close();
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
std::string saveScriptInTemp(std::string script)
|
||||
{
|
||||
std::string temp_file = settings.value["temp_dir"].get<std::string>() + "/autom_script";
|
||||
std::ofstream file(temp_file);
|
||||
|
||||
std::string script_content = "";
|
||||
std::ifstream file2(script);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(file2, line))
|
||||
{
|
||||
script_content += line + "\n";
|
||||
}
|
||||
|
||||
file << script_content;
|
||||
file.close();
|
||||
|
||||
std::filesystem::permissions(temp_file, std::filesystem::perms::owner_all | std::filesystem::perms::group_all | std::filesystem::perms::others_all);
|
||||
|
||||
return temp_file;
|
||||
}
|
||||
|
||||
json mergeJson(json a, json b)
|
||||
{
|
||||
json result = a;
|
||||
for (auto &el : b.items())
|
||||
{
|
||||
std::cout << "key: " << el.key() << " value: " << el.value() << std::endl;
|
||||
result[el.key()] = el.value();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// run a script with is in the autom directory
|
||||
void runScript(int argc, char *argv[])
|
||||
{
|
||||
@ -109,14 +216,26 @@ void runScript(int argc, char *argv[])
|
||||
{
|
||||
std::string pre_script = "cd " + dir + " && ";
|
||||
std::string args = "";
|
||||
std::string original_script = script;
|
||||
|
||||
std::cout << "original script: " << original_script << std::endl;
|
||||
|
||||
for (int i = 2; i < argc; i++)
|
||||
{
|
||||
args += argv[i];
|
||||
args += " ";
|
||||
}
|
||||
// std::string script = pre_script + dir + "/" + argv[1] + " " + args;
|
||||
|
||||
script = saveScriptInTemp(script);
|
||||
|
||||
json json_settings = parseAndWriteBack(script).settings;
|
||||
|
||||
script_settings = mergeJson(script_settings, json_settings);
|
||||
|
||||
script = scriptBuilder(script, args, script_settings);
|
||||
std::cout << "executing: " << (dir + "/" + argv[1] + " " + args) << std::endl;
|
||||
|
||||
std::cout << "executing: " << (script + " " + args) << std::endl;
|
||||
|
||||
// if (script_settings["sudo"])
|
||||
// script = "sudo " + script;
|
||||
|
||||
@ -8,6 +8,9 @@
|
||||
#include <sys/stat.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include "../libs/json/single_include/nlohmann/json.hpp"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
#include "inputparser.h"
|
||||
#include "command.h"
|
||||
@ -18,6 +21,12 @@ Settings settings;
|
||||
std::string home_dir = "";
|
||||
Command command;
|
||||
|
||||
struct automScript
|
||||
{
|
||||
json settings;
|
||||
std::string script;
|
||||
};
|
||||
|
||||
// input function for parsing arguments and creating commands and running them
|
||||
void input(int argc, char *argv[]);
|
||||
// build a script
|
||||
|
||||
25
src/setup.h
25
src/setup.h
@ -8,6 +8,8 @@
|
||||
#include <sys/stat.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include "../libs/json/single_include/nlohmann/json.hpp"
|
||||
using json = nlohmann::json;
|
||||
@ -24,10 +26,14 @@ public:
|
||||
std::string home = getenv("HOME");
|
||||
std::string editor = "vim";
|
||||
#endif
|
||||
|
||||
std::vector<std::string> folders_to_create;
|
||||
|
||||
Setup(/* args */)
|
||||
{
|
||||
home = home + "/.autom";
|
||||
|
||||
folders_to_create = {home, home + "/temp"};
|
||||
runSetup();
|
||||
}
|
||||
|
||||
@ -38,9 +44,19 @@ public:
|
||||
void createFolder()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
_mkdir((home).c_str());
|
||||
for (std::string folder : folders_to_create)
|
||||
{
|
||||
if (!std::filesystem::exists(folder))
|
||||
_mkdir(folder.c_str());
|
||||
}
|
||||
#else
|
||||
mkdir((home).c_str(), 0777);
|
||||
for (std::string folder : folders_to_create)
|
||||
{
|
||||
if (!std::filesystem::exists(folder))
|
||||
{
|
||||
mkdir(folder.c_str(), 0777);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -53,6 +69,7 @@ public:
|
||||
json j = {
|
||||
{"editor", editor},
|
||||
{"search_dirs", {home}},
|
||||
{"temp_dir", home + "/temp"},
|
||||
{"autom_home_dir", home}};
|
||||
|
||||
file << j.dump(4);
|
||||
@ -62,9 +79,7 @@ public:
|
||||
|
||||
void runSetup()
|
||||
{
|
||||
|
||||
if (!std::filesystem::exists(home))
|
||||
createFolder();
|
||||
createFolder();
|
||||
|
||||
if (!std::filesystem::exists(home + "/.automconfig.json"))
|
||||
createSettings();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user