From dc471a91a6951dfb36010105ef16de00a41a4b63 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 19 Nov 2023 13:00:14 +0100 Subject: [PATCH 1/7] fix Showing script --- src/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index e1d5acc..83fe2ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -148,7 +148,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; From cb577cfdf52dcb2a11677f6d3ec9369f6a6724a8 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 19 Nov 2023 13:33:34 +0100 Subject: [PATCH 2/7] add config show command --- src/main.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.h | 2 ++ src/settings.cpp | 31 +++++++++++++++++++++++++++++- src/settings.h | 1 + 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 83fe2ea..d66bb81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,6 +31,7 @@ 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); @@ -107,6 +108,54 @@ 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; + } + + // 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[]) { diff --git a/src/main.h b/src/main.h index eba237c..f4aa53e 100644 --- a/src/main.h +++ b/src/main.h @@ -22,6 +22,8 @@ Command command; void input(int argc, char *argv[]); // 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..301dee1 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -24,4 +24,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..8f398e7 100644 --- a/src/settings.h +++ b/src/settings.h @@ -29,6 +29,7 @@ public: Settings(void); ~Settings(void); void readSettings(); + std::string getSettingsAsString(); Settings &operator=(const Settings &) = default; }; From 8c7edfc55e099d6bfd5fd4ae9ede8888c9e87bfc Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 19 Nov 2023 16:36:50 +0100 Subject: [PATCH 3/7] add -config edit editor- --- src/main.cpp | 28 ++++++++++++++++++++++++++++ src/settings.cpp | 15 ++++++++------- src/settings.h | 2 ++ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d66bb81..58d50f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -123,6 +123,34 @@ void config(int argc, char *argv[]) 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: "; diff --git a/src/settings.cpp b/src/settings.cpp index 301dee1..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; @@ -31,12 +32,12 @@ 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::writeSettings() +{ + std::ofstream file(setup.home + "/.automconfig.json"); + file << this->value.dump(4); + file.close(); +} // void Settings::set(std::string key, std::string value) // { diff --git a/src/settings.h b/src/settings.h index 8f398e7..39d6f41 100644 --- a/src/settings.h +++ b/src/settings.h @@ -26,9 +26,11 @@ private: public: json value; + std::string filepath; Settings(void); ~Settings(void); void readSettings(); + void writeSettings(); std::string getSettingsAsString(); Settings &operator=(const Settings &) = default; }; From c38ad5a890f05363f6bbaed299c5d3f22b6ed3c4 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Tue, 5 Dec 2023 21:36:06 +0100 Subject: [PATCH 4/7] add scriptBuilder function to build script command with json-config options --- src/main.cpp | 31 ++++++++++++++++++++++++++++--- src/main.h | 2 ++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 58d50f8..5b0324a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,6 +37,30 @@ void input(int argc, char *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()) + 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[]) { @@ -90,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"]) @@ -99,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; diff --git a/src/main.h b/src/main.h index f4aa53e..f30cfdb 100644 --- a/src/main.h +++ b/src/main.h @@ -20,6 +20,8 @@ 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 From 1bfbdea99f5177b2ef4612fa92c0687b030a9d42 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Thu, 7 Dec 2023 20:47:23 +0100 Subject: [PATCH 5/7] add nlohmann as submodule --- .gitmodules | 3 +++ libs/json | 1 + libs/libs/json | 1 + 3 files changed, 5 insertions(+) create mode 100644 .gitmodules create mode 160000 libs/json create mode 160000 libs/libs/json diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8d9ae47 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libs/libs/json"] + path = libs/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/libs/libs/json b/libs/libs/json new file mode 160000 index 0000000..3780b41 --- /dev/null +++ b/libs/libs/json @@ -0,0 +1 @@ +Subproject commit 3780b41dd070436f3f55327b0a88f27a52e2dfa8 From 04a37f422a4cb4d66fc190f30acdbe38897adbea Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Thu, 7 Dec 2023 20:54:56 +0100 Subject: [PATCH 6/7] revert module-folder mistake --- .gitmodules | 4 ++-- libs/libs/json | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) delete mode 160000 libs/libs/json diff --git a/.gitmodules b/.gitmodules index 8d9ae47..4b8e53f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "libs/libs/json"] - path = libs/libs/json +[submodule "libs/json"] + path = libs/json url = git@github.com:nlohmann/json.git diff --git a/libs/libs/json b/libs/libs/json deleted file mode 160000 index 3780b41..0000000 --- a/libs/libs/json +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3780b41dd070436f3f55327b0a88f27a52e2dfa8 From e4d272fc535da13cb7409ba3cf50a9a665becd24 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Fri, 8 Dec 2023 23:13:48 +0100 Subject: [PATCH 7/7] update nlohmann/json include --- src/settings.h | 2 +- src/setup.h | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/settings.h b/src/settings.h index d78ebb5..372846b 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; 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()