From cb577cfdf52dcb2a11677f6d3ec9369f6a6724a8 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Sun, 19 Nov 2023 13:33:34 +0100 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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