diff --git a/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipMap.cs b/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipMap.cs new file mode 100644 index 00000000..00f2f8ad --- /dev/null +++ b/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipMap.cs @@ -0,0 +1,19 @@ +using System; +using Box.V2.Models; +using CsvHelper.Configuration; + +namespace BoxCLI.CommandUtilities.CsvModels +{ + public class BoxGroupMembershipMap : CsvClassMap + { + public BoxGroupMembershipMap() + { + Map(m => m.Id); + References(m => m.User); + References(m => m.Group); + Map(m => m.CreatedAt); + Map(m => m.ModifiedAt); + Map(m => m.Role); + } + } +} diff --git a/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipRequestMap.cs b/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipRequestMap.cs new file mode 100644 index 00000000..9c62d5dc --- /dev/null +++ b/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipRequestMap.cs @@ -0,0 +1,45 @@ +using System; +using Box.V2.Models; +using Box.V2.Models.Request; +using CsvHelper.Configuration; + +namespace BoxCLI.CommandUtilities.CsvModels +{ + public class BoxGroupMembershipRequestMap : CsvClassMap + { + public BoxGroupMembershipRequestMap() + { + Map(m => m.Role); + Map(m => m.User).ConvertUsing(row => + { + var field = row.GetField("BoxUserId"); + if (string.IsNullOrEmpty(field)) + { + return null; + } + else + { + return new BoxRequestEntity() + { + Id = field + }; + } + }); + Map(m => m.Group).ConvertUsing(row => + { + var field = row.GetField("BoxGroupId"); + if (string.IsNullOrEmpty(field)) + { + return null; + } + else + { + return new BoxGroupRequest() + { + Id = field + }; + } + }); + } + } +} diff --git a/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipUpdateRequestMap.cs b/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipUpdateRequestMap.cs new file mode 100644 index 00000000..d537929f --- /dev/null +++ b/BoxCLI/CommandUtilities/CsvModels/BoxGroupMembershipUpdateRequestMap.cs @@ -0,0 +1,16 @@ +using System; +using Box.V2.Models; +using Box.V2.Models.Request; +using CsvHelper.Configuration; + +namespace BoxCLI.CommandUtilities.CsvModels +{ + public class BoxGroupMembershipUpdateRequestMap : CsvClassMap + { + public BoxGroupMembershipUpdateRequestMap() + { + Map(m => m.Id); + Map(m => m.Role); + } + } +} diff --git a/BoxCLI/CommandUtilities/CsvModels/BoxGroupUpdateRequestMap.cs b/BoxCLI/CommandUtilities/CsvModels/BoxGroupUpdateRequestMap.cs new file mode 100644 index 00000000..26d456ef --- /dev/null +++ b/BoxCLI/CommandUtilities/CsvModels/BoxGroupUpdateRequestMap.cs @@ -0,0 +1,17 @@ +using System; +using Box.V2.Models.Request; +using CsvHelper.Configuration; + +namespace BoxCLI.CommandUtilities.CsvModels +{ + public class BoxGroupUpdateRequestMap : CsvClassMap + { + public BoxGroupUpdateRequestMap() + { + Map(m => m.Id); + Map(m => m.Name).Default(""); + Map(m => m.InvitabilityLevel).Default(""); + Map(m => m.MemberViewabilityLevel).Default(""); + } + } +} diff --git a/BoxCLI/Commands/GroupSubCommands/GroupCreateCommand.cs b/BoxCLI/Commands/GroupSubCommands/GroupCreateCommand.cs index 2fe65b01..241f45b6 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupCreateCommand.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupCreateCommand.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Box.V2.Models; using Box.V2.Models.Request; @@ -6,6 +7,7 @@ using BoxCLI.BoxPlatform.Service; using BoxCLI.CommandUtilities; using BoxCLI.CommandUtilities.CommandOptions; +using BoxCLI.CommandUtilities.CsvModels; using BoxCLI.CommandUtilities.Globalization; using Microsoft.Extensions.CommandLineUtils; @@ -19,6 +21,7 @@ public class GroupCreateCommand : GroupSubCommandBase private CommandOption _save; private CommandOption _bulkPath; private CommandOption _idOnly; + private CommandOption _fileFormat; private CommandLineApplication _app; private IBoxHome _home; @@ -34,6 +37,7 @@ public override void Configure(CommandLineApplication command) _name = command.Argument("name", "Group name"); _bulkPath = BulkFilePathOption.ConfigureOption(command); _save = SaveOption.ConfigureOption(command); + _fileFormat = FileFormatOption.ConfigureOption(command); _inviteLevel = command.Option("-i|--invite", "Specifies who can invite the group to collaborate. Enter admins_only, admins_and_members, or all_managed_users", CommandOptionType.SingleValue); @@ -64,7 +68,8 @@ private async Task RunCreate() { json = true; } - await base.CreateGroupsFromFile(this._bulkPath.Value(), this._save.HasValue(), json: json); + await base.CreateGroupsFromFile(this._bulkPath.Value(), this._save.HasValue(), + json: json, overrideSaveFileFormat: this._fileFormat.Value()); return; } base.CheckForValue(this._name.Value, this._app, "A group name is required for this command"); @@ -80,6 +85,16 @@ private async Task RunCreate() groupRequest.MemberViewabilityLevel = base.CheckViewMembersLevel(this._viewMembershipLevel.Value()); } var createdGroup = await boxClient.GroupsManager.CreateAsync(groupRequest); + if (_save.HasValue()) + { + var fileName = $"{base._names.CommandNames.Groups}-{base._names.SubCommandNames.Create}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; + Reporter.WriteInformation("Saving file..."); + var listWrapper = new List(); + listWrapper.Add(createdGroup); + var saved = base.WriteListResultsToReport(listWrapper, fileName, fileFormat: this._fileFormat.Value()); + Reporter.WriteInformation($"File saved: {saved}"); + return; + } if (this._idOnly.HasValue()) { Reporter.WriteInformation(createdGroup.Id); diff --git a/BoxCLI/Commands/GroupSubCommands/GroupDeleteCommand.cs b/BoxCLI/Commands/GroupSubCommands/GroupDeleteCommand.cs index 31c636da..f8b2f091 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupDeleteCommand.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupDeleteCommand.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Box.V2; using BoxCLI.BoxHome; using BoxCLI.BoxPlatform.Service; using BoxCLI.CommandUtilities; @@ -12,9 +13,12 @@ namespace BoxCLI.Commands.GroupSubCommands public class GroupDeleteCommand : GroupSubCommandBase { private CommandArgument _groupId; + + private CommandOption _bulkPath; + private CommandOption _dontPrompt; private CommandLineApplication _app; - public GroupDeleteCommand(IBoxPlatformServiceBuilder boxPlatformBuilder, IBoxHome home, LocalizedStringsResource names) + public GroupDeleteCommand(IBoxPlatformServiceBuilder boxPlatformBuilder, IBoxHome home, LocalizedStringsResource names) : base(boxPlatformBuilder, home, names) { } @@ -24,6 +28,7 @@ public override void Configure(CommandLineApplication command) command.Description = "Delete a group."; _groupId = command.Argument("groupId", "Id of group"); + _bulkPath = BulkFilePathOption.ConfigureOption(command); _dontPrompt = SuppressDeletePromptOption.ConfigureOption(command); command.OnExecute(async () => { @@ -40,29 +45,52 @@ protected async override Task Execute() private async Task RunDelete() { + var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); + if (this._bulkPath.HasValue()) + { + var path = GeneralUtilities.TranslatePath(this._bulkPath.Value()); + var ids = base.ReadFileForIds(path); + foreach (var id in ids) + { + try + { + if (await this.DeleteGroup(boxClient, id)) + { + Reporter.WriteSuccess($"Deleted group {id}"); + } + } + catch (Exception e) + { + Reporter.WriteError($"Error deleting group {id}."); + Reporter.WriteError(e.Message); + } + } + Reporter.WriteInformation("Finished deleting groups..."); + return; + } base.CheckForValue(this._groupId.Value, this._app, "A group ID is required for this command"); bool result; - if (this._dontPrompt.HasValue()) - { - result = await this.DeleteGroup(); - } - else - { - Reporter.WriteWarningNoNewLine("Are you sure you want to delete this group? y/N "); - var yNKey = "n"; - yNKey = Console.ReadLine().ToLower(); - if (yNKey != "y") - { - Reporter.WriteInformation("Aborted group deletion."); - return; - } - else - { - result = await this.DeleteGroup(); - } - } - if(result) + if (this._dontPrompt.HasValue()) + { + result = await this.DeleteGroup(boxClient, this._groupId.Value); + } + else + { + Reporter.WriteWarningNoNewLine("Are you sure you want to delete this group? y/N "); + var yNKey = "n"; + yNKey = Console.ReadLine().ToLower(); + if (yNKey != "y") + { + Reporter.WriteInformation("Aborted group deletion."); + return; + } + else + { + result = await this.DeleteGroup(boxClient, this._groupId.Value); + } + } + if (result) { Reporter.WriteSuccess($"Deleted group {this._groupId.Value}"); } @@ -72,10 +100,9 @@ private async Task RunDelete() } } - private async Task DeleteGroup() + private async Task DeleteGroup(BoxClient boxClient, string groupId) { - var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); - return await boxClient.GroupsManager.DeleteAsync(this._groupId.Value); + return await boxClient.GroupsManager.DeleteAsync(groupId); } } } \ No newline at end of file diff --git a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipCreateCommand.cs b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipCreateCommand.cs index 1dd6c129..8d44fb6c 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipCreateCommand.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipCreateCommand.cs @@ -3,6 +3,7 @@ using Box.V2.Models.Request; using BoxCLI.BoxHome; using BoxCLI.BoxPlatform.Service; +using BoxCLI.CommandUtilities.CommandOptions; using BoxCLI.CommandUtilities.Globalization; using Microsoft.Extensions.CommandLineUtils; @@ -12,6 +13,9 @@ public class GroupMembershipCreateCommand : GroupMembershipSubCommandBase { private CommandArgument _userId; private CommandArgument _groupId; + private CommandOption _save; + private CommandOption _bulkPath; + private CommandOption _fileFormat; private CommandOption _admin; private CommandOption _member; private CommandLineApplication _app; @@ -27,6 +31,9 @@ public override void Configure(CommandLineApplication command) { _app = command; command.Description = "Add a user to a group."; + _bulkPath = BulkFilePathOption.ConfigureOption(command); + _save = SaveOption.ConfigureOption(command); + _fileFormat = FileFormatOption.ConfigureOption(command); _userId = command.Argument("userId", "Id of user"); _groupId = command.Argument("groupId", @@ -51,6 +58,17 @@ protected async override Task Execute() private async Task RunCreate() { + if (this._bulkPath.HasValue()) + { + var json = false; + if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) + { + json = true; + } + await base.CreateGroupMembershipsFromFile(this._bulkPath.Value(), this._save.HasValue(), + json: json, overrideSaveFileFormat: this._fileFormat.Value()); + return; + } base.CheckForValue(this._userId.Value, this._app, "A user ID is required for this command."); base.CheckForValue(this._groupId.Value, this._app, "A group ID is required for this command."); var role = ""; diff --git a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipDeleteCommand.cs b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipDeleteCommand.cs index cd58161f..a9e8d3a8 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipDeleteCommand.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipDeleteCommand.cs @@ -1,7 +1,9 @@ +using System; using System.Threading.Tasks; using BoxCLI.BoxHome; using BoxCLI.BoxPlatform.Service; using BoxCLI.CommandUtilities; +using BoxCLI.CommandUtilities.CommandOptions; using BoxCLI.CommandUtilities.Globalization; using Microsoft.Extensions.CommandLineUtils; @@ -10,6 +12,7 @@ namespace BoxCLI.Commands.GroupSubCommands.GroupMembershipSubCommands public class GroupMembershipDeleteCommand : GroupMembershipSubCommandBase { private CommandArgument _membershipId; + private CommandOption _bulkPath; private CommandLineApplication _app; public GroupMembershipDeleteCommand(IBoxPlatformServiceBuilder boxPlatformBuilder, IBoxHome boxHome, LocalizedStringsResource names) : base(boxPlatformBuilder, boxHome, names) @@ -22,7 +25,7 @@ public override void Configure(CommandLineApplication command) command.Description = "Remove a user from a group."; _membershipId = command.Argument("membershipId", "Id of group membership"); - + _bulkPath = BulkFilePathOption.ConfigureOption(command); command.OnExecute(async () => { return await this.Execute(); @@ -38,8 +41,30 @@ protected async override Task Execute() private async Task RunDelete() { - base.CheckForValue(this._membershipId.Value, this._app, "A group memebership ID is required for this command."); var boxClient = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value()); + if (this._bulkPath.HasValue()) + { + var path = GeneralUtilities.TranslatePath(this._bulkPath.Value()); + var ids = base.ReadFileForIds(path); + foreach (var id in ids) + { + try + { + if (await boxClient.GroupsManager.DeleteGroupMembershipAsync(id)) + { + Reporter.WriteSuccess($"Deleted group membership {id}"); + } + } + catch (Exception e) + { + Reporter.WriteError($"Error deleting group membership {id}."); + Reporter.WriteError(e.Message); + } + } + Reporter.WriteInformation("Finished deleting group memberships..."); + return; + } + base.CheckForValue(this._membershipId.Value, this._app, "A group memebership ID is required for this command."); var result = await boxClient.GroupsManager.DeleteGroupMembershipAsync(this._membershipId.Value); if(result) { diff --git a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipListCommand.cs b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipListCommand.cs index 6161626d..30e940ca 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipListCommand.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipListCommand.cs @@ -3,8 +3,11 @@ using Box.V2.Models; using BoxCLI.BoxHome; using BoxCLI.BoxPlatform.Service; +using BoxCLI.CommandUtilities; using BoxCLI.CommandUtilities.Globalization; +using BoxCLI.CommandUtilities.CsvModels; using Microsoft.Extensions.CommandLineUtils; +using BoxCLI.CommandUtilities.CommandOptions; namespace BoxCLI.Commands.GroupSubCommands.GroupMembershipSubCommands { @@ -14,6 +17,8 @@ public class GroupMembershipListCommand : GroupMembershipSubCommandBase private CommandOption _listMembers; private CommandOption _listGroups; private CommandOption _listCollab; + private CommandOption _save; + private CommandOption _fileFormat; private CommandLineApplication _app; private IBoxHome _home; @@ -35,7 +40,8 @@ public override void Configure(CommandLineApplication command) "List groups a user belongs to with a user ID", CommandOptionType.NoValue); _listCollab = command.Option("--list-collaborations", "List collaborations for a group with a group ID", CommandOptionType.NoValue); - + _save = SaveOption.ConfigureOption(command); + _fileFormat = FileFormatOption.ConfigureOption(command); command.OnExecute(async () => { return await this.Execute(); @@ -57,9 +63,18 @@ private async Task RunList() 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(userMemberships, fileName, fileFormat: this._fileFormat.Value()); + Reporter.WriteInformation($"File saved: {saved}"); + return; + } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { - var memberships = boxClient.GroupsManager.GetAllGroupMembershipsForUserAsync(this._id.Value, autoPaginate: true); + var memberships = await boxClient.GroupsManager.GetAllGroupMembershipsForUserAsync(this._id.Value, autoPaginate: true); base.OutputJson(memberships); return; } @@ -70,9 +85,18 @@ await BoxCollectionsIterators.ListOffsetCollectionToConsole( } 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(membershipCollaborations, fileName, fileFormat: this._fileFormat.Value()); + Reporter.WriteInformation($"File saved: {saved}"); + return; + } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { - var memberships = boxClient.GroupsManager.GetCollaborationsForGroupAsync(this._id.Value, autoPaginate: true); + var memberships = await boxClient.GroupsManager.GetCollaborationsForGroupAsync(this._id.Value, autoPaginate: true); base.OutputJson(memberships); return; } @@ -83,9 +107,18 @@ await BoxCollectionsIterators.ListOffsetCollectionToConsole((o } 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(memberships, fileName, fileFormat: this._fileFormat.Value()); + Reporter.WriteInformation($"File saved: {saved}"); + return; + } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { - var memberships = boxClient.GroupsManager.GetAllGroupMembershipsForGroupAsync(this._id.Value, autoPaginate: true); + var memberships = await boxClient.GroupsManager.GetAllGroupMembershipsForGroupAsync(this._id.Value, autoPaginate: true); base.OutputJson(memberships); return; } diff --git a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipSubCommandBase.cs b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipSubCommandBase.cs index 4c23a7ca..f5660795 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipSubCommandBase.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipSubCommandBase.cs @@ -1,7 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; using Box.V2.Models; +using Box.V2.Models.Request; using BoxCLI.BoxHome; using BoxCLI.BoxPlatform.Service; using BoxCLI.CommandUtilities; +using BoxCLI.CommandUtilities.CsvModels; using BoxCLI.CommandUtilities.Globalization; using Microsoft.Extensions.CommandLineUtils; @@ -14,6 +19,19 @@ public GroupMembershipSubCommandBase(IBoxPlatformServiceBuilder boxPlatformBuild { } + protected virtual void PrintGroupMember(BoxGroupMembership g, bool json) + { + if (json) + { + base.OutputJson(g); + return; + } + else + { + this.PrintGroupMember(g); + } + } + protected virtual void PrintGroupMember(BoxGroupMembership g) { Reporter.WriteInformation($"ID: {g.Id}"); @@ -23,5 +41,121 @@ protected virtual void PrintGroupMember(BoxGroupMembership g) Reporter.WriteInformation($"Role: {g.Role}"); base.PrintMiniUser(g.User); } + + protected async Task CreateGroupMembershipsFromFile(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 groupMembershipRequests = base.ReadFile(path); + List saveCreated = new List(); + + foreach (var groupMembershipRequest in groupMembershipRequests) + { + BoxGroupMembership createdGroupMembership = null; + try + { + createdGroupMembership = await boxClient.GroupsManager.AddMemberToGroupAsync(groupMembershipRequest); + } + catch (Exception e) + { + Reporter.WriteError("Couldn't create group membership..."); + Reporter.WriteError(e.Message); + } + if (createdGroupMembership != null) + { + this.PrintGroupMember(createdGroupMembership, json); + if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) + { + saveCreated.Add(createdGroupMembership); + } + } + } + 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.GroupMembership}-{base._names.SubCommandNames.Create}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; + base.WriteListResultsToReport(saveCreated, fileName, savePath, fileFormat); + } + } + catch (Exception e) + { + Reporter.WriteError(e.Message); + } + } + protected async Task UpdateGroupMembershipsFromFile(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 groupMembershipRequests = base.ReadFile(path); + List saveUpdated = new List(); + + foreach (var groupMembershipRequest in groupMembershipRequests) + { + BoxGroupMembership updatedGroupMembership = null; + try + { + updatedGroupMembership = await boxClient.GroupsManager.UpdateGroupMembershipAsync(groupMembershipRequest.Id, new BoxGroupMembershipRequest() + { + Role = groupMembershipRequest.Role + }); + } + catch (Exception e) + { + Reporter.WriteError("Couldn't update group membership..."); + Reporter.WriteError(e.Message); + } + if (updatedGroupMembership != null) + { + this.PrintGroupMember(updatedGroupMembership, json); + if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) + { + saveUpdated.Add(updatedGroupMembership); + } + } + } + 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.GroupMembership}-{base._names.SubCommandNames.Update}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; + base.WriteListResultsToReport(saveUpdated, fileName, savePath, fileFormat); + } + } + catch (Exception e) + { + Reporter.WriteError(e.Message); + } + } } } \ No newline at end of file diff --git a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipUpdateCommand.cs b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipUpdateCommand.cs index 6a961258..eaaa2428 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipUpdateCommand.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupMembershipSubCommands/GroupMembershipUpdateCommand.cs @@ -5,6 +5,7 @@ using BoxCLI.BoxHome; using BoxCLI.BoxPlatform.Service; using BoxCLI.CommandUtilities; +using BoxCLI.CommandUtilities.CommandOptions; using BoxCLI.CommandUtilities.Globalization; using Microsoft.Extensions.CommandLineUtils; @@ -13,6 +14,9 @@ namespace BoxCLI.Commands.GroupSubCommands.GroupMembershipSubCommands public class GroupMembershipUpdateCommand : GroupMembershipSubCommandBase { private CommandArgument _membershipId; + private CommandOption _save; + private CommandOption _bulkPath; + private CommandOption _fileFormat; private CommandOption _admin; private CommandOption _member; private CommandLineApplication _app; @@ -28,6 +32,9 @@ public override void Configure(CommandLineApplication command) { _app = command; command.Description = "Update a user's membership to a group."; + _bulkPath = BulkFilePathOption.ConfigureOption(command); + _save = SaveOption.ConfigureOption(command); + _fileFormat = FileFormatOption.ConfigureOption(command); _membershipId = command.Argument("membershipId", "Id of group membership"); _admin = command.Option("--set-admin", @@ -50,6 +57,17 @@ protected async override Task Execute() private async Task RunCreate() { + if (this._bulkPath.HasValue()) + { + var json = false; + if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) + { + json = true; + } + await base.CreateGroupMembershipsFromFile(this._bulkPath.Value(), this._save.HasValue(), + json: json, overrideSaveFileFormat: this._fileFormat.Value()); + return; + } base.CheckForValue(this._membershipId.Value, this._app, "A group memebership ID is required for this command."); var role = ""; if (this._admin.HasValue()) diff --git a/BoxCLI/Commands/GroupSubCommands/GroupSubCommandBase.cs b/BoxCLI/Commands/GroupSubCommands/GroupSubCommandBase.cs index 38c09d67..d61b3aa9 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupSubCommandBase.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupSubCommandBase.cs @@ -123,5 +123,60 @@ protected async Task CreateGroupsFromFile(string path, Reporter.WriteError(e.Message); } } + protected async Task UpdateGroupsFromFile(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(path); + List saveCreated = new List(); + + foreach (var groupRequest in groupRequests) + { + BoxGroup updatedGroup = null; + try + { + updatedGroup = await boxClient.GroupsManager.UpdateAsync(groupRequest.Id, groupRequest); + } + catch (Exception e) + { + Reporter.WriteError("Couldn't update group..."); + Reporter.WriteError(e.Message); + } + if (updatedGroup != null) + { + this.PrintGroup(updatedGroup, json); + if (save || !string.IsNullOrEmpty(overrideSavePath) || base._settings.GetAutoSaveSetting()) + { + saveCreated.Add(updatedGroup); + } + } + } + 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.Update}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}"; + base.WriteListResultsToReport(saveCreated, fileName, savePath, fileFormat); + } + } + catch (Exception e) + { + Reporter.WriteError(e.Message); + } + } } } \ No newline at end of file diff --git a/BoxCLI/Commands/GroupSubCommands/GroupUpdateCommand.cs b/BoxCLI/Commands/GroupSubCommands/GroupUpdateCommand.cs index 9cbd4f8c..159e18c0 100644 --- a/BoxCLI/Commands/GroupSubCommands/GroupUpdateCommand.cs +++ b/BoxCLI/Commands/GroupSubCommands/GroupUpdateCommand.cs @@ -1,7 +1,13 @@ +using System; +using System.Collections.Generic; using System.Threading.Tasks; +using Box.V2.Models; using Box.V2.Models.Request; using BoxCLI.BoxHome; using BoxCLI.BoxPlatform.Service; +using BoxCLI.CommandUtilities; +using BoxCLI.CommandUtilities.CommandOptions; +using BoxCLI.CommandUtilities.CsvModels; using BoxCLI.CommandUtilities.Globalization; using Microsoft.Extensions.CommandLineUtils; @@ -13,6 +19,9 @@ public class GroupUpdateCommand : GroupSubCommandBase private CommandOption _name; private CommandOption _inviteLevel; private CommandOption _viewMembershipLevel; + private CommandOption _save; + private CommandOption _bulkPath; + private CommandOption _fileFormat; private CommandLineApplication _app; private IBoxHome _home; @@ -32,7 +41,9 @@ public override void Configure(CommandLineApplication command) _viewMembershipLevel = command.Option("-m|--view-members", "Specifies who can view the members of the group. Enter admins_only, admins_and_members, or all_managed_users", CommandOptionType.SingleValue); - + _bulkPath = BulkFilePathOption.ConfigureOption(command); + _save = SaveOption.ConfigureOption(command); + _fileFormat = FileFormatOption.ConfigureOption(command); command.OnExecute(async () => { return await this.Execute(); @@ -48,6 +59,16 @@ protected async override Task Execute() 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(); @@ -64,6 +85,16 @@ private async Task RunUpdate() 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(); + listWrapper.Add(updatedGroup); + var saved = base.WriteListResultsToReport(listWrapper, fileName, fileFormat: this._fileFormat.Value()); + Reporter.WriteInformation($"File saved: {saved}"); + return; + } if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting()) { base.OutputJson(updatedGroup);