Skip to content

Commit

Permalink
feat: added commands to delete a project and to disable a project (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmayr authored Jun 17, 2022
1 parent 690a91b commit 2bdb94c
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
73 changes: 73 additions & 0 deletions source/Octopus.Cli/Commands/Project/DeleteProjectCommand.cs
Original file line number Diff line number Diff line change
@@ -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<string>("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
}
});
}
}
}
79 changes: 79 additions & 0 deletions source/Octopus.Cli/Commands/Project/DisableProjectCommand.cs
Original file line number Diff line number Diff line change
@@ -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<string>("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
}
});
}
}
}

0 comments on commit 2bdb94c

Please sign in to comment.