-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added commands to delete a project and to disable a project (#177)
- Loading branch information
Showing
2 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
source/Octopus.Cli/Commands/Project/DeleteProjectCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
79
source/Octopus.Cli/Commands/Project/DisableProjectCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
} | ||
} | ||
} |