Simplify code for readability (#961)

* Remove buildNormalizedPath complexity and simplify code
This commit is contained in:
abraunegg 2020-06-21 10:01:24 +10:00 committed by GitHub
parent d8bfe846c2
commit 650cb97d3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,6 +23,9 @@ int main(string[] args)
// main function variables // main function variables
string confdirOption; string confdirOption;
string configFilePath;
string syncListFilePath;
string databaseFilePath;
string currentConfigHash; string currentConfigHash;
string currentSyncListHash; string currentSyncListHash;
string previousConfigHash; string previousConfigHash;
@ -137,6 +140,11 @@ int main(string[] args)
// update configuration from command line args // update configuration from command line args
cfg.update_from_args(args); cfg.update_from_args(args);
// Initialise normalised file paths
configFilePath = buildNormalizedPath(cfg.configDirName ~ "/config");
syncListFilePath = buildNormalizedPath(cfg.configDirName ~ "/sync_list");
databaseFilePath = buildNormalizedPath(cfg.configDirName ~ "/items.db");
// Has any of our configuration that would require a --resync been changed? // Has any of our configuration that would require a --resync been changed?
// 1. sync_list file modification // 1. sync_list file modification
// 2. config file modification - but only if sync_dir, skip_dir, skip_file or drive_id was modified // 2. config file modification - but only if sync_dir, skip_dir, skip_file or drive_id was modified
@ -146,19 +154,19 @@ int main(string[] args)
configBackupFile = buildNormalizedPath(cfg.configDirName ~ "/.config.backup"); configBackupFile = buildNormalizedPath(cfg.configDirName ~ "/.config.backup");
// Does a config file exist with a valid hash file // Does a config file exist with a valid hash file
if ((exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) && (!exists(configHashFile))) { if ((exists(configFilePath)) && (!exists(configHashFile))) {
// Hash of config file needs to be created // Hash of config file needs to be created
std.file.write(configHashFile, computeQuickXorHash(buildNormalizedPath(cfg.configDirName ~ "/config"))); std.file.write(configHashFile, computeQuickXorHash(configFilePath));
} }
// Does a sync_list file exist with a valid hash file // Does a sync_list file exist with a valid hash file
if ((exists(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))) && (!exists(syncListHashFile))) { if ((exists(syncListFilePath)) && (!exists(syncListHashFile))) {
// Hash of sync_list file needs to be created // Hash of sync_list file needs to be created
std.file.write(syncListHashFile, computeQuickXorHash(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))); std.file.write(syncListHashFile, computeQuickXorHash(syncListFilePath));
} }
// If hash files exist, but config files do not ... remove the hash, but only if --resync was issued as now the application will use 'defaults' which 'may' be different // If hash files exist, but config files do not ... remove the hash, but only if --resync was issued as now the application will use 'defaults' which 'may' be different
if ((!exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) && (exists(configHashFile))) { if ((!exists(configFilePath)) && (exists(configHashFile))) {
// if --resync safe remove config.hash and config.backup // if --resync safe remove config.hash and config.backup
if (cfg.getValueBool("resync")) { if (cfg.getValueBool("resync")) {
safeRemove(configHashFile); safeRemove(configHashFile);
@ -167,14 +175,14 @@ int main(string[] args)
} }
// If sync_list hash file exists, but sync_list file does not ... remove the hash, but only if --resync was issued as now the application will use 'defaults' which 'may' be different // If sync_list hash file exists, but sync_list file does not ... remove the hash, but only if --resync was issued as now the application will use 'defaults' which 'may' be different
if ((!exists(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))) && (exists(syncListHashFile))) { if ((!exists(syncListFilePath)) && (exists(syncListHashFile))) {
// if --resync safe remove sync_list.hash // if --resync safe remove sync_list.hash
if (cfg.getValueBool("resync")) safeRemove(syncListHashFile); if (cfg.getValueBool("resync")) safeRemove(syncListHashFile);
} }
// Read config hashes if they exist // Read config hashes if they exist
if (exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) currentConfigHash = computeQuickXorHash(buildNormalizedPath(cfg.configDirName ~ "/config")); if (exists(configFilePath)) currentConfigHash = computeQuickXorHash(configFilePath);
if (exists(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))) currentSyncListHash = computeQuickXorHash(buildNormalizedPath(cfg.configDirName ~ "/sync_list")); if (exists(syncListFilePath)) currentSyncListHash = computeQuickXorHash(syncListFilePath);
if (exists(configHashFile)) previousConfigHash = readText(configHashFile); if (exists(configHashFile)) previousConfigHash = readText(configHashFile);
if (exists(syncListHashFile)) previousSyncListHash = readText(syncListHashFile); if (exists(syncListHashFile)) previousSyncListHash = readText(syncListHashFile);
@ -254,26 +262,26 @@ int main(string[] args)
// we are not in a dry-run scenario // we are not in a dry-run scenario
// update config hash // update config hash
log.vdebug("updating config hash as it is out of date"); log.vdebug("updating config hash as it is out of date");
std.file.write(configHashFile, computeQuickXorHash(cfg.configDirName ~ "/config")); std.file.write(configHashFile, computeQuickXorHash(configFilePath));
// create backup copy of current config file // create backup copy of current config file
log.vdebug("making backup of config file as it is out of date"); log.vdebug("making backup of config file as it is out of date");
std.file.copy(cfg.configDirName ~ "/config", configBackupFile); std.file.copy(configFilePath, configBackupFile);
} }
} }
} }
} }
// Is there a backup of the config file if the config file exists? // Is there a backup of the config file if the config file exists?
if ((exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) && (!exists(configBackupFile))) { if ((exists(configFilePath)) && (!exists(configBackupFile))) {
// create backup copy of current config file // create backup copy of current config file
std.file.copy(buildNormalizedPath(cfg.configDirName ~ "/config"), configBackupFile); std.file.copy(configFilePath, configBackupFile);
} }
// config file set options can be changed via CLI input, specifically these will impact sync and --resync will be needed: // config file set options can be changed via CLI input, specifically these will impact sync and --resync will be needed:
// --syncdir ARG // --syncdir ARG
// --skip-file ARG // --skip-file ARG
// --skip-dir ARG // --skip-dir ARG
if (exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) { if (exists(configFilePath)) {
// config file exists // config file exists
// was the sync_dir updated by CLI? // was the sync_dir updated by CLI?
if (cfg.configFileSyncDir != "") { if (cfg.configFileSyncDir != "") {
@ -319,18 +327,18 @@ int main(string[] args)
// --resync issued, update hashes of config files if they exist // --resync issued, update hashes of config files if they exist
if (!cfg.getValueBool("dry_run")) { if (!cfg.getValueBool("dry_run")) {
// not doing a dry run, update hash files if config & sync_list exist // not doing a dry run, update hash files if config & sync_list exist
if (exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) { if (exists(configFilePath)) {
// update hash // update hash
log.vdebug("updating config hash as --resync issued"); log.vdebug("updating config hash as --resync issued");
std.file.write(configHashFile, computeQuickXorHash(buildNormalizedPath(cfg.configDirName ~ "/config"))); std.file.write(configHashFile, computeQuickXorHash(configFilePath));
// create backup copy of current config file // create backup copy of current config file
log.vdebug("making backup of config file as --resync issued"); log.vdebug("making backup of config file as --resync issued");
std.file.copy(buildNormalizedPath(cfg.configDirName ~ "/config"), configBackupFile); std.file.copy(configFilePath, configBackupFile);
} }
if (exists(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))) { if (exists(syncListFilePath)) {
// update sync_list hash // update sync_list hash
log.vdebug("updating sync_list hash as --resync issued"); log.vdebug("updating sync_list hash as --resync issued");
std.file.write(syncListHashFile, computeQuickXorHash(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))); std.file.write(syncListHashFile, computeQuickXorHash(syncListFilePath));
} }
} }
} }
@ -401,9 +409,9 @@ int main(string[] args)
log.setNotifications(cfg.getValueBool("monitor") && !cfg.getValueBool("disable_notifications")); log.setNotifications(cfg.getValueBool("monitor") && !cfg.getValueBool("disable_notifications"));
// Application upgrades - skilion version etc // Application upgrades - skilion version etc
if (exists(buildNormalizedPath(cfg.configDirName ~ "/items.db"))) { if (exists(databaseFilePath)) {
if (!cfg.getValueBool("dry_run")) { if (!cfg.getValueBool("dry_run")) {
safeRemove(buildNormalizedPath(cfg.configDirName ~ "/items.db")); safeRemove(databaseFilePath);
} }
log.logAndNotify("Database schema changed, resync needed"); log.logAndNotify("Database schema changed, resync needed");
cfg.setValueBool("resync", true); cfg.setValueBool("resync", true);
@ -428,8 +436,6 @@ int main(string[] args)
// Display current application configuration, no application initialisation // Display current application configuration, no application initialisation
if (cfg.getValueBool("display_config")){ if (cfg.getValueBool("display_config")){
string userConfigFilePath = buildNormalizedPath(cfg.configDirName ~ "/config");
string userSyncList = buildNormalizedPath(cfg.configDirName ~ "/sync_list");
// Display application version // Display application version
writeln("onedrive version = ", strip(import("version"))); writeln("onedrive version = ", strip(import("version")));
@ -437,11 +443,7 @@ int main(string[] args)
writeln("Config path = ", cfg.configDirName); writeln("Config path = ", cfg.configDirName);
// Does a config file exist or are we using application defaults // Does a config file exist or are we using application defaults
if (exists(userConfigFilePath)){ writeln("Config file found in config path = ", exists(configFilePath));
writeln("Config file found in config path = true");
} else {
writeln("Config file found in config path = false");
}
// Config Options // Config Options
writeln("Config option 'check_nosync' = ", cfg.getValueBool("check_nosync")); writeln("Config option 'check_nosync' = ", cfg.getValueBool("check_nosync"));
@ -461,12 +463,12 @@ int main(string[] args)
} }
// Is sync_list configured? // Is sync_list configured?
if (exists(userSyncList)){ if (exists(syncListFilePath)){
writeln("Config option 'sync_root_files' = ", cfg.getValueBool("sync_root_files")); writeln("Config option 'sync_root_files' = ", cfg.getValueBool("sync_root_files"));
writeln("Selective sync configured = true"); writeln("Selective sync configured = true");
writeln("sync_list contents:"); writeln("sync_list contents:");
// Output the sync_list contents // Output the sync_list contents
auto syncListFile = File(userSyncList); auto syncListFile = File(syncListFilePath);
auto range = syncListFile.byLine(); auto range = syncListFile.byLine();
foreach (line; range) foreach (line; range)
{ {