alert when no config file is found

This commit is contained in:
skilion 2015-09-24 18:59:17 +02:00
parent 53d5d57678
commit 3489827431

View file

@ -6,9 +6,14 @@ struct Config
this(string[] filenames...) this(string[] filenames...)
{ {
bool found = false;
foreach (filename; filenames) { foreach (filename; filenames) {
load(filename); if (exists(filename)) {
found = true;
load(filename);
}
} }
if (!found) throw new Exception("No config file found");
} }
string get(string key) string get(string key)
@ -23,19 +28,17 @@ struct Config
private void load(string filename) private void load(string filename)
{ {
if (exists(filename)) { auto file = File(filename, "r");
auto file = File(filename, "r"); auto r = regex("(?:^\\s*)(\\w+)(?:\\s*=\\s*\")(.*)(?:\"\\s*$)");
auto r = regex("(?:^\\s*)(\\w+)(?:\\s*=\\s*\")(.*)(?:\"\\s*$)"); foreach (line; file.byLine()) {
foreach (line; file.byLine()) { auto c = line.matchFirst(r);
auto c = line.matchFirst(r); if (!c.empty) {
if (!c.empty) { c.popFront(); // skip the whole match
c.popFront(); // skip the whole match string key = c.front.dup;
string key = c.front.dup; c.popFront();
c.popFront(); values[key] = c.front.dup;
values[key] = c.front.dup; } else {
} else { writeln("Malformed config line: ", line);
writeln("Malformed config line: ", line);
}
} }
} }
} }
@ -46,3 +49,12 @@ unittest
auto cfg = Config("empty", "onedrive.conf"); auto cfg = Config("empty", "onedrive.conf");
assert(cfg.get("sync_dir") == "~/OneDrive"); assert(cfg.get("sync_dir") == "~/OneDrive");
} }
unittest
{
try {
auto cfg = Config("empty");
assert(0);
} catch (Exception e) {
}
}