add simple DEBUG to autom

This commit is contained in:
lucaspalomodevelop 2024-01-05 01:36:45 +01:00
parent 8a554311d6
commit 4e8c096e27
8 changed files with 115 additions and 22 deletions

View File

@ -4,10 +4,10 @@ Command::Command() {}
Command::~Command() {} Command::~Command() {}
// add a command alias to the command map // add a command alias to the command map
void Command::addCommandAlias(std::string name, std::string alias) void Command::addCommandAlias(std::string name, std::string alias)
{ {
DEBUG("Adding command alias: " + alias + " for command: " + name);
CommandInfo mycommand{ CommandInfo mycommand{
alias, alias,
commands[name].description, commands[name].description,
@ -18,6 +18,7 @@ void Command::addCommandAlias(std::string name, std::string alias)
// add a command to the command map // add a command to the command map
void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) void Command::addCommand(std::string name, void (*func)(int argc, char *argv[]))
{ {
DEBUG("Adding command: " + name);
CommandInfo mycommand{ CommandInfo mycommand{
name, 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[])) void Command::addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[]))
{ {
DEBUG("Adding command: " + name);
CommandInfo mycommand{ CommandInfo mycommand{
name, name,
description, description,
@ -37,31 +39,37 @@ void Command::addCommand(std::string name, std::string description, void (*func)
// add a default command to the command map // add a default command to the command map
void Command::addDefaultCommand(void (*func)(int argc, char *argv[])) void Command::addDefaultCommand(void (*func)(int argc, char *argv[]))
{ {
DEBUG("Adding default command");
defaultCommand = func; defaultCommand = func;
} }
// run a command // run a command
void Command::runCommand(std::string name, int argc, char *argv[]) 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)) if (this->isInCommands(name))
{ {
DEBUG("Command found: " + name);
char *argv2[argc]; char *argv2[argc];
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
{ {
DEBUG("argv[" + std::to_string(i) + "]: " + argv[i]);
argv2[i] = argv[i + 1]; argv2[i] = argv[i + 1];
} }
DEBUG("argc: " + std::to_string(argc));
commands[name].func(argc - 1, argv2); commands[name].func(argc - 1, argv2);
} }
else else
{ {
DEBUG("Command not found: " + name);
DEBUG("Running default command");
defaultCommand(argc, argv); defaultCommand(argc, argv);
} }
} }
// check if a command is in the command map // check if a command is in the command map
bool Command::isInCommands(std::string name) bool Command::isInCommands(std::string name)
{ {
@ -69,11 +77,11 @@ bool Command::isInCommands(std::string name)
{ {
if (command.first == name) if (command.first == name)
{ {
// std::cout << "Command found: " << command.first << std::endl; DEBUG("Command found: " + command.first);
return true; return true;
} }
} }
// std::cout << "Command not found: " << name << std::endl; DEBUG("Command not found: " + name);
return false; return false;
} }
@ -81,7 +89,6 @@ std::string Command::listCommands()
{ {
std::string list = ""; std::string list = "";
for (std::map<std::string, CommandInfo>::iterator it = commands.begin(); it != commands.end(); ++it) for (std::map<std::string, CommandInfo>::iterator it = commands.begin(); it != commands.end(); ++it)
{ {
list += "\t" + it->second.name + " " + it->second.description + "\n"; list += "\t" + it->second.name + " " + it->second.description + "\n";

View File

@ -5,6 +5,8 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include "debug.h"
struct CommandInfo struct CommandInfo
{ {
std::string name; std::string name;

13
src/debug.h Normal file
View File

@ -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

View File

@ -3,9 +3,13 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
DEBUG("new obj settings");
Settings &settings = Settings::Instance(); Settings &settings = Settings::Instance();
DEBUG("new obj command");
command = Command(); command = Command();
DEBUG("get home dir from settings");
home_dir = settings.value["autom_home_dir"]; home_dir = settings.value["autom_home_dir"];
input(argc, argv); input(argc, argv);
@ -18,22 +22,52 @@ void input(int argc, char *argv[])
InputParser input(argc, argv); InputParser input(argc, argv);
// std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; // 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); command.addCommand("run", "[script] - Runs a script", runScript);
DEBUG("add 'help' command");
command.addCommand("help", "- Shows this help message", help); command.addCommand("help", "- Shows this help message", help);
DEBUG("add 'h' alias for 'help' command");
command.addCommandAlias("help", "h"); command.addCommandAlias("help", "h");
DEBUG("add 'ls' command");
command.addCommand("ls", "- Lists all scripts ", listScripts); command.addCommand("ls", "- Lists all scripts ", listScripts);
DEBUG("add 'l' alias for 'ls' command");
command.addCommandAlias("ls", "l"); command.addCommandAlias("ls", "l");
DEBUG("add 'add' command");
command.addCommand("add", "[script] - Adds a script", addScript); command.addCommand("add", "[script] - Adds a script", addScript);
DEBUG("add 'a' alias for 'add' command");
command.addCommandAlias("add", "a"); command.addCommandAlias("add", "a");
DEBUG("add 'edit' command");
command.addCommand("edit", "[script] - Edits a script", editScript); command.addCommand("edit", "[script] - Edits a script", editScript);
DEBUG("add 'e' alias for 'edit' command");
command.addCommandAlias("edit", "e"); command.addCommandAlias("edit", "e");
DEBUG("add 'remove' command");
command.addCommand("remove", "[script] - Remove a script", removeScript); command.addCommand("remove", "[script] - Remove a script", removeScript);
DEBUG("add 'r' alias for 'remove' command");
command.addCommandAlias("remove", "r"); command.addCommandAlias("remove", "r");
DEBUG("add 'show' command");
command.addCommand("show", "[script] - Shows a script", showScript); command.addCommand("show", "[script] - Shows a script", showScript);
DEBUG("add 's' alias for 'show' command");
command.addCommandAlias("show", "s"); command.addCommandAlias("show", "s");
DEBUG("add 'config' command");
command.addCommand("config", "<command> - Configures autom", config); command.addCommand("config", "<command> - Configures autom", config);
DEBUG("add default command");
command.addDefaultCommand(runScript); command.addDefaultCommand(runScript);
DEBUG("run command");
command.runCommand(argv[1], argc, argv); 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 = ""; std::string builded_script = "";
builded_script = script; builded_script = script;
DEBUG("script before sudo: " << builded_script);
if (script_settings.contains("sudo") && script_settings.at("sudo").get<bool>()) if (script_settings.contains("sudo") && script_settings.at("sudo").get<bool>())
builded_script = "sudo " + script; builded_script = "sudo " + script;
DEBUG("script before pre_script: " << builded_script);
if (script_settings.contains("pre_script") && script_settings.at("pre_script").size() > 0) 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; builded_script = script_settings.at("pre_script").get<std::string>() + " && " + builded_script;
DEBUG("script before config args: " << builded_script);
if (script_settings.contains("args") && script_settings.at("args").size() > 0) if (script_settings.contains("args") && script_settings.at("args").size() > 0)
builded_script = builded_script + " " + script_settings.at("args").get<std::string>(); builded_script = builded_script + " " + script_settings.at("args").get<std::string>();
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<bool>()) if (script_settings.contains("background") && script_settings.at("background").get<bool>())
builded_script = builded_script + " &"; builded_script = builded_script + " &";
std::cout DEBUG("script: " << builded_script);
<< "script: " << builded_script << std::endl;
return builded_script; return builded_script;
} }
@ -68,6 +108,7 @@ void runScript(int argc, char *argv[])
std::map<int, std::string> dir_options; std::map<int, std::string> dir_options;
std::string dir = home_dir; std::string dir = home_dir;
DEBUG("get script settings");
auto script_settings = settings.value["scripts"][argv[1]]; auto script_settings = settings.value["scripts"][argv[1]];
for (auto search_dir : settings.value["search_dirs"]) for (auto search_dir : settings.value["search_dirs"])
@ -80,54 +121,52 @@ void runScript(int argc, char *argv[])
if (dir_options.size() == 0) if (dir_options.size() == 0)
{ {
DEBUG("script " << argv[1] << " does not exist");
std::cout << "Script " << argv[1] << " does not exist" << std::endl; std::cout << "Script " << argv[1] << " does not exist" << std::endl;
return; return;
} }
if (dir_options.size() == 1) if (dir_options.size() == 1)
{ {
DEBUG("script " << argv[1] << " exists in " << dir_options[0]);
dir = dir_options[0]; dir = dir_options[0];
} }
if (dir_options.size() > 1) if (dir_options.size() > 1)
{ {
DEBUG("script " << argv[1] << " exists in multiple directories");
std::cout << "Which script do you want to run?" << std::endl; std::cout << "Which script do you want to run?" << std::endl;
for (auto &option : dir_options) for (auto &option : dir_options)
{ {
DEBUG("option: " << option.first << " " << option.second);
std::cout << option.first << " " << option.second << std::endl; std::cout << option.first << " " << option.second << std::endl;
} }
std::cout << "Enter number: "; std::cout << "Enter number: ";
int num; int num;
std::cin >> num; std::cin >> num;
DEBUG("selected option: " << num);
DEBUG("selected dir: " << dir_options[num]);
dir = dir_options[num]; dir = dir_options[num];
} }
// for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs"))
// {
std::string script = dir + "/" + argv[1]; std::string script = dir + "/" + argv[1];
if (std::filesystem::exists(script)) if (std::filesystem::exists(script))
{ {
DEBUG("script exists");
std::string pre_script = "cd " + dir + " && "; std::string pre_script = "cd " + dir + " && ";
std::string args = ""; std::string args = "";
for (int i = 2; i < argc; i++) for (int i = 2; i < argc; i++)
{ {
DEBUG("argv[" << i << "]: " << argv[i]);
args += argv[i]; args += argv[i];
args += " "; args += " ";
} }
// std::string script = pre_script + dir + "/" + argv[1] + " " + args;
script = scriptBuilder(script, args, script_settings); 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<std::string>().c_str());
DEBUG("run script");
system(script.c_str()); system(script.c_str());
DEBUG("script finished");
return; return;
} }
// } // }

View File

@ -13,6 +13,8 @@
#include "command.h" #include "command.h"
#include "settings.h" #include "settings.h"
#include "debug.h"
Settings settings; Settings settings;
// directory for autom scripts // directory for autom scripts
std::string home_dir = ""; std::string home_dir = "";

View File

@ -17,25 +17,39 @@ Settings::~Settings(void)
void Settings::readSettings() void Settings::readSettings()
{ {
DEBUG("Reading settings");
std::ifstream file(filepath); std::ifstream file(filepath);
if (!file.is_open()) if (!file.is_open())
{ {
DEBUG("Settings file not found");
std::cout << "Error:" + setup.home + "/.automconfig.json not found" << std::endl; std::cout << "Error:" + setup.home + "/.automconfig.json not found" << std::endl;
DEBUG("Creating settings");
setup.createSettings(); setup.createSettings();
DEBUG("Reading settings");
readSettings(); readSettings();
} }
DEBUG("Parsing settings");
this->value = json::parse(file); this->value = json::parse(file);
} }
std::string Settings::getSettingsAsString() std::string Settings::getSettingsAsString()
{ {
DEBUG("Getting settings as string");
return this->value.dump(4); return this->value.dump(4);
} }
void Settings::writeSettings() void Settings::writeSettings()
{ {
DEBUG("Writing settings");
std::ofstream file(setup.home + "/.automconfig.json"); std::ofstream file(setup.home + "/.automconfig.json");
DEBUG("Writing settings to file");
file << this->value.dump(4); file << this->value.dump(4);
DEBUG("Closing file");
file.close(); file.close();
} }

View File

@ -3,6 +3,7 @@
#ifndef SETTINGS_H #ifndef SETTINGS_H
#define SETTINGS_H #define SETTINGS_H
#include "debug.h"
#include "../libs/json/single_include/nlohmann/json.hpp" #include "../libs/json/single_include/nlohmann/json.hpp"
using json = nlohmann::json; using json = nlohmann::json;

View File

@ -9,6 +9,7 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include "debug.h"
#include "../libs/json/single_include/nlohmann/json.hpp" #include "../libs/json/single_include/nlohmann/json.hpp"
using json = nlohmann::json; using json = nlohmann::json;
@ -38,25 +39,33 @@ public:
void createFolder() void createFolder()
{ {
#ifdef _WIN32 #ifdef _WIN32
DEBUG("creating folder on windows");
_mkdir((home).c_str()); _mkdir((home).c_str());
#else #else
DEBUG("creating folder on linux");
mkdir((home).c_str(), 0777); mkdir((home).c_str(), 0777);
#endif #endif
} }
void createSettings() void createSettings()
{ {
DEBUG("creating settings file");
DEBUG("replacing backslashes with forward slashes");
std::replace(home.begin(), home.end(), '\\', '/'); std::replace(home.begin(), home.end(), '\\', '/');
std::ofstream file(home + "/.automconfig.json"); std::ofstream file(home + "/.automconfig.json");
DEBUG("creating settings file with default settings");
json j = { json j = {
{"editor", editor}, {"editor", editor},
{"search_dirs", {home}}, {"search_dirs", {home}},
{"autom_home_dir", home}}; {"autom_home_dir", home}};
DEBUG("writing settings to file");
file << j.dump(4); file << j.dump(4);
DEBUG("closing file");
file.close(); file.close();
} }
@ -64,11 +73,17 @@ public:
{ {
if (!std::filesystem::exists(home)) if (!std::filesystem::exists(home))
{
DEBUG("creating folder");
createFolder(); createFolder();
}
if (!std::filesystem::exists(home + "/.automconfig.json")) if (!std::filesystem::exists(home + "/.automconfig.json"))
{
DEBUG("creating settings file");
createSettings(); createSettings();
} }
}
}; };
#endif // SETUP_H #endif // SETUP_H