From cf85cc3c774c16732b0309dcb5bfb0ab03f8bba8 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sat, 24 May 2025 01:33:42 +0300 Subject: [PATCH 1/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BF=D0=B0=D1=80?= =?UTF-8?q?=D0=B0=D0=BC=D0=B5=D1=82=D1=80=20user=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20git?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/snapd/config/config.d | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/source/snapd/config/config.d b/source/snapd/config/config.d index 5822758..e6d7f8f 100644 --- a/source/snapd/config/config.d +++ b/source/snapd/config/config.d @@ -12,6 +12,7 @@ class SnapdConfig { private string _git; private string _project; private string _email; + private string _user; private bool isValidEmail(string email) { 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" ~ _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 project() const { return _project; } @property string email() const { return _email; } + @property string user() const { return _user; } } From ff9dd8757d45522b60128461465186376538ac29 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sat, 24 May 2025 01:34:32 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=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B0)=20=D0=BE=D1=82=D1=81?= =?UTF-8?q?=D0=BB=D0=B5=D0=B6=D0=B8=D0=B2=D0=B0=D0=B5=D0=BC=D1=8B=D1=85=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=20-=20postsnap=20-=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=B1=D0=BE=D1=80=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE?= =?UTF-8?q?=D0=B2=20(=D0=B8=D0=BB=D0=B8=20=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=3F!)=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D1=8B=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=D1=81=D0=BB?= =?UTF-8?q?=D0=B5=20=D0=B0=D1=80=D1=85=D0=B8=D0=B2=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20(=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82=D0=B0)=20=D0=BE=D1=82?= =?UTF-8?q?=D1=81=D0=BB=D0=B5=D0=B6=D0=B8=D0=B2=D0=B0=D0=B5=D0=BC=D1=8B?= =?UTF-8?q?=D1=85=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snapd.json | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/snapd.json b/snapd.json index d8b0536..3fe8034 100644 --- a/snapd.json +++ b/snapd.json @@ -1,5 +1,14 @@ { - "git": "/var/lib/snapd", - "project": "/", - "email": "user@site.domain" + "git": "/tmp/testgit", + "project": "/home/alexander/Programming/new/dlang/snapd/source", + "email": "user@site.domain", + "user": "snapd", + "presnap": [ + "/usr/bin/ls", + "/usr/local/bin/script.sh" + ], + "postsnap": [ + "/usr/bin/ls", + "/usr/local/bin/script.sh" + ] } From 66d64a964200f074c9c88778655a7554f083de06 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sat, 24 May 2025 01:38:52 +0300 Subject: [PATCH 3/4] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D1=8F=D0=B4?= =?UTF-8?q?=D1=80=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/snapd/core/core.d | 109 ++++++++++++++++++++++++++++++++++++ source/snapd/core/package.d | 3 + source/snapd/package.d | 1 + 3 files changed, 113 insertions(+) create mode 100644 source/snapd/core/core.d create mode 100644 source/snapd/core/package.d diff --git a/source/snapd/core/core.d b/source/snapd/core/core.d new file mode 100644 index 0000000..2d285c4 --- /dev/null +++ b/source/snapd/core/core.d @@ -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"); + } +} diff --git a/source/snapd/core/package.d b/source/snapd/core/package.d new file mode 100644 index 0000000..ac6ebaf --- /dev/null +++ b/source/snapd/core/package.d @@ -0,0 +1,3 @@ +module snapd.core; + +public import snapd.core.core; diff --git a/source/snapd/package.d b/source/snapd/package.d index 4f5e10f..4360689 100644 --- a/source/snapd/package.d +++ b/source/snapd/package.d @@ -2,3 +2,4 @@ module snapd; public import snapd.version_; public import snapd.config; +public import snapd.core; From 7714bc498e608ab228d3845bbdd2550ea203d579 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sat, 24 May 2025 01:39:20 +0300 Subject: [PATCH 4/4] 0.0.3 --- source/app.d | 23 +++++++++++++++++++++-- source/snapd/version_.d | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/source/app.d b/source/app.d index 3ac5b7a..72df7c3 100644 --- a/source/app.d +++ b/source/app.d @@ -2,8 +2,6 @@ import snapd; import commandr; import std.file; -import std.stdio : writeln; - import core.stdc.stdlib : EXIT_SUCCESS, EXIT_FAILURE; private string programName = "snapd"; @@ -11,6 +9,9 @@ private string programName = "snapd"; int main(string[] args) { 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") .optional .validateEachWith( @@ -31,5 +32,23 @@ int main(string[] args) 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; } diff --git a/source/snapd/version_.d b/source/snapd/version_.d index 98d0114..bc63b1b 100644 --- a/source/snapd/version_.d +++ b/source/snapd/version_.d @@ -1,3 +1,3 @@ module snapd.version_; -enum snapdVersion = "0.0.2"; +enum snapdVersion = "0.0.3";