Merge pull request #15 from lucaspalomodevelop/dev

Dev
This commit is contained in:
Lucas Palomo Develop 2023-08-06 20:40:56 +02:00 committed by GitHub
commit 2922dafea4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 26 deletions

View File

@ -9,3 +9,6 @@ $(BINDIR)/autom: $(SRCDIR)/main.cpp $(SRCDIR)/command.cpp $(SRCDIR)/inputparser.
.PHONY: clean .PHONY: clean
clean: clean:
rm -f $(BINDIR)/autom rm -f $(BINDIR)/autom
install:
cp $(BINDIR)/autom /usr/local/bin/autom

View File

@ -4,6 +4,17 @@ Command::Command() {}
Command::~Command() {} Command::~Command() {}
// add a command alias to the command map
void Command::addCommandAlias(std::string name, std::string alias)
{
CommandInfo mycommand{
alias,
commands[name].description,
commands[name].func};
commands[alias] = mycommand;
}
// 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[]))
{ {

View File

@ -5,7 +5,6 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
struct CommandInfo struct CommandInfo
{ {
std::string name; std::string name;
@ -19,6 +18,8 @@ public:
Command(void); Command(void);
~Command(void); ~Command(void);
// add a command alias to the command map
void addCommandAlias(std::string name, std::string alias);
// add a command to the command map // add a command to the command map
void addCommand(std::string name, void (*func)(int argc, char *argv[])); void addCommand(std::string name, void (*func)(int argc, char *argv[]));
void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])); void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[]));

View File

