diff --git a/source/Octopus.Cli/Commands/Project/DeleteProjectCommand.cs b/source/Octopus.Cli/Commands/Project/DeleteProjectCommand.cs new file mode 100644 index 0000000000..54d1109522 --- /dev/null +++ b/source/Octopus.Cli/Commands/Project/DeleteProjectCommand.cs @@ -0,0 +1,73 @@ +using System; +using System.Threading.Tasks; +using Octopus.Cli.Repositories; +using Octopus.Cli.Util; +using Octopus.Client; +using Octopus.Client.Model; +using Octopus.CommandLine; +using Octopus.CommandLine.Commands; +using Octopus.Cli.Infrastructure; + +namespace Octopus.Cli.Commands.Project +{ + [Command("delete-project", Description = "Deletes a project.")] + public class DeleteProjectCommand : ApiCommand, ISupportFormattedOutput + { + ProjectResource project; + bool ProjectDeleted; + + public DeleteProjectCommand(IOctopusAsyncRepositoryFactory repositoryFactory, IOctopusFileSystem fileSystem, IOctopusClientFactory clientFactory, ICommandOutputProvider commandOutputProvider) + : base(clientFactory, repositoryFactory, fileSystem, commandOutputProvider) + { + var options = Options.For("Project deletion"); + options.Add("name=", "The name of the project.", v => ProjectName = v); + } + + public string ProjectName { get; set; } + + public async Task Request() + { + if (string.IsNullOrWhiteSpace(ProjectName)) throw new CommandException("Please specify a project name using the parameter: --name=XYZ"); + + commandOutputProvider.Information("Finding project: {Project:l}", ProjectName); + + project = await Repository.Projects.FindByName(ProjectName).ConfigureAwait(false); + if (project == null) + { + throw new CouldNotFindException("project"); + } + + commandOutputProvider.Information("Deleting project: {Project:l}", ProjectName); + + try + { + await Repository.Projects.Delete(project).ConfigureAwait(false); + } + catch (Exception ex) + { + commandOutputProvider.Error("Error deleting project {Project:l}: {Exception:l}", project.Name, ex.Message); + throw; + } + ProjectDeleted = true; + + } + + public void PrintDefaultOutput() + { + commandOutputProvider.Information("Project deleted. ID: {Id:l}", project.Id); + } + + public void PrintJsonOutput() + { + commandOutputProvider.Json(new + { + Project = new + { + project.Id, + project.Name, + ProjectDeleted + } + }); + } + } +} \ No newline at end of file diff --git a/source/Octopus.Cli/Commands/Project/DisableProjectCommand.cs b/source/Octopus.Cli/Commands/Project/DisableProjectCommand.cs new file mode 100644 index 0000000000..d30a5cfae2 --- /dev/null +++ b/source/Octopus.Cli/Commands/Project/DisableProjectCommand.cs @@ -0,0 +1,79 @@ +using System; +using System.Threading.Tasks; +using Octopus.Cli.Repositories; +using Octopus.Cli.Util; +using Octopus.Client; +using Octopus.Client.Model; +using Octopus.CommandLine; +using Octopus.CommandLine.Commands; +using Octopus.Cli.Infrastructure; + +namespace Octopus.Cli.Commands.Project +{ + [Command("disable-project", Description = "Disables a project.")] + public class DisableProjectCommand : ApiCommand, ISupportFormattedOutput + { + ProjectResource project; + + public DisableProjectCommand(IOctopusAsyncRepositoryFactory repositoryFactory, IOctopusFileSystem fileSystem, IOctopusClientFactory clientFactory, ICommandOutputProvider commandOutputProvider) + : base(clientFactory, repositoryFactory, fileSystem, commandOutputProvider) + { + var options = Options.For("Project disablement"); + options.Add("name=", "The name of the project.", v => ProjectName = v); + } + + public string ProjectName { get; set; } + + public async Task Request() + { + if (string.IsNullOrWhiteSpace(ProjectName)) throw new CommandException("Please specify a project name using the parameter: --name=XYZ"); + + commandOutputProvider.Information("Finding project: {Project:l}", ProjectName); + + // Get project + project = await Repository.Projects.FindByName(ProjectName).ConfigureAwait(false); + + // Check that is project exists and isn't already disabled + if (project == null) + { + throw new CouldNotFindException("project"); + } + else if (project.IsDisabled == true) + { + commandOutputProvider.Information("The project {Project:l} is already disabled.", project.Name); + return; + } + + // Disable project + commandOutputProvider.Information("Disabling project: {Project:l}", ProjectName); + try + { + project.IsDisabled = true; + await Repository.Projects.Modify(project).ConfigureAwait(false); + } + catch (Exception ex) + { + commandOutputProvider.Error("Error disabling project {Project:l}: {Exception:l}", project.Name, ex.Message); + } + + } + + public void PrintDefaultOutput() + { + commandOutputProvider.Information("Project {Project:l} disabled. ID: {Id:l}", project.Name, project.Id); + } + + public void PrintJsonOutput() + { + commandOutputProvider.Json(new + { + Project = new + { + project.Id, + project.Name, + project.IsDisabled + } + }); + } + } +} \ No newline at end of file