Compare commits

...

4 commits

Author SHA1 Message Date
7714bc498e
0.0.3 2025-05-24 01:39:20 +03:00
66d64a9642
Добавлен модуль ядра программы 2025-05-24 01:38:52 +03:00
ff9dd8757d
Добавлены параметры конфигурационного файла
- user - имя пользователя git для фиксации коммитов
На перспективу:
- presnap - набор файлов (или команд?!) для выполнения до архивации (коммита) отслеживаемых файлов
- postsnap - набор файлов (или команд?!) для выполнения после архивации (коммита) отслеживаемых файлов
2025-05-24 01:34:32 +03:00
cf85cc3c77
Добавлен новый параметр user для конфигурации git 2025-05-24 01:33:42 +03:00
7 changed files with 161 additions and 6 deletions

View file

@ -1,5 +1,14 @@
{ {
"git": "/var/lib/snapd", "git": "/tmp/testgit",
"project": "/", "project": "/home/alexander/Programming/new/dlang/snapd/source",
"email": "user@site.domain" "email": "user@site.domain",
"user": "snapd",
"presnap": [
"/usr/bin/ls",
"/usr/local/bin/script.sh"
],
"postsnap": [
"/usr/bin/ls",
"/usr/local/bin/script.sh"
]
} }

View file

@ -2,8 +2,6 @@ import snapd;
import commandr; import commandr;
import std.file; import std.file;
import std.stdio : writeln;
import core.stdc.stdlib : EXIT_SUCCESS, EXIT_FAILURE; import core.stdc.stdlib : EXIT_SUCCESS, EXIT_FAILURE;
private string programName = "snapd"; private string programName = "snapd";
@ -11,6 +9,9 @@ private string programName = "snapd";
int main(string[] args) int main(string[] args)
{ {
auto argumets = new Program(programName, snapdVersion) auto argumets = new Program(programName, snapdVersion)
.add(new Command("init", "Initializing the repository for storing snapshots"))
.add(new Command("status", "Checking the status of tracked files"))
.add(new Command("create", "Create a new backup"))
.add(new Option("c", "config", "Сonfiguration file path") .add(new Option("c", "config", "Сonfiguration file path")
.optional .optional
.validateEachWith( .validateEachWith(
@ -31,5 +32,23 @@ int main(string[] args)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
auto snapd = new Snapd(config);
try {
argumets
.on("init", (init) {
snapd.initialize();
})
.on("status", (status) {
snapd.status();
})
.on("create", (create) {
snapd.create();
});
} catch (SnapdException e) {
e.print();
return EXIT_FAILURE;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View file

@ -12,6 +12,7 @@ class SnapdConfig {
private string _git; private string _git;
private string _project; private string _project;
private string _email; private string _email;
private string _user;
private bool isValidEmail(string email) { private bool isValidEmail(string email) {
auto emailPattern = ctRegex!r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; auto emailPattern = ctRegex!r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
@ -85,9 +86,22 @@ class SnapdConfig {
"Invalid email address provided in the \"email\" parameter:\n\t" "Invalid email address provided in the \"email\" parameter:\n\t"
~ _email ~ _email
); );
if ("user" !in jsonData)
throw new SnapdConfigException(
"The configuration file is missing the \"user\" parameter"
);
_user = jsonData["user"].str;
if (!_user.length)
throw new SnapdConfigException(
"The \"user\" parameter must contain an user name"
);
} }
@property string git() const { return _git; } @property string git() const { return _git; }
@property string project() const { return _project; } @property string project() const { return _project; }
@property string email() const { return _email; } @property string email() const { return _email; }
@property string user() const { return _user; }
} }

109
source/snapd/core/core.d Normal file
View file

@ -0,0 +1,109 @@
module snapd.core.core;
import snapd.config;
import std.format;
import std.stdio;
import std.array;
import std.exception;
import std.process;
import std.algorithm;
import std.string;
class SnapdException : Exception {
this(string msg, string file = __FILE__, size_t line = __LINE__) {
super(msg, file, line);
}
void print() {
writeln(msg);
}
}
class Snapd {
private string[] _baseCommand;
private SnapdConfig _config;
this(SnapdConfig config) {
_baseCommand = format(
"git --git-dir=%s --work-tree=%s",
config.git, config.project
).split();
_config = config;
}
void initialize() {
auto result = execute(_baseCommand ~ "init");
if (result.status)
throw new SnapdException(
"A Git repository initialization error occurred:\n"
~ result.output
);
result = execute(
_baseCommand ~ ["config", "user.email", _config.email]
);
if (result.status)
throw new SnapdException(
"A Git repository initialization error occurred:\n"
~ result.output
);
result = execute(
_baseCommand ~ ["config", "user.name", _config.user]
);
if (result.status)
throw new SnapdException(
"A Git repository initialization error occurred:\n"
~ result.output
);
writeln(
"The Git repository has been initialized successfully: "
~ _config.git
);
}
void status() {
auto result = execute(
_baseCommand ~ ["status", "--porcelain"]
);
if (result.status)
throw new SnapdException(
"An error occurred while checking the file tracking status:\n"
~ result.output
);
writeln("The following list of files requires backup:");
/**
Цепочка выполняет разбивку по переводу на новую строку,
отсеивает пустые строки, перебирает в цикле имеющиеся строки,
выводит только вторую часть строки (разделенную по пробелу)
*/
result.output.split('\n').filter!(e => !e.strip.empty).each!((e) {
writefln("\t/%s", e.strip.split[1]);
});
}
void create() {
auto result = execute(
_baseCommand ~ ["add", "."]
);
if (result.status)
throw new SnapdException(
"Failed to prepare files for archiving:\n"
~ result.output
);
result = execute(
_baseCommand ~ ["commit", "-m", "test"]
);
if (result.status)
throw new SnapdException(
"Failed to create a backup:\n"
~ result.output
);
writeln("Backup was created successfully");
}
}

View file

@ -0,0 +1,3 @@
module snapd.core;
public import snapd.core.core;

View file

@ -2,3 +2,4 @@ module snapd;
public import snapd.version_; public import snapd.version_;
public import snapd.config; public import snapd.config;
public import snapd.core;

View file

@ -1,3 +1,3 @@
module snapd.version_; module snapd.version_;
enum snapdVersion = "0.0.2"; enum snapdVersion = "0.0.3";