add 'add' 'edit' command

This commit is contained in:
lucaspalomodevelop 2023-05-06 23:51:38 +02:00
parent 21628a7320
commit b926949d69
2 changed files with 51 additions and 0 deletions

View File

@ -22,6 +22,8 @@ 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);
}
@ -48,6 +50,49 @@ void listScripts(int argc, char *argv[])
}
}
// 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[])
{

View File

@ -3,6 +3,7 @@
#include <string>
#include <sys/stat.h>
#include <filesystem>
#include <fstream>
#include "inputparser.h"
#include "command.h"
@ -24,6 +25,11 @@ void input(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 create(int argc,char *argv[]);