Merge pull request #19 from lucaspalomodevelop/feat/config_edit

Feat/config edit
This commit is contained in:
Lucas Palomo Develop 2023-12-08 23:18:30 +01:00 committed by GitHub
commit 82200dae96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 144 additions and 5 deletions

View File

@ -31,11 +31,36 @@ void input(int argc, char *argv[])
command.addCommandAlias("remove", "r"); command.addCommandAlias("remove", "r");
command.addCommand("show", "[script] - Shows a script", showScript); command.addCommand("show", "[script] - Shows a script", showScript);
command.addCommandAlias("show", "s"); command.addCommandAlias("show", "s");
command.addCommand("config", "<command> - Configures autom", config);
command.addDefaultCommand(runScript); command.addDefaultCommand(runScript);
command.runCommand(argv[1], argc, argv); command.runCommand(argv[1], argc, argv);
} }
std::string scriptBuilder(std::string script, std::string args, json script_settings)
{
std::string builded_script = "";
builded_script = script;
if (script_settings.contains("sudo") && script_settings.at("sudo").get<bool>())
builded_script = "sudo " + script;
if (script_settings.contains("pre_script") && script_settings.at("pre_script").size() > 0)
builded_script = script_settings.at("pre_script").get<std::string>() + " && " + builded_script;
if (script_settings.contains("args") && script_settings.at("args").size() > 0)
builded_script = builded_script + " " + script_settings.at("args").get<std::string>();
if (script_settings.contains("background") && script_settings.at("background").get<bool>())
builded_script = builded_script + " &";
std::cout
<< "script: " << builded_script << std::endl;
return builded_script;
}
// run a script with is in the autom directory // run a script with is in the autom directory
void runScript(int argc, char *argv[]) void runScript(int argc, char *argv[])
{ {
@ -89,7 +114,8 @@ void runScript(int argc, char *argv[])
args += argv[i]; args += argv[i];
args += " "; args += " ";
} }
std::string script = pre_script + dir + "/" + argv[1] + " " + args; // std::string script = pre_script + dir + "/" + argv[1] + " " + args;
script = scriptBuilder(script, args, script_settings);
std::cout << "executing: " << (dir + "/" + argv[1] + " " + args) << std::endl; std::cout << "executing: " << (dir + "/" + argv[1] + " " + args) << std::endl;
// if (script_settings["sudo"]) // if (script_settings["sudo"])
@ -98,8 +124,8 @@ void runScript(int argc, char *argv[])
// if (script_settings["background"]) // if (script_settings["background"])
// script = script + " &"; // script = script + " &";
if (script_settings["pre_script"].size() > 0) // if (script_settings["pre_script"].size() > 0)
system(script_settings["pre_script"].get<std::string>().c_str()); // system(script_settings["pre_script"].get<std::string>().c_str());
system(script.c_str()); system(script.c_str());
return; return;
@ -107,6 +133,82 @@ void runScript(int argc, char *argv[])
// } // }
} }
void config(int argc, char *argv[])
{
if (argc < 1)
{
std::cout << "Usage: autom config <command>" << std::endl;
return;
}
if (std::string(argv[1]) == "show")
{
std::cout << "Settings:" << std::endl;
std::cout << settings.getSettingsAsString() << std::endl;
}
else if (std::string(argv[1]) == "edit")
{
if (argc > 2)
{
if (std::string(argv[2]) == "editor")
{
std::string editor;
if (argc > 3)
editor = argv[3];
else
{
std::cout << "Enter editor: ";
std::cin >> editor;
}
settings.value["editor"] = editor;
settings.writeSettings();
return;
}
return;
}
else
{
system((std::string(settings.value["editor"]) + " " + settings.filepath).c_str());
}
}
// if (argv[2] == "editor")
// {
// std::cout << "Enter editor: ";
// std::string editor;
// std::cin >> editor;
// settings.value["editor"] = editor;
// settings.save();
// return;
// }
// if (argv[2] == "search_dirs")
// {
// std::cout << "Enter search dirs: ";
// std::string search_dirs;
// std::cin >> search_dirs;
// settings.value["search_dirs"] = search_dirs;
// settings.save();
// return;
// }
// if (argv[2] == "scripts")
// {
// std::cout << "Enter scripts: ";
// std::string scripts;
// std::cin >> scripts;
// settings.value["scripts"] = scripts;
// settings.save();
// return;
// }
// std::cout << "Command " << argv[2] << " does not exist" << std::endl;
}
void showScript(int argc, char *argv[]) void showScript(int argc, char *argv[])
{ {

View File

@ -20,8 +20,12 @@ Command command;
// input function for parsing arguments and creating commands and running them // input function for parsing arguments and creating commands and running them
void input(int argc, char *argv[]); void input(int argc, char *argv[]);
// build a script
std::string scriptBuilder(std::string pre_script, std::string script_name, std::string args, json script_settings);
// run a script with is in the autom directory // run a script with is in the autom directory
void runScript(int argc, char *argv[]); void runScript(int argc, char *argv[]);
// config function for configuring autom
void config(int argc, char *argv[]);
// list all scripts in the autom directory // list all scripts in the autom directory
void listScripts(int argc, char *argv[]); void listScripts(int argc, char *argv[]);
// add a script in the autom directory // add a script in the autom directory

View File

@ -7,6 +7,7 @@ Setup setup;
Settings::Settings(void) Settings::Settings(void)
{ {
setup = Setup(); setup = Setup();
filepath = setup.home + "/.automconfig.json";
readSettings(); readSettings();
} }
@ -16,7 +17,7 @@ Settings::~Settings(void)
void Settings::readSettings() void Settings::readSettings()
{ {
std::ifstream file(setup.home + "/.automconfig.json"); std::ifstream file(filepath);
if (!file.is_open()) if (!file.is_open())
{ {
std::cout << "Error:" + setup.home + "/.automconfig.json not found" << std::endl; std::cout << "Error:" + setup.home + "/.automconfig.json not found" << std::endl;
@ -24,4 +25,33 @@ void Settings::readSettings()
readSettings(); readSettings();
} }
this->value = json::parse(file); this->value = json::parse(file);
} }
std::string Settings::getSettingsAsString()
{
return this->value.dump(4);
}
void Settings::writeSettings()
{
std::ofstream file(setup.home + "/.automconfig.json");
file << this->value.dump(4);
file.close();
}
// void Settings::set(std::string key, std::string value)
// {
// this->value[key] = value;
// writeSettings();
// }
// std::string Settings::get(std::string key)
// {
// return this->value[key];
// }
// void Settings::set(std::string key, int value)
// {
// this->value[key] = value;
// writeSettings();
// }

View File

@ -26,9 +26,12 @@ private:
public: public:
json value; json value;
std::string filepath;
Settings(void); Settings(void);
~Settings(void); ~Settings(void);
void readSettings(); void readSettings();
void writeSettings();
std::string getSettingsAsString();
Settings &operator=(const Settings &) = default; Settings &operator=(const Settings &) = default;
}; };