snag/source/snag/config/config.d

130 lines
3.3 KiB
D

module snag.config.config;
import std.json;
import std.file;
import std.path;
import std.string;
import snag.lib;
import std.algorithm;
import std.array;
import snag.config.exception;
class SnagConfig {
private string _git;
private string _project;
private string _email;
private string _author;
private string[] _tracking;
private string[] _ignore;
this(string configFile) {
string jsonText;
JSONValue jsonData;
try {
jsonText = readText(configFile);
jsonData = parseJSON(jsonText);
} catch (Exception e) {
throw new SnagConfigException(
"An error occurred while reading the configuration file:\n\t"
~ e.msg
);
}
if ("git" !in jsonData)
throw new SnagConfigException(
"The configuration file is missing the \"git\" parameter"
);
_git = jsonData["git"].str;
if (!_git.length)
throw new SnagConfigException(
"The \"git\" parameter must contain the path to the directory"
);
if (!_git.isAbsolute)
throw new SnagConfigException(
"The \"git\" parameter must be an absolute path to the directory:\n\t"
~ _git
);
if ("project" !in jsonData)
throw new SnagConfigException(
"The configuration file is missing the \"project\" parameter"
);
_project = jsonData["project"].str;
if (!_project.length)
throw new SnagConfigException(
"The \"project\" parameter must contain the path to the directory"
);
if (!_project.isAbsolute)
throw new SnagConfigException(
"The \"project\" parameter must be an absolute path to the directory:\n\t"
~ _project
);
if ("email" !in jsonData)
throw new SnagConfigException(
"The configuration file is missing the \"email\" parameter"
);
_email = jsonData["email"].str;
if (!_email.length)
throw new SnagConfigException(
"The \"email\" parameter must contain an email address"
);
if (!isValidEmail(_email))
throw new SnagConfigException(
"Invalid email address provided in the \"email\" parameter:\n\t"
~ _email
);
if ("author" !in jsonData)
throw new SnagConfigException(
"The configuration file is missing the \"author\" parameter"
);
_author = jsonData["author"].str;
if (!_author.length)
throw new SnagConfigException(
"The \"author\" parameter must contain an author name"
);
if ("rules" in jsonData) {
if (jsonData["rules"].type != JSONType.object)
throw new SnagConfigException(
"The \"rules\" parameter must be an object"
);
auto rules = jsonData["rules"];
if ("tracking" in rules) {
if (rules["tracking"].type != JSONType.array)
throw new SnagConfigException(
"The \"tracking\" parameter must be an array containing a set of paths to tracked files"
);
_tracking = rules["tracking"].array.map!(item => item.str).array;
}
if ("ignore" in rules) {
if (rules["ignore"].type != JSONType.array)
throw new SnagConfigException(
"The \"ignore\" parameter must contain a gitignore rule"
);
_ignore = rules["ignore"].array.map!(item => item.str).array;
}
}
}
@property string git() const { return _git; }
@property string project() const { return _project; }
@property string email() const { return _email; }
@property string author() const { return _author; }
@property const(string[]) tracking() const { return _tracking; }
@property const(string[]) ignore() const { return _ignore; }
}