Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check config file keys for validity, use setValue instead of direct access #296

Merged
merged 4 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class Config
syncListFilePath = configDirName ~ "/sync_list";
}

void init()
bool init()
{
// Default configuration directory
setValue("sync_dir", "~/OneDrive");
Expand All @@ -39,10 +39,20 @@ final class Config
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", "");

if (!load(userConfigFilePath)) {
log.vlog("No config file found, using defaults");
// 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;
}

string getValue(string key)
Expand Down Expand Up @@ -82,10 +92,17 @@ final class Config
if (!c.empty) {
c.popFront(); // skip the whole match
string key = c.front.dup;
c.popFront();
values[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;
}
} else {
log.log("Malformed config line: ", line);
return false;
}
}
return true;
Expand Down
6 changes: 5 additions & 1 deletion src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ int main(string[] args)
log.vlog("Using Config Dir: ", configDirName);
if (!exists(configDirName)) mkdirRecurse(configDirName);
auto cfg = new config.Config(configDirName);
cfg.init();
if(!cfg.init()){
// There was an error loading the configuration
// Error message already printed
return EXIT_FAILURE;
}

// Configure logging if enabled
if (enableLogFile){
Expand Down