Add pre-commit.

- Add pre-commit
- Format code.
This commit is contained in:
realaltffour 2020-04-05 13:07:25 +03:00
parent fb084466ce
commit f35201061d
No known key found for this signature in database
GPG Key ID: 05B35E2E8F56C5A6
6 changed files with 127 additions and 107 deletions

14
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,14 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/doublify/pre-commit-clang-format.git
rev: master
hooks:
- id: clang-format

View File

@ -1,36 +1,34 @@
#ifndef DATA_H
#define DATA_H
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include "json.hpp"
using nlohmann::json;
#define LOC_DB = ".db"
#define LOC_EVENTLOG = ".log"
#define LOC_SKIDID = "skid" // used as LOC_SKIDID+SKIDNUM
#define LOC_SKIDID = "skid" // used as LOC_SKIDID+SKIDNUM
/////////////// DATA CONTAINING STRUCTURES ///////////////////
struct Date {
int day = 0;
int month = 0; // 0-11
int year = 0; // + 1900 for currentyear
int day = 0;
int month = 0; // 0-11
int year = 0; // + 1900 for currentyear
};
struct Project {
std::string name = "";
std::string desc = "";
std::string uuid;
std::string uuid;
int pri = 0;
Date creationDate;
Date doneDate;
Date doneDate;
bool isDone = false;
};
@ -41,15 +39,9 @@ struct Skid {
/////////////// END OF DATA CONTAINING STRUCTURES ////////////
/////////////// EVENT HISTORY SYSTEM STRUCTURES ////////////////////
enum EventType {
Access = 0,
Creation = 1,
Modification = 2,
None = -1
};
enum EventType { Access = 0, Creation = 1, Modification = 2, None = -1 };
struct Event {
int id = -1;
@ -67,9 +59,9 @@ struct EventLog {
// Main Database that represents the skidjular directory.
struct DB {
std::map<std::string, Project> *projects;
std::vector<Skid> skids;
EventLog log;
std::vector<Skid> skids;
EventLog log;
Date creationDate;
Date lastAccessTime;
};

View File

@ -1,12 +1,11 @@
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <iostream>
#include "module.h"
#include "modules/init.h"
#include "data.h"
#include "module.h"
#include "modules/init.h"
int main(int argc, const char* argv[]) {
using namespace boost::program_options;
@ -15,60 +14,56 @@ int main(int argc, const char* argv[]) {
lmodules["init"] = new modules::init::mod;
try {
options_description general("General Options");
general.add_options()
("help", "Help Message")
("help-module", value<string>(),
"Module for help message")
("module", value<string>(), "Module to execute")
("module-args", value<string>(), "Arguments to use")
;
positional_options_description general_positional;
general_positional.add("module", 1);
general_positional.add("module-args", 10);
options_description general("General Options");
general.add_options()("help", "Help Message")(
"help-module", value<string>(), "Module for help message")(
"module", value<string>(), "Module to execute")(
"module-args", value<string>(), "Arguments to use");
positional_options_description general_positional;
general_positional.add("module", 1);
general_positional.add("module-args", 10);
variables_map vm;
store(command_line_parser(argc, argv)
.options(general).allow_unregistered()
.positional(general_positional)
.run(), vm);
notify(vm);
if (vm.count("help")) {
std::cout << general << "\n";
exit(0);
} else if (vm.count("help-module")) {
auto name = vm["help-module"].as<string>();
if (lmodules.find(name) == lmodules.end()) {
std::cout << "Module not found.\n";
exit(1);
}
auto module = lmodules[name];
std::cout << module->desc << "\n";
}
else if (vm.count("module")) {
auto name = vm["module"].as<string>();
variables_map vm;
store(command_line_parser(argc, argv)
.options(general)
.allow_unregistered()
.positional(general_positional)
.run(),
vm);
notify(vm);
if (lmodules.find(name) == lmodules.end()) {
std::cout << "Module not found.\n";
exit(1);
}
auto module = lmodules[name];
if (vm.count("module-args")) {
auto args = vm["module-args"].as<string>();
module->dispatch(args);
} else module->dispatch("");
}
else {
std::cout << general << "\n";
exit(0);
}
if (vm.count("help")) {
std::cout << general << "\n";
exit(0);
} else if (vm.count("help-module")) {
auto name = vm["help-module"].as<string>();
if (lmodules.find(name) == lmodules.end()) {
std::cout << "Module not found.\n";
exit(1);
}
auto module = lmodules[name];
std::cout << module->desc << "\n";
} else if (vm.count("module")) {
auto name = vm["module"].as<string>();
if (lmodules.find(name) == lmodules.end()) {
std::cout << "Module not found.\n";
exit(1);
}
auto module = lmodules[name];
}
catch (const error &ex) {
cerr << ex.what() << '\n';
if (vm.count("module-args")) {
auto args = vm["module-args"].as<string>();
module->dispatch(args);
} else
module->dispatch("");
} else {
std::cout << general << "\n";
exit(0);
}
} catch (const error& ex) {
cerr << ex.what() << '\n';
}
return 0;

View File

@ -1,33 +1,34 @@
#ifndef MODULE_H
#define MODULE_H
#include <string>
#include <functional>
#include <map>
#include <string>
#define MODFN(name, code) \
void name(std::string arguments) {code}
#define MODFNSIG(name) \
void name(std::string arguments);
void name(std::string arguments) { code }
#define MODFNSIG(name) void name(std::string arguments);
#define MODFNIMPL(name, code) \
void name(std::string arguments) {code}
void name(std::string arguments) { code }
#define MODDISPATCHFNSIG() \
virtual void dispatch(std::string arguments) = 0;
#define MODDISPATCHFNSIG() virtual void dispatch(std::string arguments) = 0;
#define MODDISPATCHFNIMPL(code) \
void mod::dispatch(std::string arguments) {code}
#define MODDISPATCHFN() \
void dispatch(std::string arguments) override;
void mod::dispatch(std::string arguments) { code }
#define MODDISPATCHFN() void dispatch(std::string arguments) override;
#define MODARGS(code) \
struct args {code}
#define MAKEMOD(code) \
struct mod : args, Module {code}
struct args { \
code \
}
#define MAKEMOD(code) \
struct mod : args, Module { \
code \
}
#define DEFMOD(name_, desc_) \
mod() { \
this->name = name_; \
this->desc = desc_; \
} \
mod() { \
this->name = name_; \
this->desc = desc_; \
}
using ModuleFn = std::function<void(std::string arguments)>;
using ModuleFnDispatcher = std::function<void(std::string arguments)>;

View File

@ -1,15 +1,31 @@
#include "init.h"
#include <iostream>
#include "../data.h"
namespace modules {
namespace init {
MODFNIMPL(init_create, {
std::cout << "Creating database in current directory."
<< std::endl;
});
namespace init {
MODFNIMPL(init_create, {
std::cout << "Creating database in current directory." << std::endl;
DB db;
time_t t = time(0);
struct tm *currentTime = localtime(&t);
MODDISPATCHFNIMPL({
init_create(arguments);
});
}
};
db.creationDate.day = currentTime->tm_mday;
db.creationDate.month = currentTime->tm_mon;
db.creationDate.year = currentTime->tm_year;
db.lastAccessTime.day = currentTime->tm_mday;
db.lastAccessTime.month = currentTime->tm_mon;
db.lastAccessTime.year = currentTime->tm_year;
// Mandatory stuff to stop segmantion fault due to
// pointers unintialized.
db.projects = new std::map<std::string, Project>();
db.log.events = new std::vector<Event>();
// TODO: Add event logging
writeDB(db, ".db");
});
MODDISPATCHFNIMPL({ init_create(arguments); });
} // namespace init
}; // namespace modules

View File

@ -1,16 +1,18 @@
#ifndef MODULE_INIT_H
#define MODULE_INIT_H
#include <time.h>
#include <ctime>
#include <iostream>
#include "module.h"
namespace modules {
namespace init {
MODARGS(
std::string m_location;
);
MAKEMOD(MODFNSIG(init__create);
MODARGS();
MAKEMOD(MODFNSIG(init_create);
MODDISPATCHFN();
DEFMOD("init",
DEFMOD("init",
"Creates a skidjular directory in $(pwd)");
);