diff --git a/config b/onedrive.conf similarity index 100% rename from config rename to onedrive.conf diff --git a/src/config.d b/src/config.d index 021caf5e..a8cc1d66 100644 --- a/src/config.d +++ b/src/config.d @@ -2,69 +2,47 @@ import std.file, std.regex, std.stdio; struct Config { - private string filename; private string[string] values; - this(string filename) + this(string[] filenames...) { - this.filename = filename; + foreach (filename; filenames) { + load(filename); + } } - string get(string key, string def = null) + string get(string key) { import core.exception; try { return values[key]; } catch (RangeError e) { - if (def) return def; throw new Exception("Missing config value: " ~ key); } } - void set(string key, string value) - { - values[key] = value; - } - - void load() - { - values = null; - auto file = File(filename, "r"); - auto r = regex("(?:^\\s*)(\\w+)(?:\\s*=\\s*\")(.*)(?:\"\\s*$)"); - foreach (line; file.byLine()) { - auto c = matchFirst(line, r); - if (!c.empty) { - c.popFront(); // skip the whole match - string key = c.front.dup; - c.popFront(); - values[key] = c.front.dup; - } else { - writeln("Malformed config line: ", line); - } - } - } - - void save() + private void load(string filename) { if (exists(filename)) { - string bkpFilename = filename ~ "~"; - rename(filename, bkpFilename); - } - auto file = File(filename, "w"); - foreach (key, value; values) { - file.writeln(key, " = \"", value, "\""); + auto file = File(filename, "r"); + auto r = regex("(?:^\\s*)(\\w+)(?:\\s*=\\s*\")(.*)(?:\"\\s*$)"); + foreach (line; file.byLine()) { + 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 { + writeln("Malformed config line: ", line); + } + } } } } unittest { - auto cfg = Config(tempDir() ~ "/test.conf"); - cfg.set("test1", "1"); - cfg.set("test2", "2"); - cfg.set("test1", "3"); - cfg.save(); - cfg.load(); - assert(cfg.get("test1") == "3"); - assert(cfg.get("test2") == "2"); + auto cfg = Config("empty", "onedrive.conf"); + assert(cfg.get("sync_dir") == "~/OneDrive"); } diff --git a/src/main.d b/src/main.d index 858415d8..80a8d771 100644 --- a/src/main.d +++ b/src/main.d @@ -28,7 +28,9 @@ void main(string[] args) } string configDirName = expandTilde(environment.get("XDG_CONFIG_HOME", "~/.config")) ~ "/onedrive"; - string configFilePath = configDirName ~ "/config"; + string configFile1Path = "/etc/onedrive.conf"; + string configFile2Path = "/usr/local/etc/onedrive.conf"; + string configFile3Path = configDirName ~ "/config"; string refreshTokenFilePath = configDirName ~ "/refresh_token"; string statusTokenFilePath = configDirName ~ "/status_token"; string databaseFilePath = configDirName ~ "/items.db"; @@ -40,8 +42,7 @@ void main(string[] args) } if (verbose) writeln("Loading config ..."); - auto cfg = config.Config(configFilePath); - cfg.load(); + auto cfg = config.Config(configFile1Path, configFile2Path, configFile3Path); if (verbose) writeln("Initializing the OneDrive API ..."); auto onedrive = new OneDriveApi(cfg, verbose); diff --git a/src/monitor.d b/src/monitor.d index 9ebe15f7..49edd830 100644 --- a/src/monitor.d +++ b/src/monitor.d @@ -40,8 +40,8 @@ struct Monitor void init(Config cfg, bool verbose) { this.verbose = verbose; - skipDir = wild2regex(cfg.get("skip_dir", "")); - skipFile = wild2regex(cfg.get("skip_file", "")); + skipDir = wild2regex(cfg.get("skip_dir")); + skipFile = wild2regex(cfg.get("skip_file")); fd = inotify_init(); if (fd == -1) throw new MonitorException("inotify_init failed"); if (!buffer) buffer = new void[4096]; diff --git a/src/sync.d b/src/sync.d index f85088f9..12bc21e6 100644 --- a/src/sync.d +++ b/src/sync.d @@ -69,8 +69,8 @@ final class SyncEngine this.onedrive = onedrive; this.itemdb = itemdb; this.verbose = verbose; - skipDir = wild2regex(cfg.get("skip_dir", "")); - skipFile = wild2regex(cfg.get("skip_file", "")); + skipDir = wild2regex(cfg.get("skip_dir")); + skipFile = wild2regex(cfg.get("skip_file")); } void setStatusToken(string statusToken)