diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..4b8e53f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libs/json"] + path = libs/json + url = git@github.com:nlohmann/json.git diff --git a/libs/json b/libs/json new file mode 160000 index 0000000..3780b41 --- /dev/null +++ b/libs/json @@ -0,0 +1 @@ +Subproject commit 3780b41dd070436f3f55327b0a88f27a52e2dfa8 diff --git a/src/main.cpp b/src/main.cpp index e1d5acc..5b0324a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,11 +31,36 @@ void input(int argc, char *argv[]) command.addCommandAlias("remove", "r"); command.addCommand("show", "[script] - Shows a script", showScript); command.addCommandAlias("show", "s"); + command.addCommand("config", " - Configures autom", config); command.addDefaultCommand(runScript); 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()) + 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() + " && " + builded_script; + + if (script_settings.contains("args") && script_settings.at("args").size() > 0) + builded_script = builded_script + " " + script_settings.at("args").get(); + + if (script_settings.contains("background") && script_settings.at("background").get()) + builded_script = builded_script + " &"; + + std::cout + << "script: " << builded_script << std::endl; + + return builded_script; +} + // run a script with is in the autom directory void runScript(int argc, char *argv[]) { @@ -89,7 +114,8 @@ void runScript(int argc, char *argv[]) args += argv[i]; 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; // if (script_settings["sudo"]) @@ -98,8 +124,8 @@ void runScript(int argc, char *argv[]) // if (script_settings["background"]) // script = script + " &"; - if (script_settings["pre_script"].size() > 0) - system(script_settings["pre_script"].get().c_str()); + // if (script_settings["pre_script"].size() > 0) + // system(script_settings["pre_script"].get().c_str()); system(script.c_str()); 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 " << 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[]) { @@ -148,7 +250,6 @@ void showScript(int argc, char *argv[]) std::cout << "Showing script: " << script << std::endl; if (std::filesystem::exists(script)) { - std::cout << "Showing script: " << argv[1] << std::endl; std::ifstream file(script); std::string line; int line_number = 0; diff --git a/src/main.h b/src/main.h index eba237c..f30cfdb 100644 --- a/src/main.h +++ b/src/main.h @@ -20,8 +20,12 @@ Command command; // input function for parsing arguments and creating commands and running them 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 void runScript(int argc, char *argv[]); +// config function for configuring autom +void config(int argc, char *argv[]); // list all scripts in the autom directory void listScripts(int argc, char *argv[]); // add a script in the autom directory diff --git a/src/settings.cpp b/src/settings.cpp index 83e8113..e87570d 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -7,6 +7,7 @@ Setup setup; Settings::Settings(void) { setup = Setup(); + filepath = setup.home + "/.automconfig.json"; readSettings(); } @@ -16,7 +17,7 @@ Settings::~Settings(void) void Settings::readSettings() { - std::ifstream file(setup.home + "/.automconfig.json"); + std::ifstream file(filepath); if (!file.is_open()) { std::cout << "Error:" + setup.home + "/.automconfig.json not found" << std::endl; @@ -24,4 +25,33 @@ void Settings::readSettings() readSettings(); } this->value = json::parse(file); -} \ No newline at end of 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(); +// } diff --git a/src/settings.h b/src/settings.h index d78ebb5..6e0c544 100644 --- a/src/settings.h +++ b/src/settings.h @@ -3,7 +3,7 @@ #ifndef SETTINGS_H #define SETTINGS_H -#include +#include "../libs/json/single_include/nlohmann/json.hpp" using json = nlohmann::json; @@ -26,9 +26,12 @@ private: public: json value; + std::string filepath; Settings(void); ~Settings(void); void readSettings(); + void writeSettings(); + std::string getSettingsAsString(); Settings &operator=(const Settings &) = default; }; diff --git a/src/setup.h b/src/setup.h index eb5a789..95597e3 100644 --- a/src/setup.h +++ b/src/setup.h @@ -9,7 +9,7 @@ #include #include -#include +#include "../libs/json/single_include/nlohmann/json.hpp" using json = nlohmann::json; class Setup @@ -48,19 +48,16 @@ public: { std::replace(home.begin(), home.end(), '\\', '/'); - std::ofstream file(home + "/.automconfig.json"); - json j = { + json j = { {"editor", editor}, {"search_dirs", {home}}, - {"autom_home_dir", home} - }; + {"autom_home_dir", home}}; file << j.dump(4); file.close(); - } void runSetup()