Merge pull request #1 from lucaspalomodevelop/dev

Dev
This commit is contained in:
Lucas Palomo Develop 2023-04-13 20:11:07 +02:00 committed by GitHub
commit 9425714fd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 270 additions and 20 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
/bin
# Prerequisites
*.d

67
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,67 @@
{
"files.associations": {
"*.html": "html",
"*.jsste": "plaintext",
"string": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"compare": "cpp",
"concepts": "cpp",
"ios": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
}
}

View File

@ -1,3 +1,11 @@
all:
g++ -std=c++11 ./src/main.cpp -o ./src/autom
./src/autom test
CC = g++
CFLAGS = -std=c++11 -Wall
SRCDIR = ./src
BINDIR = ./bin
$(BINDIR)/main: $(SRCDIR)/main.cpp $(SRCDIR)/command.cpp $(SRCDIR)/inputparser.cpp
$(CC) $(CFLAGS) $^ -o $@
.PHONY: clean
clean:
rm -f $(BINDIR)/autom

BIN
src/autom

Binary file not shown.

49
src/command.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "command.h"
Command::Command() {}
Command::~Command() {}
void Command::addCommand(std::string name, void (*func)(int argc, char *argv[]))
{
commands[name] = func;
}
void Command::addDefaultCommand(void (*func)(int argc, char *argv[]))
{
defaultCommand = func;
}
void Command::runCommand(char *name, int argc, char *argv[])
{
std::cout << "Running command: " << name << std::endl;
if (this->isInCommands(name))
{
for (int i = 0; i < argc; i++)
{
argv[i] = argv[i + 1];
}
commands[name](argc, argv);
}
else
{
defaultCommand(argc, argv);
}
}
bool Command::isInCommands(char *name)
{
for (auto const &command : commands)
{
if (command.first == name)
{
std::cout << "Command found: " << command.first << std::endl;
return true;
}
}
std::cout << "Command not found: " << name << std::endl;
return false;
}

23
src/command.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef COMMAND_H
#define COMMAND_H
#include <map>
#include <string>
#include <iostream>
class Command
{
public:
Command(void);
~Command(void);
void addCommand(std::string name, void (*func)(int argc, char *argv[]));
void addDefaultCommand(void (*func)(int argc,char *argv[]));
void runCommand(char *name, int argc, char *argv[]);
bool isInCommands(char *name);
private:
std::map<std::string, void (*)(int argc,char *argv[])> commands;
void (*defaultCommand)(int argc,char *argv[]);
};
#endif // COMMAND_H

38
src/inputparser.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "./inputparser.h"
InputParser::InputParser(int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
{
std::string arg(argv[i]);
if (arg.find("-") == 0)
{
std::string key = arg.substr(1);
if (i + 1 < argc)
{
std::string value(argv[i + 1]);
if (value.find("-") != 0)
{
args[key] = value;
i++;
}
else
{
args[key] = "";
}
}
else
{
args[key] = "";
}
}
}
}
std::string InputParser::getValue(std::string key)
{
return args[key];
}
bool InputParser::hasKey(std::string key)
{
return args.count(key) > 0;
}

20
src/inputparser.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef INPUTPARSER_H
#define INPUTPARSER_H
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
class InputParser
{
std::map<std::string, std::string> args;
public:
InputParser(int argc, char *argv[]);
std::string getValue(std::string key);
bool hasKey(std::string key);
};
#endif // INPUTPARSER_H

BIN
src/main

Binary file not shown.

View File

@ -1,25 +1,43 @@
#include <iostream>
#include <string>
#include <sys/stat.h>
#include "main.h"
int main(int argc, char *argv[])
{
// make dir in /etc/autom
std::string home = getenv("HOME");
std::string dir = home + "/.autom";
// std::cout << "dir: " << dir << std::endl;
#ifdef _WIN32
_mkdir(dir.c_str());
#else
mkdir(dir.c_str(), 0777);
#endif
if (argc > 1 && argv[1][0] != '-')
{
std::string pre_script = "cd " + dir + " && ";
std::string script = pre_script + dir + "/" + argv[1] + ".sh";
system(script.c_str());
}
// else{
// std::cout << "Usage: autom <script>" << std::endl;
// }
input(argc, argv);
return 0;
}
void input(int argc, char *argv[])
{
InputParser input(argc, argv);
Command command = Command();
command.addCommand("run", runScript);
command.addCommand("help", help);
command.addDefaultCommand(runScript);
command.runCommand(argv[1],argc, argv);
}
void runScript(int argc,char *argv[])
{
std::cout << "Running script: " << argv[1] << std::endl;
std::string pre_script = "cd " + dir + " && ";
std::string script = pre_script + dir + "/" + argv[1];
std::cout << "Running script: " << script << std::endl;
system(script.c_str());
}
void help(int argc,char *argv[])
{
std::cout << "Usage: autom [command] [options]" << std::endl;
std::cout << "Commands:" << std::endl;
std::cout << " run [script] - Runs a script" << std::endl;
std::cout << " help - Shows this help message" << std::endl;
}

24
src/main.h Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <string>
#include <sys/stat.h>
#include "inputparser.h"
#include "command.h"
#ifdef _WIN32
#include <direct.h>
std::string home = getenv("USERPROFILE");
#else
std::string home = getenv("HOME");
#endif
std::string dir = home + "/autom";
void input(int argc, char *argv[]);
void runScript(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[]);
void edit(int argc,char *argv[]);
void run(int argc,char *argv[]);