From b1fcb814c37f284de4188f5df7c3d840f4ecd810 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Sun, 15 Mar 2020 06:29:44 +1100 Subject: [PATCH] Update application output when just authorising application (Issue #820) (#821) * Update application output to be clearer when just authorising the application and --synchronize or --monitor not passed in * Update usage.md with updated authorize details and example --- docs/USAGE.md | 20 +++++++++++++++++--- src/config.d | 5 ++++- src/main.d | 27 +++++++++++++++++++-------- src/onedrive.d | 1 + 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/docs/USAGE.md b/docs/USAGE.md index b0e0190e..fbe76673 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -29,10 +29,10 @@ The application will attempt to handle instances where you have two files with t ### curl compatibility If your system utilises curl >= 7.62.0 curl defaults to prefer HTTP/2 over HTTP/1.1 by default. If you wish to use HTTP/2 for some operations you will need to use the `--force-http-2` config option to enable otherwise all operations will use HTTP/1.1. -### First run :zap: -After installing the application you must run it at least once from the terminal to authorize it. +### Authorize the application with your OneDrive Account +After installing the application you must authorize the application with your OneDrive Account. This is done by running the application without any additional command switches. -You will be asked to open a specific link using your web browser where you will have to login into your Microsoft Account and give the application the permission to access your files. After giving the permission, you will be redirected to a blank page. Copy the URI of the blank page into the application. +You will be asked to open a specific URL by using your web browser where you will have to login into your Microsoft Account and give the application the permission to access your files. After giving permission to the application, you will be redirected to a blank page. Copy the URI of the blank page into the application. ```text [user@hostname ~]$ onedrive @@ -44,6 +44,20 @@ Enter the response uri: ``` +**Example:** +``` +[user@hostname ~]$ onedrive +Authorize this app visiting: + +https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=22c49a0d-d21c-4792-aed1-8f163c982546&scope=Files.ReadWrite%20Files.ReadWrite.all%20Sites.ReadWrite.All%20offline_access&response_type=code&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient + +Enter the response uri: https://login.microsoftonline.com/common/oauth2/nativeclient?code= + +Application has been successfully authorised, however no additional command switches were provided. + +Please use --help for further assistance in regards to running this application. +``` + ### Show your configuration To validate your configuration the application will use, utilize the following: ```text diff --git a/src/config.d b/src/config.d index 83f85ef4..fdbfbaef 100644 --- a/src/config.d +++ b/src/config.d @@ -20,6 +20,8 @@ final class Config public string configFileSyncDir; public string configFileSkipFile; public string configFileSkipDir; + // was the application just authorised - paste of response uri + public bool applicationAuthorizeResponseUri = false; private string userConfigFilePath; // hashmap for the values found in the user config file @@ -31,7 +33,8 @@ final class Config this(string confdirOption) { - // default configuration + // default configuration - entries in config file ~/.config/onedrive/config + // an entry here means it can be set via the config file if there is a coresponding read and set in update_from_args() stringValues["sync_dir"] = defaultSyncDir; stringValues["skip_file"] = defaultSkipFile; stringValues["skip_dir"] = defaultSkipDir; diff --git a/src/main.d b/src/main.d index 1758fd75..612d0693 100644 --- a/src/main.d +++ b/src/main.d @@ -413,7 +413,8 @@ int main(string[] args) log.log("NOTE: The use of --force-http-1.1 is depreciated"); } - log.vlog("Initializing the OneDrive API ..."); + // Test if OneDrive service can be reached + log.vdebug("Testing network to ensure network connectivity to Microsoft OneDrive Service"); try { online = testNetwork(); } catch (CurlException e) { @@ -424,8 +425,9 @@ int main(string[] args) return EXIT_FAILURE; } } - + // Initialize OneDrive, check for authorization + log.vlog("Initializing the OneDrive API ..."); oneDrive = new OneDriveApi(cfg); oneDrive.printAccessToken = cfg.getValueBool("print_token"); if (!oneDrive.init()) { @@ -435,9 +437,8 @@ int main(string[] args) return EXIT_UNAUTHORIZED; } - // if --synchronize or --monitor not passed in, exit & display help + // if --synchronize or --monitor not passed in, configure the flag to display help & exit auto performSyncOK = false; - if (cfg.getValueBool("synchronize") || cfg.getValueBool("monitor")) { performSyncOK = true; } @@ -449,10 +450,20 @@ int main(string[] args) } if (!performSyncOK) { - writeln("\n--synchronize or --monitor missing from your command options or use --help for further assistance\n"); - writeln("No OneDrive sync will be performed without either of these two arguments being present\n"); - oneDrive.http.shutdown(); - return EXIT_FAILURE; + // was the application just authorised? + if (cfg.applicationAuthorizeResponseUri) { + // Application was just authorised + log.log("\nApplication has been successfully authorised, however no additional command switches were provided.\n"); + log.log("Please use --help for further assistance in regards to running this application.\n"); + oneDrive.http.shutdown(); + return EXIT_SUCCESS; + } else { + // Application was not just authorised + log.log("\n--synchronize or --monitor switches missing from your command line input. Please add one (not both) of these switches to your command line or use --help for further assistance.\n"); + log.log("No OneDrive sync will be performed without one of these two arguments being present.\n"); + oneDrive.http.shutdown(); + return EXIT_FAILURE; + } } // if --synchronize && --monitor passed in, exit & display help as these conflict with each other diff --git a/src/onedrive.d b/src/onedrive.d index fd383fa8..e2fe18e4 100644 --- a/src/onedrive.d +++ b/src/onedrive.d @@ -180,6 +180,7 @@ final class OneDriveApi log.log("Authorize this app visiting:\n"); write(url, "\n\n", "Enter the response uri: "); readln(response); + cfg.applicationAuthorizeResponseUri = true; } else { string[] authFiles = authFilesString.split(":"); string authUrl = authFiles[0];