mirror of
https://github.com/abraunegg/onedrive
synced 2024-05-04 23:13:17 +02:00
changed how config files works
This commit is contained in:
parent
4f5d4fad91
commit
7cfa936db8
64
src/config.d
64
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");
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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];
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue