From 650cb97d3fb98482dd63f145c6b74886287757c9 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Sun, 21 Jun 2020 10:01:24 +1000 Subject: [PATCH] Simplify code for readability (#961) * Remove buildNormalizedPath complexity and simplify code --- src/main.d | 60 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/main.d b/src/main.d index 878d4b45..5db7e7de 100644 --- a/src/main.d +++ b/src/main.d @@ -23,6 +23,9 @@ int main(string[] args) // main function variables string confdirOption; + string configFilePath; + string syncListFilePath; + string databaseFilePath; string currentConfigHash; string currentSyncListHash; string previousConfigHash; @@ -137,6 +140,11 @@ int main(string[] args) // update configuration from command line 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? // 1. sync_list file modification // 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"); // 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 - 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 - if ((exists(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))) && (!exists(syncListHashFile))) { + if ((exists(syncListFilePath)) && (!exists(syncListHashFile))) { // 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 ((!exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) && (exists(configHashFile))) { + if ((!exists(configFilePath)) && (exists(configHashFile))) { // if --resync safe remove config.hash and config.backup if (cfg.getValueBool("resync")) { 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 ((!exists(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))) && (exists(syncListHashFile))) { + if ((!exists(syncListFilePath)) && (exists(syncListHashFile))) { // if --resync safe remove sync_list.hash if (cfg.getValueBool("resync")) safeRemove(syncListHashFile); } // Read config hashes if they exist - if (exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) currentConfigHash = computeQuickXorHash(buildNormalizedPath(cfg.configDirName ~ "/config")); - if (exists(buildNormalizedPath(cfg.configDirName ~ "/sync_list"))) currentSyncListHash = computeQuickXorHash(buildNormalizedPath(cfg.configDirName ~ "/sync_list")); + if (exists(configFilePath)) currentConfigHash = computeQuickXorHash(configFilePath); + if (exists(syncListFilePath)) currentSyncListHash = computeQuickXorHash(syncListFilePath); if (exists(configHashFile)) previousConfigHash = readText(configHashFile); if (exists(syncListHashFile)) previousSyncListHash = readText(syncListHashFile); @@ -254,26 +262,26 @@ int main(string[] args) // we are not in a dry-run scenario // update config hash 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 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? - if ((exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) && (!exists(configBackupFile))) { + if ((exists(configFilePath)) && (!exists(configBackupFile))) { // 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: // --syncdir ARG // --skip-file ARG // --skip-dir ARG - if (exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) { + if (exists(configFilePath)) { // config file exists // was the sync_dir updated by CLI? if (cfg.configFileSyncDir != "") { @@ -319,18 +327,18 @@ int main(string[] args) // --resync issued, update hashes of config files if they exist if (!cfg.getValueBool("dry_run")) { // not doing a dry run, update hash files if config & sync_list exist - if (exists(buildNormalizedPath(cfg.configDirName ~ "/config"))) { + if (exists(configFilePath)) { // update hash 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 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 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")); // Application upgrades - skilion version etc - if (exists(buildNormalizedPath(cfg.configDirName ~ "/items.db"))) { + if (exists(databaseFilePath)) { if (!cfg.getValueBool("dry_run")) { - safeRemove(buildNormalizedPath(cfg.configDirName ~ "/items.db")); + safeRemove(databaseFilePath); } log.logAndNotify("Database schema changed, resync needed"); cfg.setValueBool("resync", true); @@ -428,8 +436,6 @@ int main(string[] args) // Display current application configuration, no application initialisation if (cfg.getValueBool("display_config")){ - string userConfigFilePath = buildNormalizedPath(cfg.configDirName ~ "/config"); - string userSyncList = buildNormalizedPath(cfg.configDirName ~ "/sync_list"); // Display application version writeln("onedrive version = ", strip(import("version"))); @@ -437,11 +443,7 @@ int main(string[] args) writeln("Config path = ", cfg.configDirName); // Does a config file exist or are we using application defaults - if (exists(userConfigFilePath)){ - writeln("Config file found in config path = true"); - } else { - writeln("Config file found in config path = false"); - } + writeln("Config file found in config path = ", exists(configFilePath)); // Config Options writeln("Config option 'check_nosync' = ", cfg.getValueBool("check_nosync")); @@ -461,12 +463,12 @@ int main(string[] args) } // Is sync_list configured? - if (exists(userSyncList)){ + if (exists(syncListFilePath)){ writeln("Config option 'sync_root_files' = ", cfg.getValueBool("sync_root_files")); writeln("Selective sync configured = true"); writeln("sync_list contents:"); // Output the sync_list contents - auto syncListFile = File(userSyncList); + auto syncListFile = File(syncListFilePath); auto range = syncListFile.byLine(); foreach (line; range) {