From 66d64a964200f074c9c88778655a7554f083de06 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sat, 24 May 2025 01:38:52 +0300 Subject: [PATCH] =?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;