107 lines
2.5 KiB
D
107 lines
2.5 KiB
D
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 string _user;
|
|
|
|
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
|
|
);
|
|
|
|
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; }
|
|
}
|