private async Task RunList() { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (this._save.HasValue()) { var dateString = DateTime.Now.ToString(GeneralUtilities.GetDateFormatString()); var fileNameTemplates = $"{base._names.CommandNames.MetadataTemplates}-{base._names.SubCommandNames.List}-{dateString}"; var fileNameFields = $"{base._names.CommandNames.MetadataTemplateFields}-{base._names.SubCommandNames.List}-{dateString}"; Reporter.WriteInformation("Saving file..."); var saveMetadataTemplates = await boxClient.MetadataManager.GetEnterpriseMetadataAsync(); var saved = base.WriteMetadataTemplateCollectionResultsToReport(saveMetadataTemplates.Entries, fileNameTemplate: fileNameTemplates, fileNameFields: fileNameFields, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } var collection = await boxClient.MetadataManager.GetEnterpriseMetadataAsync(); if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(collection); return; } foreach (var template in collection.Entries) { base.PrintMetadataTemplate(template); } }
protected async Task RunGetItems() { base.CheckForId(this._folderId.Value, this._app); var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (this._save.HasValue()) { Reporter.WriteInformation("Saving file..."); var foldersFileName = $"{base._names.CommandNames.Folders}-{base._names.SubCommandNames.List}-folder-id-{this._folderId.Value}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; var filesFileName = $"{base._names.CommandNames.Files}-{base._names.SubCommandNames.List}-folder-id-{this._folderId.Value}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; var collection = await boxClient.FoldersManager.GetFolderItemsAsync(this._folderId.Value, 1000, autoPaginate : true, fields : base._fields); var folders = collection.Entries.FindAll(x => x.Type == "folder").Cast <BoxFolder>().ToList(); var files = collection.Entries.FindAll(x => x.Type == "file").Cast <BoxFile>().ToList(); var savedFolders = base.WriteListResultsToReport <BoxFolder, BoxFolderMap>(folders, foldersFileName, fileFormat: this._fileFormat.Value(), filePath: this._path.Value()); var savedFiles = base.WriteListResultsToReport <BoxFile, BoxFileMap>(files, filesFileName, fileFormat: this._fileFormat.Value(), filePath: this._path.Value()); if (savedFiles && savedFolders) { Reporter.WriteSuccess("Saved file."); } else { Reporter.WriteError("Couldn't save file."); } return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var result = await boxClient.FoldersManager.GetFolderItemsAsync(this._folderId.Value, 1000, autoPaginate : true, fields : base._fields); base.OutputJson(result); return; } var BoxCollectionsIterators = base.GetIterators(!String.IsNullOrEmpty(base._oneUseToken.Value())); await BoxCollectionsIterators.ListOffsetCollectionToConsole <BoxItem>((offset) => { return(boxClient.FoldersManager.GetFolderItemsAsync(this._folderId.Value, 1000, offset: (int)offset)); }, base.PrintItem); }
public async Task RunList() { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); var BoxCollectionsIterators = base.GetIterators(!String.IsNullOrEmpty(base._oneUseToken.Value())); if (_save.HasValue()) { var fileName = $"{base._names.CommandNames.Webhooks}-{base._names.SubCommandNames.List}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; var webhooks = await boxClient.WebhooksManager.GetWebhooksAsync(autoPaginate : true); var saved = base.WriteMarkerCollectionResultsToReport <BoxWebhook, BoxWebhookMap>(webhooks, fileName, _path.Value(), _fileFormat.Value()); } else if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var webhooks = await boxClient.WebhooksManager.GetWebhooksAsync(autoPaginate : true); base.OutputJson(webhooks); return; } else { await BoxCollectionsIterators.ListMarkerCollectionToConsole <BoxWebhook>((marker) => { return(boxClient.WebhooksManager.GetWebhooksAsync(nextMarker: marker)); }, base.PrintWebhook); } Reporter.WriteInformation("Finished..."); }
private async Task RunUpdate() { if (this._bulkPath.HasValue()) { var json = false; if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { json = true; } await base.UpdateGroupsFromFile(this._bulkPath.Value(), this._save.HasValue(), json : json, overrideSaveFileFormat : this._fileFormat.Value()); return; } base.CheckForValue(this._id.Value, this._app, "A group ID is required for this command"); var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); var groupRequest = new BoxGroupRequest(); if (this._name.HasValue()) { groupRequest.Name = this._name.Value(); } if (this._inviteLevel.HasValue()) { groupRequest.InvitabilityLevel = base.CheckInvitabilityLevel(this._inviteLevel.Value()); } if (this._viewMembershipLevel.HasValue()) { groupRequest.MemberViewabilityLevel = base.CheckViewMembersLevel(this._viewMembershipLevel.Value()); } var updatedGroup = await boxClient.GroupsManager.UpdateAsync(this._id.Value, groupRequest); if (_save.HasValue()) { var fileName = $"{base._names.CommandNames.Groups}-{base._names.SubCommandNames.Update}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); var listWrapper = new List <BoxGroup>(); listWrapper.Add(updatedGroup); var saved = base.WriteListResultsToReport <BoxGroup, BoxGroupMap>(listWrapper, fileName, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(updatedGroup); return; } base.PrintGroup(updatedGroup); }
private async Task RunGetAll() { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (this._save.HasValue()) { var fileName = $"{base._names.CommandNames.Metadata}-{base._names.SubCommandNames.GetAll}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; BoxMetadataTemplateCollection <Dictionary <string, object> > metadataCollectionForReport; var type = ""; if (base._t == BoxType.file) { type = "file"; metadataCollectionForReport = await boxClient.MetadataManager.GetAllFileMetadataTemplatesAsync(_id.Value); } else if (base._t == BoxType.folder) { type = "folder"; metadataCollectionForReport = await boxClient.MetadataManager.GetAllFolderMetadataTemplatesAsync(_id.Value); } else { throw new Exception("This item doesn't currently support metadata."); } var metadataFullObjectCollection = new List <BoxMetadataForCsv>(); foreach (var metadata in metadataCollectionForReport.Entries) { metadataFullObjectCollection.Add(new BoxMetadataForCsv() { ItemId = this._id.Value, ItemType = type, Metadata = metadata }); } base.WriteMetadataCollectionResultsToReport(metadataFullObjectCollection, fileName, fileFormat: this._fileFormat.Value()); return; } base.CheckForId(this._id.Value, _app); BoxMetadataTemplateCollection <Dictionary <string, object> > metadataCollection; if (base._t == BoxType.file) { metadataCollection = await boxClient.MetadataManager.GetAllFileMetadataTemplatesAsync(_id.Value); } else if (base._t == BoxType.folder) { metadataCollection = await boxClient.MetadataManager.GetAllFolderMetadataTemplatesAsync(_id.Value); } else { throw new Exception("This item doesn't currently support metadata."); } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(metadataCollection); return; } base.PrintMetadataCollection(metadataCollection); }
protected async Task BulkDownload(List <BoxBulkDownload> files, string fileName = "") { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); using (var archive = ZipArchive.Create()) { Reporter.WriteInformation("Created zip archive..."); foreach (var file in files) { var fileInfo = await boxClient.FilesManager.GetInformationAsync(file.Id); Reporter.WriteInformation($"Downloading {fileInfo.Name}..."); using (Stream stream = (!string.IsNullOrEmpty(file.VersionId)) ? await boxClient.FilesManager.DownloadStreamAsync(file.Id, file.VersionId) : await boxClient.FilesManager.DownloadStreamAsync(file.Id)) { Reporter.WriteInformation("About to add entry..."); try { var ms = new MemoryStream(); await stream.CopyToAsync(ms); Reporter.WriteInformation("Adding entry..."); archive.AddEntry(fileInfo.Name, ms, true, ms.Length); } catch (Exception e) { Reporter.WriteInformation("Failed."); Reporter.WriteError(e.Message); Reporter.WriteError(e.InnerException.Message); Reporter.WriteError(e.StackTrace); } } } if (string.IsNullOrEmpty(fileName)) { fileName = $"{base._names.CommandNames.Files}-{base._names.SubCommandNames.Download}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; } Reporter.WriteInformation("Processed files.."); var downloadPath = base.ConstructDownloadsPath(fileName); Reporter.WriteInformation($"Saving to ${downloadPath}"); archive.SaveTo($"{downloadPath}.zip", CompressionType.Deflate); Reporter.WriteSuccess("Downloaded files."); } }
private async Task RunList() { base.CheckForValue(this._fileId.Value, this._app, "A file ID is required for this command"); var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); var BoxCollectionsIterators = base.GetIterators(!String.IsNullOrEmpty(base._oneUseToken.Value())); var comments = await boxClient.FilesManager.GetCommentsAsync(this._fileId.Value); if (_save.HasValue()) { var fileName = $"{base._names.CommandNames.Comment}-{base._names.SubCommandNames.List}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; var saved = base.WriteListResultsToReport <BoxComment, BoxCommentMap>(comments.Entries, fileName, _path.Value(), _fileFormat.Value()); Reporter.WriteSuccess($"File saved: {saved}"); } else if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(comments); return; } else { var showNext = ""; while (comments.Entries.Count > 0 && showNext != "q") { showNext = BoxCollectionsIterators.PageInConsole <BoxComment>(base.PrintComment, comments); } } Reporter.WriteInformation("Finished..."); }
private async Task RunDownload() { var fileName = $"{base._names.CommandNames.Folders}-{base._names.SubCommandNames.Download}-{this._folderId.Value}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; if (this._bulkPath.HasValue()) { await base.ProcessFolderBulkDownload(this._bulkPath.Value(), this._asUser.Value(), fileName); return; } var BoxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (this._multiId.HasValue()) { var ids = this._folderId.Value; var idsList = ids.Split(',').ToList(); var fileList = new List <BoxBulkDownload>(); foreach (var id in idsList) { var tempItems = await BoxClient.FoldersManager.GetFolderItemsAsync(id, 1000, autoPaginate : true); var folderFiles = tempItems.Entries.Where(i => i.Type == "file"); foreach (var file in folderFiles) { fileList.Add(new BoxBulkDownload() { Id = file.Id }); } } await base.BulkDownload(fileList, fileName); return; } base.CheckForId(this._folderId.Value, this._app); var items = await BoxClient.FoldersManager.GetFolderItemsAsync(this._folderId.Value, 1000, autoPaginate : true); var files = items.Entries.Where(i => i.Type == "file"); var fileListFromItems = new List <BoxBulkDownload>(); foreach (var file in files) { fileListFromItems.Add(new BoxBulkDownload() { Id = file.Id }); } await base.BulkDownload(fileListFromItems, fileName); }
private async Task RunList() { base.CheckForValue(this._id.Value, this._app, "A group ID or user ID is required for this command"); var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); var BoxCollectionsIterators = base.GetIterators(!String.IsNullOrEmpty(base._oneUseToken.Value())); if (this._listGroups.HasValue()) { if (_save.HasValue()) { var userMemberships = await boxClient.GroupsManager.GetAllGroupMembershipsForUserAsync(this._id.Value, autoPaginate : true); var fileName = $"{base._names.CommandNames.GroupMembership}-{base._names.SubCommandNames.List}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); var saved = base.WriteOffsetCollectionResultsToReport <BoxGroupMembership, BoxGroupMembershipMap>(userMemberships, fileName, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var memberships = await boxClient.GroupsManager.GetAllGroupMembershipsForUserAsync(this._id.Value, autoPaginate : true); base.OutputJson(memberships); return; } await BoxCollectionsIterators.ListOffsetCollectionToConsole <BoxGroupMembership>((offset) => { return(boxClient.GroupsManager.GetAllGroupMembershipsForUserAsync(this._id.Value, offset: (int)offset)); }, base.PrintGroupMember); } else if (this._listCollab.HasValue()) { if (_save.HasValue()) { var membershipCollaborations = await boxClient.GroupsManager.GetCollaborationsForGroupAsync(this._id.Value, autoPaginate : true); var fileName = $"{base._names.CommandNames.GroupMembership}-{base._names.SubCommandNames.List}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); var saved = base.WriteOffsetCollectionResultsToReport <BoxCollaboration, BoxCollaborationMap>(membershipCollaborations, fileName, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var memberships = await boxClient.GroupsManager.GetCollaborationsForGroupAsync(this._id.Value, autoPaginate : true); base.OutputJson(memberships); return; } await BoxCollectionsIterators.ListOffsetCollectionToConsole <BoxCollaboration>((offset) => { return(boxClient.GroupsManager.GetCollaborationsForGroupAsync(this._id.Value, offset: (int)offset)); }, base.PrintCollaboration); } else { if (_save.HasValue()) { var memberships = await boxClient.GroupsManager.GetAllGroupMembershipsForGroupAsync(this._id.Value, autoPaginate : true); var fileName = $"{base._names.CommandNames.GroupMembership}-{base._names.SubCommandNames.List}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); var saved = base.WriteOffsetCollectionResultsToReport <BoxGroupMembership, BoxGroupMembershipMap>(memberships, fileName, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var memberships = await boxClient.GroupsManager.GetAllGroupMembershipsForGroupAsync(this._id.Value, autoPaginate : true); base.OutputJson(memberships); return; } await BoxCollectionsIterators.ListOffsetCollectionToConsole <BoxGroupMembership>((offset) => { return(boxClient.GroupsManager.GetAllGroupMembershipsForGroupAsync(this._id.Value, offset: (int)offset)); }, base.PrintGroupMember); } }
protected async virtual Task CreateMetadataTemplatesFromFile(string filePath, string filePathFields = "", bool save = false, string overrideSavePath = "", string overrideSaveFileFormat = "", bool json = false) { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (!string.IsNullOrEmpty(filePath)) { filePath = GeneralUtilities.TranslatePath(filePath); } if (!string.IsNullOrEmpty(filePathFields)) { filePathFields = GeneralUtilities.TranslatePath(filePathFields); } try { var fileFormat = base.ProcessFileFormatFromPath(filePath); List <BoxMetadataTemplate> templateRequests; if (fileFormat.ToLower() == base._settings.FILE_FORMAT_CSV) { if (string.IsNullOrEmpty(filePathFields)) { throw new Exception("You must use the --bulk-file-path-csv option with metadata templates and provide file paths to CSV files for the tempate and the template fields."); } templateRequests = this.ReadMetadataTemplateCsvFile(filePath, filePathFields); } else if (fileFormat.ToLower() == base._settings.FILE_FORMAT_JSON) { templateRequests = this.ReadMetadataTemplateJsonFile(filePath); } else { throw new Exception($"File format {fileFormat} is not currently supported."); } var saveCreated = new List <BoxMetadataTemplate>(); foreach (var templateRequest in templateRequests) { BoxMetadataTemplate createdTemplate = null; try { createdTemplate = await boxClient.MetadataManager.CreateMetadataTemplate(templateRequest); } catch (Exception e) { Reporter.WriteError("Couldn't create metadata template..."); Reporter.WriteError(e.Message); } if (createdTemplate != null) { this.PrintMetadataTemplate(createdTemplate, json); if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { saveCreated.Add(createdTemplate); } } } if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { var saveFileFormat = base._settings.GetBoxReportsFileFormatSetting(); if (!string.IsNullOrEmpty(overrideSaveFileFormat)) { saveFileFormat = overrideSaveFileFormat; } var savePath = base._settings.GetBoxReportsFolderPath(); if (!string.IsNullOrEmpty(overrideSavePath)) { savePath = overrideSavePath; } var dateString = DateTime.Now.ToString(GeneralUtilities.GetDateFormatString()); var fileName = $"{base._names.CommandNames.MetadataTemplates}-{base._names.SubCommandNames.Create}-{dateString}"; var fileNameFields = $"{base._names.CommandNames.MetadataTemplateFields}-{base._names.SubCommandNames.Create}-{dateString}"; base.WriteMetadataTemplateCollectionResultsToReport(saveCreated, fileName, fileNameFields: fileNameFields, filePathTemplate: savePath, fileFormat: fileFormat); } } catch (Exception e) { Reporter.WriteError(e.Message); } }
protected async virtual Task AddMetadataToItemFromFile(string path, string type = "", bool save = false, string overrideSavePath = "", string overrideSaveFileFormat = "", bool json = false) { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (!string.IsNullOrEmpty(path)) { path = GeneralUtilities.TranslatePath(path); } try { var metadataRequests = this.ReadMetadataCsvFile(path); var saveCreated = new List <BoxMetadataForCsv>(); foreach (var metadataRequest in metadataRequests) { Dictionary <string, object> createdMetadata = null; if (metadataRequest.ItemType != null) { type = metadataRequest.ItemType; } else { throw new Exception("Must have a Box Item type of file or folder"); } try { if (type == "file") { createdMetadata = await boxClient.MetadataManager.CreateFileMetadataAsync(metadataRequest.ItemId, metadataRequest.Metadata, metadataRequest.Scope, metadataRequest.TemplateKey); } else if (type == "folder") { createdMetadata = await boxClient.MetadataManager.CreateFolderMetadataAsync(metadataRequest.ItemId, metadataRequest.Metadata, metadataRequest.Scope, metadataRequest.TemplateKey); } else { throw new Exception("Metadata currently only supported on files and folders."); } } catch (Exception e) { Reporter.WriteError("Couldn't add metadata..."); Reporter.WriteError(e.Message); } if (createdMetadata != null) { this.PrintMetadata(createdMetadata, json); if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { saveCreated.Add(new BoxMetadataForCsv() { TemplateKey = metadataRequest.TemplateKey, Scope = metadataRequest.Scope, ItemId = metadataRequest.ItemId, ItemType = metadataRequest.ItemType, Metadata = createdMetadata }); } } } if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { var fileFormat = base._settings.GetBoxReportsFileFormatSetting(); if (!string.IsNullOrEmpty(overrideSaveFileFormat)) { fileFormat = overrideSaveFileFormat; } var savePath = base._settings.GetBoxReportsFolderPath(); if (!string.IsNullOrEmpty(overrideSavePath)) { savePath = overrideSavePath; } var fileName = $"{base._names.CommandNames.Metadata}-{base._names.SubCommandNames.Create}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; base.WriteMetadataCollectionResultsToReport(saveCreated, fileName, savePath, fileFormat); } } catch (Exception e) { Reporter.WriteError(e.Message); } }
private async Task RunList() { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (_save.HasValue()) { var fileName = $"{base._names.CommandNames.Groups}-{base._names.SubCommandNames.List}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); var saveGroups = await boxClient.GroupsManager.GetAllGroupsAsync(autoPaginate : true); var saved = base.WriteOffsetCollectionResultsToReport <BoxGroup, BoxGroupMap>(saveGroups, fileName, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var saveGroups = await boxClient.GroupsManager.GetAllGroupsAsync(autoPaginate : true); base.OutputJson(saveGroups); return; } var BoxCollectionsIterators = base.GetIterators(!String.IsNullOrEmpty(base._oneUseToken.Value())); await BoxCollectionsIterators.ListOffsetCollectionToConsole <BoxGroup>((offset) => { return(boxClient.GroupsManager.GetAllGroupsAsync(limit: 100, offset: (int)offset)); }, base.PrintGroup); }
protected async virtual Task UpdateFoldersFromFile(string path, bool save = false, string overrideSavePath = "", string overrideSaveFileFormat = "", bool json = false) { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (!string.IsNullOrEmpty(path)) { path = GeneralUtilities.TranslatePath(path); } try { var folderRequests = base.ReadFile <BoxFolderRequest, BoxFolderUpdateRequestMap>(path); List <BoxFolder> saveUpdated = new List <BoxFolder>(); foreach (var folderRequest in folderRequests) { BoxFolder updatedFolder = null; try { updatedFolder = await boxClient.FoldersManager.UpdateInformationAsync(folderRequest); } catch (Exception e) { Reporter.WriteError("Couldn't update folder..."); Reporter.WriteError(e.Message); } if (updatedFolder != null) { this.PrintFolder(updatedFolder, json); if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { saveUpdated.Add(updatedFolder); } } } if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { var fileFormat = base._settings.GetBoxReportsFileFormatSetting(); if (!string.IsNullOrEmpty(overrideSaveFileFormat)) { fileFormat = overrideSaveFileFormat; } var savePath = base._settings.GetBoxReportsFolderPath(); if (!string.IsNullOrEmpty(overrideSavePath)) { savePath = overrideSavePath; } var fileName = $"{base._names.CommandNames.Folders}-{base._names.SubCommandNames.Update}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; base.WriteListResultsToReport <BoxFolder, BoxFolderMap>(saveUpdated, fileName, savePath, fileFormat); } } catch (Exception e) { Reporter.WriteError(e.Message); } }
protected async Task CreateGroupsFromFile(string path, bool save = false, string overrideSavePath = "", string overrideSaveFileFormat = "", bool json = false) { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (!string.IsNullOrEmpty(path)) { path = GeneralUtilities.TranslatePath(path); } try { var groupRequests = base.ReadFile <BoxGroupRequest, BoxGroupRequestMap>(path); List <BoxGroup> saveCreated = new List <BoxGroup>(); foreach (var groupRequest in groupRequests) { BoxGroup createdGroup = null; try { createdGroup = await boxClient.GroupsManager.CreateAsync(groupRequest); } catch (Exception e) { Reporter.WriteError("Couldn't create group..."); Reporter.WriteError(e.Message); } if (createdGroup != null) { this.PrintGroup(createdGroup, json); if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { saveCreated.Add(createdGroup); } } } if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { var fileFormat = base._settings.GetBoxReportsFileFormatSetting(); if (!string.IsNullOrEmpty(overrideSaveFileFormat)) { fileFormat = overrideSaveFileFormat; } var savePath = base._settings.GetBoxReportsFolderPath(); if (!string.IsNullOrEmpty(overrideSavePath)) { savePath = overrideSavePath; } var fileName = $"{base._names.CommandNames.Groups}-{base._names.SubCommandNames.Create}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; base.WriteListResultsToReport <BoxGroup, BoxGroupMap>(saveCreated, fileName, savePath, fileFormat); } } catch (Exception e) { Reporter.WriteError(e.Message); } }
private async Task RunListGroups() { base.CheckForValue(this._id.Value, this._app, "A group ID is required for this command."); var fields = base.ProcessFields(this._fieldsOption.Value(), CollaborationSubCommandBase._fields); var BoxCollectionsIterators = base.GetIterators(!String.IsNullOrEmpty(base._oneUseToken.Value())); var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (_save.HasValue()) { BoxCollection <BoxCollaboration> collabs; var fileName = $"{base._names.CommandNames.Collaborations}-{base._names.SubCommandNames.GetPending}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); collabs = await boxClient.GroupsManager.GetCollaborationsForGroupAsync(this._id.Value, autoPaginate : true, fields : fields); var saved = base.WriteOffsetCollectionResultsToReport <BoxCollaboration, BoxCollaborationMap>(collabs, fileName, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var result = await boxClient.GroupsManager.GetCollaborationsForGroupAsync(this._id.Value, autoPaginate : true, fields : fields); base.OutputJson(result); return; } int limit = -1; if (this._limit.HasValue()) { try { limit = Convert.ToInt32(this._limit.Value()); } catch { } } await BoxCollectionsIterators.ListOffsetCollectionToConsole <BoxCollaboration>((offset) => { return(boxClient.GroupsManager.GetCollaborationsForGroupAsync(this._id.Value, offset: (int)offset, fields: fields)); }, base.PrintCollaboration, limit); }
public async Task RunList() { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); var BoxCollectionsIterators = base.GetIterators(!String.IsNullOrEmpty(base._oneUseToken.Value())); var fileName = $"{base._names.CommandNames.Users}-{base._names.SubCommandNames.List}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; var fields = base.ProcessFields(this._fieldsOption.Value(), base._fields); if (this._save.HasValue()) { Reporter.WriteInformation("Saving file..."); BoxCollection <BoxUser> users; if (this._appUsers.HasValue()) { users = await boxClient.UsersManager.GetEnterpriseUsersAsync(fields : fields, autoPaginate : true, filterTerm : "AppUser"); } else if (this._managedUsers.HasValue()) { users = await boxClient.UsersManager.GetEnterpriseUsersAsync(fields : fields, autoPaginate : true); users.Entries.RemoveAll(user => { return(user.Login.Contains("AppUser")); }); users.TotalCount = users.Entries.Count; } else { users = await boxClient.UsersManager.GetEnterpriseUsersAsync(fields : fields, autoPaginate : true); } var saved = base.WriteOffsetCollectionResultsToReport <BoxUser, BoxUserMap>(users, fileName, this._path.Value(), this._fileFormat.Value()); if (saved) { Reporter.WriteSuccess("File saved."); } else { Reporter.WriteError("Error while saving file."); } return; } if (this._managedUsers.HasValue()) { var users = await boxClient.UsersManager.GetEnterpriseUsersAsync(fields : fields, autoPaginate : true); users.Entries.RemoveAll(user => { return(user.Login.Contains("AppUser")); }); users.TotalCount = users.Entries.Count; if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(users); return; } var showNext = ""; while (users.Entries.Count > 0 && showNext != "q") { showNext = BoxCollectionsIterators.PageInConsole <BoxUser>(PrintUserInfo, users); } Reporter.WriteInformation("Finished..."); return; } else if (this._appUsers.HasValue()) { var users = await boxClient.UsersManager.GetEnterpriseUsersAsync(fields : fields, autoPaginate : true, filterTerm : "AppUser"); if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(users); return; } var showNext = ""; while (users.Entries.Count > 0 && showNext != "q") { showNext = BoxCollectionsIterators.PageInConsole <BoxUser>(PrintUserInfo, users); } Reporter.WriteInformation("Finished..."); return; } else { if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var users = await boxClient.UsersManager.GetEnterpriseUsersAsync(fields : fields, autoPaginate : true); base.OutputJson(users); return; } int limit = -1; if (this._limit.HasValue()) { try { limit = Convert.ToInt32(this._limit.Value()); } catch { } } await BoxCollectionsIterators.ListOffsetCollectionToConsole <BoxUser>((offset) => { return(boxClient.UsersManager.GetEnterpriseUsersAsync(offset: offset)); }, PrintUserInfo, limit); } Reporter.WriteInformation("Finished..."); }
protected async Task ProcessAddCollaborationsFromFile(string path, BoxType t, string command, List <string> fields, bool save = false, string overrideSavePath = "", string overrideSaveFileFormat = "", bool json = false) { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); var processedCollaborations = new List <BoxCollaboration>(); if (!string.IsNullOrEmpty(path)) { path = GeneralUtilities.TranslatePath(path); } try { var collaborationRequests = new List <BoxCollaborationRequest>(); if (command.ToLower() == CollaborationAddCommand.commandName) { collaborationRequests = base.ReadFile <BoxCollaborationRequest, BoxCollaborationRequestMap>(path); } else if (command.ToLower() == CollaborationUpdateCommand.commandName) { collaborationRequests = base.ReadFile <BoxCollaborationRequest, BoxCollaborationUpdateRequestMap>(path); } else { throw new Exception("Cannot use bulk-file-path with this command."); } foreach (var collaborationRequest in collaborationRequests) { try { var createdCollaboration = new BoxCollaboration(); if (command.ToLower() == CollaborationAddCommand.commandName) { Reporter.WriteInformation($"Processing collaboration for item ID: {collaborationRequest.Item.Id}"); Reporter.WriteInformation($"Processing collaboration for item type: {collaborationRequest.Item.Type}"); createdCollaboration = await boxClient.CollaborationsManager.AddCollaborationAsync(collaborationRequest, fields : fields); } else if (command.ToLower() == CollaborationUpdateCommand.commandName) { Reporter.WriteInformation($"Processing update on collaboration ID: {collaborationRequest.Id}"); createdCollaboration = await boxClient.CollaborationsManager.EditCollaborationAsync(collaborationRequest, fields : fields); } else { throw new Exception("Cannot use bulk-file-path with this command."); } if (createdCollaboration != null) { this.PrintCollaboration(createdCollaboration, json); if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { processedCollaborations.Add(createdCollaboration); } } } catch (BoxException e) { var errorMessage = JObject.Parse(e.Message); var code = errorMessage.GetValue("code").ToString(); if (code == "user_already_collaborator" || code == "conflict") { Reporter.WriteWarning("Collaboration already exists..."); var existingCollaborations = new BoxCollection <BoxCollaboration>(); var itemId = collaborationRequest.Item.Id; if (collaborationRequest.Item.Type == BoxType.folder) { existingCollaborations = await boxClient.FoldersManager.GetCollaborationsAsync(itemId, fields : fields); } else if (collaborationRequest.Item.Type == BoxType.file) { existingCollaborations = await boxClient.FilesManager.GetCollaborationsAsync(itemId, fields : fields); } else { throw e; } var existingCollab = new BoxCollaboration(); if (!string.IsNullOrEmpty(collaborationRequest.AccessibleBy.Id)) { existingCollab = existingCollaborations.Entries.Find((collaboration) => { return(collaboration.AccessibleBy.Id == collaborationRequest.AccessibleBy.Id); }); } else if (!string.IsNullOrEmpty(collaborationRequest.AccessibleBy.Login)) { var user = (await boxClient.UsersManager.GetEnterpriseUsersAsync(filterTerm: collaborationRequest.AccessibleBy.Login)).Entries.FirstOrDefault(); if (user != null) { existingCollab = existingCollaborations.Entries.Find((collaboration) => { return(collaboration.AccessibleBy.Id == user.Id); }); } else { throw e; } } if (existingCollab != null) { processedCollaborations.Add(existingCollab); } else { throw e; } } else { Reporter.WriteError("Failed processing on collaboration:"); if (!String.IsNullOrEmpty(collaborationRequest.Id)) { Reporter.WriteError($"Collaboration ID: {collaborationRequest.Id}"); } Reporter.WriteError($"Role: {collaborationRequest.Role}"); if (!String.IsNullOrEmpty(collaborationRequest.Status)) { Reporter.WriteError($"Status: {collaborationRequest.Status}"); } if (collaborationRequest.Item != null) { Reporter.WriteError($"Item ID: {collaborationRequest.Item.Id}"); Reporter.WriteError($"Item type: {collaborationRequest.Item.Type}"); } if (collaborationRequest.AccessibleBy != null) { Reporter.WriteError($"Accessible by ID: {collaborationRequest.AccessibleBy.Id}"); Reporter.WriteError($"Accessible by type: {collaborationRequest.AccessibleBy.Type}"); } Reporter.WriteError("Reason:"); Reporter.WriteError(e.Message); } } catch (Exception e) { Reporter.WriteError("Failed processing on collaboration:"); if (!String.IsNullOrEmpty(collaborationRequest.Id)) { Reporter.WriteError($"Collaboration ID: {collaborationRequest.Id}"); } Reporter.WriteError($"Role: {collaborationRequest.Role}"); if (!String.IsNullOrEmpty(collaborationRequest.Status)) { Reporter.WriteError($"Status: {collaborationRequest.Status}"); } if (collaborationRequest.Item != null) { Reporter.WriteError($"Item ID: {collaborationRequest.Item.Id}"); Reporter.WriteError($"Item type: {collaborationRequest.Item.Type}"); } if (collaborationRequest.AccessibleBy != null) { Reporter.WriteError($"Accessible by ID: {collaborationRequest.AccessibleBy.Id}"); Reporter.WriteError($"Accessible by type: {collaborationRequest.AccessibleBy.Type}"); } Reporter.WriteError("Reason:"); Reporter.WriteError(e.Message); } } } catch (Exception e) { Reporter.WriteError(e.Message); } if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { var fileFormat = base._settings.GetBoxReportsFileFormatSetting(); if (!string.IsNullOrEmpty(overrideSaveFileFormat)) { fileFormat = overrideSaveFileFormat; } var savePath = base._settings.GetBoxReportsFolderPath(); if (!string.IsNullOrEmpty(overrideSavePath)) { savePath = overrideSavePath; } var fileName = String.Empty; if (command.ToLower() == CollaborationAddCommand.commandName) { fileName = $"{base._names.CommandNames.Collaborations}-{base._names.SubCommandNames.Add}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; } else if (command.ToLower() == CollaborationUpdateCommand.commandName) { fileName = $"{base._names.CommandNames.Collaborations}-{base._names.SubCommandNames.Update}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; } if (!String.IsNullOrEmpty(fileName)) { base.WriteListResultsToReport <BoxCollaboration, BoxCollaborationMap>(processedCollaborations, fileName, savePath, fileFormat); } } }
private async Task RunGet() { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); var BoxCollectionsIterators = base.GetIterators(!String.IsNullOrEmpty(base._oneUseToken.Value())); if (this._enterprise.HasValue()) { var createdBefore = DateTime.Now; var createdAfter = DateTime.Now.AddDays(-5); if (this._createdBefore.HasValue()) { try { createdBefore = GeneralUtilities.GetDateTimeFromString(this._createdBefore.Value(), true); } catch { createdBefore = DateTime.Parse(this._createdBefore.Value()); } Reporter.WriteInformation(createdBefore.ToString()); } if (this._createdAfter.HasValue()) { try { createdAfter = GeneralUtilities.GetDateTimeFromString(this._createdAfter.Value(), true); } catch { createdAfter = DateTime.Parse(this._createdAfter.Value()); } } if (this._save.HasValue()) { var fileName = $"{base._names.CommandNames.Events}-enterprise-{base._names.SubCommandNames.Get}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); var events = await BoxCollectionsIterators.ReturnAllEvents((position) => { return boxClient.EventsManager.EnterpriseEventsAsync(limit: 1000, createdAfter: createdAfter, createdBefore: createdBefore, streamPosition: position); }); base.WriteEventListResultsToReport(events.Entries, fileName, _path.Value(), _fileFormat.Value()); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var events = await BoxCollectionsIterators.ReturnAllEvents((position) => { return boxClient.EventsManager.EnterpriseEventsAsync(limit: 1000, createdAfter: createdAfter, createdBefore: createdBefore, streamPosition: position); }); base.OutputJson(events); return; } await BoxCollectionsIterators.ListEventCollectionToConsole((position) => { return boxClient.EventsManager.EnterpriseEventsAsync(createdAfter: createdAfter, createdBefore: createdBefore, streamPosition: position); }, base.PrintEvent); } else { if (this._save.HasValue()) { var fileName = $"{base._names.CommandNames.Events}-user-{base._names.SubCommandNames.Get}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); var events = await BoxCollectionsIterators.ReturnAllEvents((position) => { return boxClient.EventsManager.UserEventsAsync(limit: 1000, streamPosition: position); }); base.WriteEventListResultsToReport(events.Entries, fileName, _path.Value(), _fileFormat.Value()); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { var events = await BoxCollectionsIterators.ReturnAllEvents((position) => { return boxClient.EventsManager.UserEventsAsync(limit: 1000, streamPosition: position); }); base.OutputJson(events); return; } await BoxCollectionsIterators.ListEventCollectionToConsole((position) => { return boxClient.EventsManager.UserEventsAsync(streamPosition: position); }, base.PrintEvent); } }
private async Task RunGetPending() { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); var collabs = await boxClient.CollaborationsManager.GetPendingCollaborationAsync(); if (_save.HasValue()) { var fileName = $"{base._names.CommandNames.Collaborations}-{base._names.SubCommandNames.GetPending}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); var saved = base.WriteOffsetCollectionResultsToReport <BoxCollaboration, BoxCollaborationMap>(collabs, fileName, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(collabs); return; } base.PrintCollaborations(collabs); }
private async Task RunList() { base.CheckForValue(this._id.Value, this._app, "An ID is required for this command."); var fields = base.ProcessFields(this._fieldsOption.Value(), CollaborationSubCommandBase._fields); var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (_save.HasValue()) { var fileName = $"{base._names.CommandNames.Collaborations}-{base._names.SubCommandNames.List}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; Reporter.WriteInformation("Saving file..."); BoxCollection <BoxCollaboration> saveCollabs; if (base._t == BoxType.file) { saveCollabs = await boxClient.FilesManager.GetCollaborationsAsync(this._id.Value, fields : fields); } else if (base._t == BoxType.folder) { saveCollabs = await boxClient.FoldersManager.GetCollaborationsAsync(this._id.Value, fields : fields); } else { throw new Exception("This item doesn't currently support collaborations."); } var saved = base.WriteOffsetCollectionResultsToReport <BoxCollaboration, BoxCollaborationMap>(saveCollabs, fileName, fileFormat: this._fileFormat.Value()); Reporter.WriteInformation($"File saved: {saved}"); return; } BoxCollection <BoxCollaboration> collabs; if (base._t == BoxType.file) { collabs = await boxClient.FilesManager.GetCollaborationsAsync(this._id.Value, fields : fields); } else if (base._t == BoxType.folder) { collabs = await boxClient.FoldersManager.GetCollaborationsAsync(this._id.Value, fields : fields); } else { throw new Exception("This item doesn't currently support collaborations."); } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(collabs); return; } base.PrintCollaborations(collabs); }
private async Task AssignStoragePoliciesFromFile(string path, bool save = false, string overrideSavePath = "", string overrideSaveFileFormat = "", bool json = false) { var boxClient = base.ConfigureBoxClient(oneCallAsUserId: this._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); if (!string.IsNullOrEmpty(path)) { path = GeneralUtilities.TranslatePath(path); } try { var assignmentRequests = base.ReadFile <BoxStoragePolicyAssignmentRequest, BoxStoragePolicyAssignmentRequestMap>(path); var saveCreated = new List <BoxStoragePolicyAssignment>(); foreach (var assignmentRequest in assignmentRequests) { BoxStoragePolicyAssignment newAssignment = null; try { newAssignment = await boxClient.StoragePoliciesManager.AssignAsync(assignmentRequest.UserId, assignmentRequest.StoragePolicyId); } catch (Exception e) { Reporter.WriteError($"Couldn't assign storage policy {assignmentRequest.StoragePolicyId} to user {assignmentRequest.UserId}..."); Reporter.WriteError(e.Message); } if (newAssignment != null) { base.PrintStoragePolicyAssignment(newAssignment, json); if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { saveCreated.Add(newAssignment); } } } if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) { var fileFormat = base._settings.GetBoxReportsFileFormatSetting(); if (!string.IsNullOrEmpty(overrideSaveFileFormat)) { fileFormat = overrideSaveFileFormat; } var savePath = base._settings.GetBoxReportsFolderPath(); if (!string.IsNullOrEmpty(overrideSavePath)) { savePath = overrideSavePath; } var fileName = $"{base._names.CommandNames.StoragePolicy}-{base._names.SubCommandNames.Assign}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; base.WriteListResultsToReport <BoxStoragePolicyAssignment, BoxStoragePolicyAssignmentMap>(saveCreated, fileName, savePath, fileFormat); } } catch (Exception e) { Reporter.WriteError(e.Message); } }