@ -6,7 +6,7 @@ int main(int argc, char *argv[])
Settings &settings = Settings::Instance(); Settings &settings = Settings::Instance();
command = Command(); command = Command();
dir = settings.getSetting<std::string>("autom_home_dir"); home_dir = settings.getSetting<std::string>("autom_home_dir");
input(argc, argv); input(argc, argv);
return 0; return 0;
@ -20,13 +20,17 @@ void input(int argc, char *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;
command.addCommand("run", "[script] - Runs a script", runScript); command.addCommand("run", "[script] - Runs a script", runScript);
command.addCommand("help", "- Shows this help message", help); command.addCommand("help", "- Shows this help message", help);
command.addCommandAlias("help", "h");
command.addCommand("ls", "- Lists all scripts ", listScripts); command.addCommand("ls", "- Lists all scripts ", listScripts);
// command.addCommand("config", "open configure dialog", removeScript); command.addCommandAlias("ls", "l");
command.addCommand("add", "[script] - Adds a script", addScript); command.addCommand("add", "[script] - Adds a script", addScript);
command.addCommand("new", "[script] - Adds a script", addScript); command.addCommandAlias("add", "a");
command.addCommand("edit", "[script] - Edits a script", editScript); command.addCommand("edit", "[script] - Edits a script", editScript);
command.addCommandAlias("edit", "e");
command.addCommand("remove", "[script] - Remove a script", removeScript); command.addCommand("remove", "[script] - Remove a script", removeScript);
command.addCommandAlias("remove", "r");
command.addCommand("show", "[script] - Shows a script", showScript); command.addCommand("show", "[script] - Shows a script", showScript);
command.addCommandAlias("show", "s");
command.addDefaultCommand(runScript); command.addDefaultCommand(runScript);
command.runCommand(argv[1], argc, argv); command.runCommand(argv[1], argc, argv);
} }
@ -35,32 +39,66 @@ void input(int argc, char *argv[])
void runScript(int argc, char *argv[]) void runScript(int argc, char *argv[])
{ {
// std::cout << "Running script: " << argv[1] << std::endl; std::map<int, std::string> dir_options;
std::string dir = home_dir;
for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs")) for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs"))
{ {
std::string script = search_dir + "/" + argv[1]; if (std::filesystem::exists(search_dir + "/" + argv[1]))
{
dir_options[dir_options.size()] = search_dir;
}
}
if (dir_options.size() == 0)
{
std::cout << "Script " << argv[1] << " does not exist" << std::endl;
return;
}
if (dir_options.size() == 1)
{
dir = dir_options[0];
}
if (dir_options.size() > 1)
{
std::cout << "Which script do you want to run?" << std::endl;
for (auto &option : dir_options)
{
std::cout << option.first << " " << option.second << std::endl;
}
std::cout << "Enter number: ";
int num;
std::cin >> num;
dir = dir_options[num];
}
// for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs"))
// {
std::string script = dir + "/" + argv[1];
if (std::filesystem::exists(script)) if (std::filesystem::exists(script))
{ {
std::string pre_script = "cd " + search_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++)
{ {
args += argv[i]; args += argv[i];
args += " "; args += " ";
} }
std::string script = pre_script + search_dir + "/" + argv[1] + " " + args; std::string script = pre_script + dir + "/" + argv[1] + " " + args;
std::cout << "executing: " << (search_dir + "/" + argv[1] + " " + args) << std::endl; std::cout << "executing: " << (dir + "/" + argv[1] + " " + args) << std::endl;
system(script.c_str()); system(script.c_str());
return; return;
} }
} // }
} }
void showScript(int argc, char *argv[]) void showScript(int argc, char *argv[])
{ {
std::map<int, std::string> dir_options; std::map<int, std::string> dir_options;
std::string dir = "";
for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs")) for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs"))
{ {
if (std::filesystem::exists(search_dir + "/" + argv[1])) if (std::filesystem::exists(search_dir + "/" + argv[1]))
@ -137,7 +175,7 @@ void listScripts(int argc, char *argv[])
// add a script in the autom directory // add a script in the autom directory
void addScript(int argc, char *argv[]) void addScript(int argc, char *argv[])
{ {
std::string add_dir = dir; std::string dir = home_dir;
if (settings.getSetting<std::vector<std::string>>("search_dirs").size() > 1) if (settings.getSetting<std::vector<std::string>>("search_dirs").size() > 1)
{ {
@ -150,17 +188,17 @@ void addScript(int argc, char *argv[])
std::cout << "Enter number: "; std::cout << "Enter number: ";
int num; int num;
std::cin >> num; std::cin >> num;
add_dir = search_dirs[num]; dir = search_dirs[num];
} }
if (std::filesystem::exists(add_dir + "/" + argv[1])) if (std::filesystem::exists(dir + "/" + argv[1]))
{ {
std::cout << "Script " << argv[1] << " in folder " << add_dir << " already exists" << std::endl; std::cout << "Script " << argv[1] << " in folder " << dir << " already exists" << std::endl;
return; return;
} }
std::cout << "Adding script: " << argv[1] << std::endl; std::cout << "Adding script: " << argv[1] << std::endl;
std::string script = add_dir + "/" + argv[1]; std::string script = dir + "/" + argv[1];
std::ofstream file(script); std::ofstream file(script);
#ifdef _WIN32 #ifdef _WIN32
@ -173,7 +211,7 @@ void addScript(int argc, char *argv[])
file.close(); file.close();
editScript(argv[1], add_dir); editScript(argv[1], dir);
} }
// edit a script in the autom directory // edit a script in the autom directory
@ -181,6 +219,7 @@ void editScript(int argc, char *argv[])
{ {
std::map<int, std::string> dir_options; std::map<int, std::string> dir_options;
std::string dir = home_dir;
for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs")) for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs"))
{ {
@ -227,8 +266,8 @@ void editScript(std::string name, std::string dir)
void removeScript(int argc, char *argv[]) void removeScript(int argc, char *argv[])
{ {
std::map<int, std::string> dir_options; std::map<int, std::string> dir_options;
std::string dir = home_dir;
for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs")) for (auto search_dir : settings.getSetting<std::vector<std::string>>("search_dirs"))
{ {

View File

@ -16,7 +16,7 @@
Settings settings; Settings settings;
// directory for autom scripts // directory for autom scripts
std::string dir = ""; std::string home_dir = "";
Command command; Command command;
// input function for parsing arguments and creating commands and running them // input function for parsing arguments and creating commands and running them