From 4e8c096e27fd6cd5b42c7825f72aa26c38075c51 Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Fri, 5 Jan 2024 01:36:45 +0100 Subject: [PATCH] add simple DEBUG to autom --- src/command.cpp | 19 +++++++++---- src/command.h | 2 ++ src/debug.h | 13 +++++++++ src/main.cpp | 71 +++++++++++++++++++++++++++++++++++++----------- src/main.h | 2 ++ src/settings.cpp | 14 ++++++++++ src/settings.h | 1 + src/setup.h | 15 ++++++++++ 8 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 src/debug.h diff --git a/src/command.cpp b/src/command.cpp index fb54856..56d9254 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -4,10 +4,10 @@ Command::Command() {} Command::~Command() {} - // add a command alias to the command map void Command::addCommandAlias(std::string name, std::string alias) { + DEBUG("Adding command alias: " + alias + " for command: " + name); CommandInfo mycommand{ alias, commands[name].description, @@ -18,6 +18,7 @@ void Command::addCommandAlias(std::string name, std::string alias) // add a command to the command map void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) { + DEBUG("Adding command: " + name); CommandInfo mycommand{ name, "", @@ -27,6 +28,7 @@ void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) void Command::addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) { + DEBUG("Adding command: " + name); CommandInfo mycommand{ name, description, @@ -37,31 +39,37 @@ void Command::addCommand(std::string name, std::string description, void (*func) // add a default command to the command map void Command::addDefaultCommand(void (*func)(int argc, char *argv[])) { + DEBUG("Adding default command"); defaultCommand = func; } // run a command void Command::runCommand(std::string name, int argc, char *argv[]) { - // std::cout << "Running command: " << name << std::endl; + DEBUG("Running command: " + name); if (this->isInCommands(name)) { + DEBUG("Command found: " + name); char *argv2[argc]; for (int i = 0; i < argc; i++) { + DEBUG("argv[" + std::to_string(i) + "]: " + argv[i]); argv2[i] = argv[i + 1]; } + + DEBUG("argc: " + std::to_string(argc)); commands[name].func(argc - 1, argv2); } else { + DEBUG("Command not found: " + name); + DEBUG("Running default command"); defaultCommand(argc, argv); } } - // check if a command is in the command map bool Command::isInCommands(std::string name) { @@ -69,11 +77,11 @@ bool Command::isInCommands(std::string name) { if (command.first == name) { - // std::cout << "Command found: " << command.first << std::endl; + DEBUG("Command found: " + command.first); return true; } } - // std::cout << "Command not found: " << name << std::endl; + DEBUG("Command not found: " + name); return false; } @@ -81,7 +89,6 @@ std::string Command::listCommands() { std::string list = ""; - for (std::map::iterator it = commands.begin(); it != commands.end(); ++it) { list += "\t" + it->second.name + " " + it->second.description + "\n"; diff --git a/src/command.h b/src/command.h index ed97960..034669a 100644 --- a/src/command.h +++ b/src/command.h @@ -5,6 +5,8 @@ #include #include +#include "debug.h" + struct CommandInfo { std::string name; diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..4db668b --- /dev/null +++ b/src/debug.h @@ -0,0 +1,13 @@ + +// #ifndef DEBUG_H +// #define DEBUG_H + +#define debug + +#ifdef debug // debug mode +#define DEBUG(x) std::cout << "DEBUG [ file " << __FILE__ << " | line " << __LINE__ << " ]: " << x << std::endl; +#else // release mode +#define DEBUG(x) ; +#endif + +// #endif // DEBUG_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5b0324a..5290530 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,9 +3,13 @@ int main(int argc, char *argv[]) { + DEBUG("new obj settings"); Settings &settings = Settings::Instance(); + + DEBUG("new obj command"); command = Command(); + DEBUG("get home dir from settings"); home_dir = settings.value["autom_home_dir"]; input(argc, argv); @@ -18,22 +22,52 @@ void input(int argc, char *argv[]) InputParser input(argc, argv); // std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; + DEBUG("add 'run' command"); command.addCommand("run", "[script] - Runs a script", runScript); + + DEBUG("add 'help' command"); command.addCommand("help", "- Shows this help message", help); + + DEBUG("add 'h' alias for 'help' command"); command.addCommandAlias("help", "h"); + + DEBUG("add 'ls' command"); command.addCommand("ls", "- Lists all scripts ", listScripts); + + DEBUG("add 'l' alias for 'ls' command"); command.addCommandAlias("ls", "l"); + + DEBUG("add 'add' command"); command.addCommand("add", "[script] - Adds a script", addScript); + + DEBUG("add 'a' alias for 'add' command"); command.addCommandAlias("add", "a"); + + DEBUG("add 'edit' command"); command.addCommand("edit", "[script] - Edits a script", editScript); + + DEBUG("add 'e' alias for 'edit' command"); command.addCommandAlias("edit", "e"); + + DEBUG("add 'remove' command"); command.addCommand("remove", "[script] - Remove a script", removeScript); + + DEBUG("add 'r' alias for 'remove' command"); command.addCommandAlias("remove", "r"); + + DEBUG("add 'show' command"); command.addCommand("show", "[script] - Shows a script", showScript); + + DEBUG("add 's' alias for 'show' command"); command.addCommandAlias("show", "s"); + + DEBUG("add 'config' command"); command.addCommand("config", " - Configures autom", config); + DEBUG("add default command"); command.addDefaultCommand(runScript); + + DEBUG("run command"); command.runCommand(argv[1], argc, argv); } @@ -43,21 +77,27 @@ std::string scriptBuilder(std::string script, std::string args, json script_sett std::string builded_script = ""; builded_script = script; + DEBUG("script before sudo: " << builded_script); if (script_settings.contains("sudo") && script_settings.at("sudo").get()) builded_script = "sudo " + script; + DEBUG("script before pre_script: " << builded_script); if (script_settings.contains("pre_script") && script_settings.at("pre_script").size() > 0) builded_script = script_settings.at("pre_script").get() + " && " + builded_script; + DEBUG("script before config args: " << builded_script); if (script_settings.contains("args") && script_settings.at("args").size() > 0) builded_script = builded_script + " " + script_settings.at("args").get(); + DEBUG("script before args: " << builded_script); + if (args.size() > 0) + builded_script = builded_script + " " + args; + + DEBUG("script before background: " << builded_script); if (script_settings.contains("background") && script_settings.at("background").get()) builded_script = builded_script + " &"; - std::cout - << "script: " << builded_script << std::endl; - + DEBUG("script: " << builded_script); return builded_script; } @@ -68,6 +108,7 @@ void runScript(int argc, char *argv[]) std::map dir_options; std::string dir = home_dir; + DEBUG("get script settings"); auto script_settings = settings.value["scripts"][argv[1]]; for (auto search_dir : settings.value["search_dirs"]) @@ -80,54 +121,52 @@ void runScript(int argc, char *argv[]) if (dir_options.size() == 0) { + DEBUG("script " << argv[1] << " does not exist"); std::cout << "Script " << argv[1] << " does not exist" << std::endl; return; } if (dir_options.size() == 1) { + DEBUG("script " << argv[1] << " exists in " << dir_options[0]); dir = dir_options[0]; } if (dir_options.size() > 1) { + DEBUG("script " << argv[1] << " exists in multiple directories"); std::cout << "Which script do you want to run?" << std::endl; for (auto &option : dir_options) { + DEBUG("option: " << option.first << " " << option.second); std::cout << option.first << " " << option.second << std::endl; } std::cout << "Enter number: "; int num; std::cin >> num; + DEBUG("selected option: " << num); + DEBUG("selected dir: " << dir_options[num]); dir = dir_options[num]; } - // for (auto search_dir : settings.getSetting>("search_dirs")) - // { std::string script = dir + "/" + argv[1]; if (std::filesystem::exists(script)) { + DEBUG("script exists"); std::string pre_script = "cd " + dir + " && "; std::string args = ""; for (int i = 2; i < argc; i++) { + DEBUG("argv[" << i << "]: " << argv[i]); args += argv[i]; 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"]) - // script = "sudo " + script; - - // if (script_settings["background"]) - // script = script + " &"; - - // if (script_settings["pre_script"].size() > 0) - // system(script_settings["pre_script"].get().c_str()); + DEBUG("run script"); system(script.c_str()); + DEBUG("script finished"); return; } // } diff --git a/src/main.h b/src/main.h index f30cfdb..0c047f6 100644 --- a/src/main.h +++ b/src/main.h @@ -13,6 +13,8 @@ #include "command.h" #include "settings.h" +#include "debug.h" + Settings settings; // directory for autom scripts std::string home_dir = ""; diff --git a/src/settings.cpp b/src/settings.cpp index e87570d..0df06af 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -17,25 +17,39 @@ Settings::~Settings(void) void Settings::readSettings() { + DEBUG("Reading settings"); std::ifstream file(filepath); if (!file.is_open()) { + DEBUG("Settings file not found"); std::cout << "Error:" + setup.home + "/.automconfig.json not found" << std::endl; + + DEBUG("Creating settings"); setup.createSettings(); + + DEBUG("Reading settings"); readSettings(); } + + DEBUG("Parsing settings"); this->value = json::parse(file); } std::string Settings::getSettingsAsString() { + DEBUG("Getting settings as string"); return this->value.dump(4); } void Settings::writeSettings() { + DEBUG("Writing settings"); std::ofstream file(setup.home + "/.automconfig.json"); + + DEBUG("Writing settings to file"); file << this->value.dump(4); + + DEBUG("Closing file"); file.close(); } diff --git a/src/settings.h b/src/settings.h index 6e0c544..150cce0 100644 --- a/src/settings.h +++ b/src/settings.h @@ -3,6 +3,7 @@ #ifndef SETTINGS_H #define SETTINGS_H +#include "debug.h" #include "../libs/json/single_include/nlohmann/json.hpp" using json = nlohmann::json; diff --git a/src/setup.h b/src/setup.h index 95597e3..8fb308f 100644 --- a/src/setup.h +++ b/src/setup.h @@ -9,6 +9,7 @@ #include #include +#include "debug.h" #include "../libs/json/single_include/nlohmann/json.hpp" using json = nlohmann::json; @@ -38,25 +39,33 @@ public: void createFolder() { #ifdef _WIN32 + DEBUG("creating folder on windows"); _mkdir((home).c_str()); #else + DEBUG("creating folder on linux"); mkdir((home).c_str(), 0777); #endif } void createSettings() { + DEBUG("creating settings file"); + + DEBUG("replacing backslashes with forward slashes"); std::replace(home.begin(), home.end(), '\\', '/'); std::ofstream file(home + "/.automconfig.json"); + DEBUG("creating settings file with default settings"); json j = { {"editor", editor}, {"search_dirs", {home}}, {"autom_home_dir", home}}; + DEBUG("writing settings to file"); file << j.dump(4); + DEBUG("closing file"); file.close(); } @@ -64,10 +73,16 @@ public: { if (!std::filesystem::exists(home)) + { + DEBUG("creating folder"); createFolder(); + } if (!std::filesystem::exists(home + "/.automconfig.json")) + { + DEBUG("creating settings file"); createSettings(); + } } };