abraunegg-onedrive/src/config.d

121 lines
3.1 KiB
D
Raw Normal View History

import std.file, std.string, std.regex, std.stdio;
import selective;
static import log;
2015-09-01 20:45:34 +02:00
final class Config
2015-09-01 20:45:34 +02:00
{
public string refreshTokenFilePath;
public string deltaLinkFilePath;
public string databaseFilePath;
public string uploadStateFilePath;
public string syncListFilePath;
private string userConfigFilePath;
// hashmap for the values found in the user config file
2015-09-01 20:45:34 +02:00
private string[string] values;
this(string configDirName)
{
refreshTokenFilePath = configDirName ~ "/refresh_token";
deltaLinkFilePath = configDirName ~ "/delta_link";
2016-12-25 19:23:33 +01:00
databaseFilePath = configDirName ~ "/items.sqlite3";
uploadStateFilePath = configDirName ~ "/resume_upload";
userConfigFilePath = configDirName ~ "/config";
syncListFilePath = configDirName ~ "/sync_list";
}
bool init()
2015-09-01 20:45:34 +02:00
{
// Default configuration directory
setValue("sync_dir", "~/OneDrive");
// Configure to skip ONLY temp files (~*.doc etc) by default
// Prior configuration was: .*|~*
setValue("skip_file", "~*");
// By default symlinks are not skipped (using string type
// instead of boolean because hashmap only stores string types)
setValue("skip_symlinks", "false");
// Configure the monitor mode loop - the number of seconds by which
// each sync operation is undertaken when idle under monitor mode
setValue("monitor_interval", "45");
// Configure the default logging directory to be /var/log/onedrive/
setValue("log_dir", "/var/log/onedrive/");
// Configure a default empty value for drive_id
setValue("drive_id", "");
// Minimal changes that trigger a log and notification on sync
setValue("min_notif_changes", "5");
if (!load(userConfigFilePath)) {
// What was the reason for failure?
if (!exists(userConfigFilePath)) {
log.vlog("No config file found, using application defaults");
return true;
} else {
log.log("Configuration file has errors - please check your configuration");
return false;
}
}
return true;
2015-09-01 20:45:34 +02:00
}
string getValue(string key)
2015-09-01 20:45:34 +02:00
{
auto p = key in values;
if (p) {
return *p;
} else {
2015-09-14 19:21:06 +02:00
throw new Exception("Missing config value: " ~ key);
}
2015-09-01 20:45:34 +02:00
}
2016-09-18 11:37:41 +02:00
string getValue(string key, string value)
{
auto p = key in values;
if (p) {
return *p;
} else {
return value;
}
}
void setValue(string key, string value)
{
values[key] = value;
}
private bool load(string filename)
2015-09-01 20:45:34 +02:00
{
scope(failure) return false;
2015-09-24 18:59:17 +02:00
auto file = File(filename, "r");
auto r = regex(`^(\w+)\s*=\s*"(.*)"\s*$`);
2015-09-24 18:59:17 +02:00
foreach (line; file.byLine()) {
line = stripLeft(line);
if (line.length == 0 || line[0] == ';' || line[0] == '#') continue;
2015-09-24 18:59:17 +02:00
auto c = line.matchFirst(r);
if (!c.empty) {
c.popFront(); // skip the whole match
string key = c.front.dup;
auto p = key in values;
if (p) {
c.popFront();
setValue(key, c.front.dup);
} else {
log.log("Unknown key in config file: ", key);
return false;
}
2015-09-24 18:59:17 +02:00
} else {
log.log("Malformed config line: ", line);
return false;
2015-09-22 14:48:18 +02:00
}
2015-09-01 20:45:34 +02:00
}
return true;
2015-09-01 20:45:34 +02:00
}
}
unittest
{
auto cfg = new Config("");
cfg.load("config");
assert(cfg.getValue("sync_dir") == "~/OneDrive");
2016-09-18 11:37:41 +02:00
assert(cfg.getValue("empty", "default") == "default");
2015-09-24 18:59:17 +02:00
}