mirror of
https://github.com/abraunegg/onedrive
synced 2024-05-18 13:46:41 +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
|
struct Config
|
||||||
{
|
{
|
||||||
private string filename;
|
|
||||||
private string[string] values;
|
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;
|
import core.exception;
|
||||||
try {
|
try {
|
||||||
return values[key];
|
return values[key];
|
||||||
} catch (RangeError e) {
|
} catch (RangeError e) {
|
||||||
if (def) return def;
|
|
||||||
throw new Exception("Missing config value: " ~ key);
|
throw new Exception("Missing config value: " ~ key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void set(string key, string value)
|
private void load(string filename)
|
||||||
{
|
|
||||||
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()
|
|
||||||
{
|
{
|
||||||
if (exists(filename)) {
|
if (exists(filename)) {
|
||||||
string bkpFilename = filename ~ "~";
|
auto file = File(filename, "r");
|
||||||
rename(filename, bkpFilename);
|
auto r = regex("(?:^\\s*)(\\w+)(?:\\s*=\\s*\")(.*)(?:\"\\s*$)");
|
||||||
}
|
foreach (line; file.byLine()) {
|
||||||
auto file = File(filename, "w");
|
auto c = line.matchFirst(r);
|
||||||
foreach (key, value; values) {
|
if (!c.empty) {
|
||||||
file.writeln(key, " = \"", value, "\"");
|
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
|
unittest
|
||||||
{
|
{
|
||||||
auto cfg = Config(tempDir() ~ "/test.conf");
|
auto cfg = Config("empty", "onedrive.conf");
|
||||||
cfg.set("test1", "1");
|
assert(cfg.get("sync_dir") == "~/OneDrive");
|
||||||
cfg.set("test2", "2");
|
|
||||||
cfg.set("test1", "3");
|
|
||||||
cfg.save();
|
|
||||||
cfg.load();
|
|
||||||
assert(cfg.get("test1") == "3");
|
|
||||||
assert(cfg.get("test2") == "2");
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,9 @@ void main(string[] args)
|
||||||
}
|
}
|
||||||
|
|
||||||
string configDirName = expandTilde(environment.get("XDG_CONFIG_HOME", "~/.config")) ~ "/onedrive";
|
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 refreshTokenFilePath = configDirName ~ "/refresh_token";
|
||||||
string statusTokenFilePath = configDirName ~ "/status_token";
|
string statusTokenFilePath = configDirName ~ "/status_token";
|
||||||
string databaseFilePath = configDirName ~ "/items.db";
|
string databaseFilePath = configDirName ~ "/items.db";
|
||||||
|
@ -40,8 +42,7 @@ void main(string[] args)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose) writeln("Loading config ...");
|
if (verbose) writeln("Loading config ...");
|
||||||
auto cfg = config.Config(configFilePath);
|
auto cfg = config.Config(configFile1Path, configFile2Path, configFile3Path);
|
||||||
cfg.load();
|
|
||||||
|
|
||||||
if (verbose) writeln("Initializing the OneDrive API ...");
|
if (verbose) writeln("Initializing the OneDrive API ...");
|
||||||
auto onedrive = new OneDriveApi(cfg, verbose);
|
auto onedrive = new OneDriveApi(cfg, verbose);
|
||||||
|
|
|
@ -40,8 +40,8 @@ struct Monitor
|
||||||
void init(Config cfg, bool verbose)
|
void init(Config cfg, bool verbose)
|
||||||
{
|
{
|
||||||
this.verbose = verbose;
|
this.verbose = verbose;
|
||||||
skipDir = wild2regex(cfg.get("skip_dir", ""));
|
skipDir = wild2regex(cfg.get("skip_dir"));
|
||||||
skipFile = wild2regex(cfg.get("skip_file", ""));
|
skipFile = wild2regex(cfg.get("skip_file"));
|
||||||
fd = inotify_init();
|
fd = inotify_init();
|
||||||
if (fd == -1) throw new MonitorException("inotify_init failed");
|
if (fd == -1) throw new MonitorException("inotify_init failed");
|
||||||
if (!buffer) buffer = new void[4096];
|
if (!buffer) buffer = new void[4096];
|
||||||
|
|
|
@ -69,8 +69,8 @@ final class SyncEngine
|
||||||
this.onedrive = onedrive;
|
this.onedrive = onedrive;
|
||||||
this.itemdb = itemdb;
|
this.itemdb = itemdb;
|
||||||
this.verbose = verbose;
|
this.verbose = verbose;
|
||||||
skipDir = wild2regex(cfg.get("skip_dir", ""));
|
skipDir = wild2regex(cfg.get("skip_dir"));
|
||||||
skipFile = wild2regex(cfg.get("skip_file", ""));
|
skipFile = wild2regex(cfg.get("skip_file"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void setStatusToken(string statusToken)
|
void setStatusToken(string statusToken)
|
||||||
|
|
Loading…
Reference in a new issue