Skip to content

Commit

Permalink
Implement --display-quota
Browse files Browse the repository at this point in the history
* Implement --display-quota
  • Loading branch information
abraunegg committed Oct 19, 2023
1 parent d7ea797 commit 77c294b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/application-config-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Before reading this document, please ensure you are running application version
- [CLI Option: --destination-directory](#cli-option---destination-directory)
- [CLI Option: --display-config](#cli-option---display-config)
- [CLI Option: --display-sync-status](#cli-option---display-sync-status)
- [CLI Option: --display-quota](#cli-option---display-quota)
- [CLI Option: --force](#cli-option---force)
- [CLI Option: --force-sync](#cli-option---force-sync)
- [CLI Option: --get-file-link](#cli-option---get-file-link)
Expand Down Expand Up @@ -947,6 +948,11 @@ _**Usage Example:**_ `onedrive --display-sync-status`

_**Additional Usage Notes:**_ This option can also use the `--single-directory` option to determine the sync status of a specific directory within the configured 'sync_dir'

### CLI Option: ---display-quota
_**Description:**_ This CLI option will display the quota status of the account drive id or the configured 'drive_id' value

_**Usage Example:**_ `onedrive --display-quota`

### CLI Option: --force
_**Description:**_ This CLI option enables the force the deletion of data when a 'big delete' is detected.

Expand Down
16 changes: 16 additions & 0 deletions src/config.d
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ class ApplicationConfig {
stringValues["auth_response"] = "";
boolValues["display_config"] = false;
boolValues["display_sync_status"] = false;
boolValues["display_quota"] = false;
boolValues["print_token"] = false;
boolValues["logout"] = false;
boolValues["reauth"] = false;
Expand Down Expand Up @@ -1044,6 +1045,9 @@ class ApplicationConfig {
"display-sync-status",
"Display the sync status of the client - no sync will be performed.",
&boolValues["display_sync_status"],
"display-quota",
"Display the quota status of the client - no sync will be performed.",
&boolValues["display_quota"],
"download-only",
"Replicate the OneDrive online state locally, by only downloading changes from OneDrive. Do not upload local changes to OneDrive.",
&boolValues["download_only"],
Expand Down Expand Up @@ -2006,6 +2010,18 @@ class ApplicationConfig {
operationalConflictDetected = true;
}

// --monitor and --display-quota cannot be used together
if ((getValueBool("monitor")) && (getValueBool("display_quota"))) {
log.error("ERROR: --monitor and --display-quota cannot be used together");
operationalConflictDetected = true;
}

// --sync and and --display-quota cannot be used together
if ((getValueBool("synchronize")) && (getValueBool("display_quota"))) {
log.error("ERROR: --sync and and --display-quota cannot be used together");
operationalConflictDetected = true;
}

// --force-sync can only be used when using --sync --single-directory
if (getValueBool("force_sync")) {

Expand Down
10 changes: 10 additions & 0 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ int main(string[] cliArgs) {
// - Are we just creating a directory online, without any sync being performed?
// - Are we just deleting a directory online, without any sync being performed?
// - Are we renaming or moving a directory?
// - Are we displaying the quota information?

// --get-sharepoint-drive-id - Get the SharePoint Library drive_id
if (appConfig.getValueString("sharepoint_library_name") != "") {
Expand Down Expand Up @@ -488,6 +489,15 @@ int main(string[] cliArgs) {
return EXIT_SUCCESS;
}

// Are we displaying the quota information?
if (appConfig.getValueBool("display_quota")) {
// Query and respond with the quota details
syncEngineInstance.queryOneDriveForQuotaDetails();
// Exit application
// Use exit scopes to shutdown API
return EXIT_SUCCESS;
}

// If we get to this point, we have not performed a 'no-sync' task ..
log.error("\nYour command line input is missing either the '--sync' or '--monitor' switches. Please include one (but not both) of these switches in your command line, or refer to 'onedrive --help' for additional guidance.\n");
log.error("It is important to note that you must include one of these two arguments in your command line for the application to perform a synchronisation with Microsoft OneDrive\n");
Expand Down
81 changes: 81 additions & 0 deletions src/sync.d
Original file line number Diff line number Diff line change
Expand Up @@ -7017,4 +7017,85 @@ class SyncEngine {
log.error("Selected path not found on local system: ", inputFilePath);
}
}

// Query OneDrive for the quota details
void queryOneDriveForQuotaDetails() {
// This function is similar to getRemainingFreeSpace() but is different in data being analysed and output method

JSONValue currentDriveQuota;
string driveId;

if (appConfig.getValueString("drive_id").length) {
driveId = appConfig.getValueString("drive_id");
} else {
driveId = appConfig.defaultDriveId;
}

try {
// Create a new OneDrive API instance
OneDriveApi getCurrentDriveQuotaApiInstance;
getCurrentDriveQuotaApiInstance = new OneDriveApi(appConfig);
getCurrentDriveQuotaApiInstance.initialise();
log.vdebug("Seeking available quota for this drive id: ", driveId);
currentDriveQuota = getCurrentDriveQuotaApiInstance.getDriveQuota(driveId);
// Shut this API instance down
getCurrentDriveQuotaApiInstance.shutdown();
// Free object and memory
object.destroy(getCurrentDriveQuotaApiInstance);
} catch (OneDriveException e) {
log.vdebug("currentDriveQuota = onedrive.getDriveQuota(driveId) generated a OneDriveException");
}

// validate that currentDriveQuota is a JSON value
if (currentDriveQuota.type() == JSONType.object) {
// was 'quota' in response?
if ("quota" in currentDriveQuota) {

// debug output of response
log.vdebug("currentDriveQuota: ", currentDriveQuota);

// human readable output of response
string deletedValue = "Not Provided";
string remainingValue = "Not Provided";
string stateValue = "Not Provided";
string totalValue = "Not Provided";
string usedValue = "Not Provided";

// Update values
if ("deleted" in currentDriveQuota["quota"]) {
deletedValue = to!string(currentDriveQuota["quota"]["deleted"].integer);
}

if ("remaining" in currentDriveQuota["quota"]) {
remainingValue = to!string(currentDriveQuota["quota"]["remaining"].integer);
}

if ("state" in currentDriveQuota["quota"]) {
stateValue = currentDriveQuota["quota"]["state"].str;
}

if ("total" in currentDriveQuota["quota"]) {
totalValue = to!string(currentDriveQuota["quota"]["total"].integer);
}

if ("used" in currentDriveQuota["quota"]) {
usedValue = to!string(currentDriveQuota["quota"]["used"].integer);
}

writeln("Microsoft OneDrive quota information as reported for this Drive ID: ", driveId);
writeln();
writeln("Deleted: ", deletedValue);
writeln("Remaining: ", remainingValue);
writeln("State: ", stateValue);
writeln("Total: ", totalValue);
writeln("Used: ", usedValue);
writeln();
writeln("The numeric values above are expressed in bytes");

} else {
writeln("Microsoft OneDrive quota information is being restricted for this Drive ID: ", driveId);
}

}
}
}

0 comments on commit 77c294b

Please sign in to comment.