2017-03-12 16:07:45 +01:00
|
|
|
import std.file, std.string, std.regex, std.stdio;
|
2017-03-24 22:30:03 +01:00
|
|
|
import selective;
|
2016-08-04 23:35:58 +02:00
|
|
|
static import log;
|
2015-09-01 20:45:34 +02:00
|
|
|
|
2016-08-04 23:35:58 +02:00
|
|
|
final class Config
|
2015-09-01 20:45:34 +02:00
|
|
|
{
|
2016-08-04 23:35:58 +02:00
|
|
|
public string refreshTokenFilePath;
|
2017-05-28 20:54:57 +02:00
|
|
|
public string deltaLinkFilePath;
|
2016-08-04 23:35:58 +02:00
|
|
|
public string databaseFilePath;
|
|
|
|
public string uploadStateFilePath;
|
2017-03-12 16:07:45 +01:00
|
|
|
public string syncListFilePath;
|
2016-08-04 23:35:58 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-08-04 23:35:58 +02:00
|
|
|
this(string configDirName)
|
|
|
|
{
|
|
|
|
refreshTokenFilePath = configDirName ~ "/refresh_token";
|
2017-05-28 20:54:57 +02:00
|
|
|
deltaLinkFilePath = configDirName ~ "/delta_link";
|
2016-12-25 19:23:33 +01:00
|
|
|
databaseFilePath = configDirName ~ "/items.sqlite3";
|
2016-08-04 23:35:58 +02:00
|
|
|
uploadStateFilePath = configDirName ~ "/resume_upload";
|
|
|
|
userConfigFilePath = configDirName ~ "/config";
|
2017-03-12 16:07:45 +01:00
|
|
|
syncListFilePath = configDirName ~ "/sync_list";
|
2016-08-04 23:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
2015-09-01 20:45:34 +02:00
|
|
|
{
|
2018-09-13 00:46:27 +02:00
|
|
|
// Default configuration directory
|
2017-03-12 16:07:45 +01:00
|
|
|
setValue("sync_dir", "~/OneDrive");
|
2018-04-16 13:39:45 +02:00
|
|
|
// Configure to skip ONLY temp files (~*.doc etc) by default
|
|
|
|
// Prior configuration was: .*|~*
|
|
|
|
setValue("skip_file", "~*");
|
2018-08-02 22:02:10 +02:00
|
|
|
// By default symlinks are not skipped (using string type
|
|
|
|
// instead of boolean because hashmap only stores string types)
|
|
|
|
setValue("skip_symlinks", "false");
|
2018-08-07 21:35:18 +02:00
|
|
|
// 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");
|
2018-08-09 23:40:17 +02:00
|
|
|
|
2017-03-12 16:07:45 +01:00
|
|
|
if (!load(userConfigFilePath)) {
|
|
|
|
log.vlog("No config file found, using defaults");
|
|
|
|
}
|
2015-09-01 20:45:34 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 23:35:58 +02:00
|
|
|
string getValue(string key)
|
2015-09-01 20:45:34 +02:00
|
|
|
{
|
2015-09-24 19:12:19 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:07:45 +01:00
|
|
|
void setValue(string key, string value)
|
|
|
|
{
|
|
|
|
values[key] = value;
|
|
|
|
}
|
|
|
|
|
2016-08-04 23:35:58 +02:00
|
|
|
private bool load(string filename)
|
2015-09-01 20:45:34 +02:00
|
|
|
{
|
2016-08-04 23:35:58 +02:00
|
|
|
scope(failure) return false;
|
2015-09-24 18:59:17 +02:00
|
|
|
auto file = File(filename, "r");
|
2017-03-12 16:07:45 +01:00
|
|
|
auto r = regex(`^(\w+)\s*=\s*"(.*)"\s*$`);
|
2015-09-24 18:59:17 +02:00
|
|
|
foreach (line; file.byLine()) {
|
2017-03-12 16:07:45 +01:00
|
|
|
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;
|
|
|
|
c.popFront();
|
|
|
|
values[key] = c.front.dup;
|
|
|
|
} else {
|
2016-08-04 23:35:58 +02:00
|
|
|
log.log("Malformed config line: ", line);
|
2015-09-22 14:48:18 +02:00
|
|
|
}
|
2015-09-01 20:45:34 +02:00
|
|
|
}
|
2016-08-04 23:35:58 +02:00
|
|
|
return true;
|
2015-09-01 20:45:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest
|
|
|
|
{
|
2016-08-04 23:35:58 +02:00
|
|
|
auto cfg = new Config("");
|
2017-03-12 16:07:45 +01:00
|
|
|
cfg.load("config");
|
2016-08-04 23:35:58 +02:00
|
|
|
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
|
|
|
}
|