Merge pull request #4 from lucaspalomodevelop/dev

Dev
This commit is contained in:
Lucas Palomo Develop 2023-05-06 23:52:25 +02:00 committed by GitHub
commit a506ad0913
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 4 deletions

View File

@ -1,5 +1,5 @@
CC = g++
CFLAGS = -std=c++11 -Wall
CFLAGS = -std=c++17 -Wall
SRCDIR = ./src
BINDIR = ./bin

View File

@ -13,7 +13,7 @@ int main(int argc, char *argv[])
return 0;
}
// input function for parsing arguments and creating commands and running them
// input function for parsing arguments and creating commands and running them
void input(int argc, char *argv[])
{
InputParser input(argc, argv);
@ -21,6 +21,9 @@ void input(int argc, char *argv[])
command.addCommand("run", runScript);
command.addCommand("help", help);
command.addCommand("ls", listScripts);
command.addCommand("add", addScript);
command.addCommand("edit", editScript);
command.addDefaultCommand(runScript);
command.runCommand(argv[1], argc, argv);
}
@ -36,6 +39,60 @@ void runScript(int argc, char *argv[])
system(script.c_str());
}
// list all scripts in the autom directory
void listScripts(int argc, char *argv[])
{
std::cout << "Scripts:" << std::endl;
for (const auto &entry : std::filesystem::directory_iterator(dir))
{
std::string name = entry.path().filename().string();
std::cout << " " << name << std::endl;
}
}
// add a script in the autom directory
void addScript(int argc, char *argv[])
{
if (std::filesystem::exists(dir + "/" + argv[1]))
{
std::cout << "Script " << argv[1] <<" already exists" << std::endl;
return;
}
std::cout << "Adding script: " << argv[1] << std::endl;
std::string script = dir + "/" + argv[1];
std::ofstream file(script);
#ifdef _WIN32
file << "@echo off" << std::endl;
_chmod(script.c_str(), _S_IREAD | _S_IWRITE);
#else
file << "#!/bin/bash" << std::endl;
system(("chmod +x " + script).c_str());
#endif
file.close();
editScript(argv[1]);
}
// edit a script in the autom directory
void editScript(int argc, char *argv[])
{
editScript(argv[1]);
}
void editScript(std::string name)
{
std::string script = dir + "/" + name;
#ifdef _WIN32
system(("notepad " + script).c_str());
#else
system(("nano " + script).c_str());
#endif
}
// help function for showing help message
void help(int argc, char *argv[])
{
@ -43,5 +100,6 @@ void help(int argc, char *argv[])
std::cout << "Commands:" << std::endl;
std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl;
std::cout << " run [script] - Runs a script" << std::endl;
std::cout << " ls - Lists all scripts" << std::endl;
std::cout << " help - Shows this help message" << std::endl;
}

View File

@ -2,6 +2,9 @@
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <filesystem>
#include <fstream>
#include "inputparser.h"
#include "command.h"
@ -19,9 +22,16 @@ std::string dir = home + "/autom";
// input function for parsing arguments and creating commands and running them
void input(int argc, char *argv[]);
// run a script with is in the autom directory
void runScript(int argc,char *argv[]);
void runScript(int argc, char *argv[]);
// list all scripts in the autom directory
void listScripts(int argc, char *argv[]);
// add a script in the autom directory
void addScript(int argc, char *argv[]);
// edit a script in the autom directory
void editScript(int argc, char *argv[]);
void editScript(std::string name);
// help function for showing help message
void help(int argc,char *argv[]);
void help(int argc, char *argv[]);
// void create(int argc,char *argv[]);
// void remove(int argc,char *argv[]);
// void list(int argc,char *argv[]);