From f1d1bce20b8250f89d725b60dbedd13d8ce12bae Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Fri, 23 May 2025 20:47:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8,=20=D0=B2=D0=BA=D0=BB=D1=8E=D1=87=D0=B0=D1=8E=D1=89?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D1=80=D0=B8=20=D0=B2=D0=BE=D0=B7=D0=BD?= =?UTF-8?q?=D0=B8=D0=BA=D0=BD=D0=BE=D0=B2=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BE?= =?UTF-8?q?=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/snapd/config/config.d | 93 +++++++++++++++++++++++++++++++++ source/snapd/config/exception.d | 14 +++++ source/snapd/config/package.d | 4 ++ 3 files changed, 111 insertions(+) create mode 100644 source/snapd/config/config.d create mode 100644 source/snapd/config/exception.d create mode 100644 source/snapd/config/package.d diff --git a/source/snapd/config/config.d b/source/snapd/config/config.d new file mode 100644 index 0000000..5822758 --- /dev/null +++ b/source/snapd/config/config.d @@ -0,0 +1,93 @@ +module snapd.config.config; + +import std.json; +import std.file; +import std.path; +import std.regex; +import std.string; + +import snapd.config.exception; + +class SnapdConfig { + private string _git; + private string _project; + private string _email; + + private bool isValidEmail(string email) { + auto emailPattern = ctRegex!r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; + return !matchFirst(email, emailPattern).empty; + } + + this(string configFile) { + string jsonText; + JSONValue jsonData; + + try { + jsonText = readText(configFile); + jsonData = parseJSON(jsonText); + } catch (Exception e) { + throw new SnapdConfigException( + "An error occurred while reading the configuration file:\n\t" + ~ e.msg + ); + } + + if ("git" !in jsonData) + throw new SnapdConfigException( + "The configuration file is missing the \"git\" parameter" + ); + + _git = jsonData["git"].str; + + if (!_git.length) + throw new SnapdConfigException( + "The \"git\" parameter must contain the path to the directory" + ); + + if (!_git.isAbsolute) + throw new SnapdConfigException( + "The \"git\" parameter must be an absolute path to the directory:\n\t" + ~ _git + ); + + if ("project" !in jsonData) + throw new SnapdConfigException( + "The configuration file is missing the \"project\" parameter" + ); + + _project = jsonData["project"].str; + + if (!_project.length) + throw new SnapdConfigException( + "The \"project\" parameter must contain the path to the directory" + ); + + if (!_project.isAbsolute) + throw new SnapdConfigException( + "The \"project\" parameter must be an absolute path to the directory:\n\t" + ~ _project + ); + + if ("email" !in jsonData) + throw new SnapdConfigException( + "The configuration file is missing the \"email\" parameter" + ); + + _email = jsonData["email"].str; + + if (!_email.length) + throw new SnapdConfigException( + "The \"email\" parameter must contain an email address" + ); + + if (!isValidEmail(_email)) + throw new SnapdConfigException( + "Invalid email address provided in the \"email\" parameter:\n\t" + ~ _email + ); + } + + @property string git() const { return _git; } + @property string project() const { return _project; } + @property string email() const { return _email; } +} diff --git a/source/snapd/config/exception.d b/source/snapd/config/exception.d new file mode 100644 index 0000000..1abc0e5 --- /dev/null +++ b/source/snapd/config/exception.d @@ -0,0 +1,14 @@ +module snapd.config.exception; + +import std.exception; +import std.stdio : writeln; + +class SnapdConfigException : Exception { + this(string msg, string file = __FILE__, size_t line = __LINE__) { + super(msg, file, line); + } + + void print() { + writeln(msg); + } +} diff --git a/source/snapd/config/package.d b/source/snapd/config/package.d new file mode 100644 index 0000000..5dd2aed --- /dev/null +++ b/source/snapd/config/package.d @@ -0,0 +1,4 @@ +module snapd.config; + +public import snapd.config.exception; +public import snapd.config.config;