-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkRemoveCommandHandler.cs
More file actions
62 lines (54 loc) · 2.18 KB
/
WorkRemoveCommandHandler.cs
File metadata and controls
62 lines (54 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.CommandLine;
using ChangeTrace.Cli.Interfaces;
using ChangeTrace.Configuration.Discovery;
using ChangeTrace.CredentialTrace.Interfaces;
using ChangeTrace.CredentialTrace.Profiles;
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console;
namespace ChangeTrace.Cli.Handlers.Profiles.Workspaces;
[AutoRegister(ServiceLifetime.Transient, typeof(WorkRemoveCommandHandler))]
internal sealed class WorkRemoveCommandHandler(
IProfileStore<OrganizationProfile> orgStore,
IWorkspaceStore workspaceStore,
IWorkspaceContext workspaceContext) : ICliHandler
{
public async Task HandleAsync(ParseResult parseResult, CancellationToken ct)
=> await HandleAsync(
parseResult.GetValue<string>("name")!,
parseResult.GetValue<string>("--org")!,
parseResult.GetValue<bool>("--yes"),
ct);
private async Task HandleAsync(
string name,
string orgName,
bool assumeYes,
CancellationToken ct)
{
var org = await orgStore.GetByNameAsync(orgName, ct);
if (org is null)
{
AnsiConsole.MarkupLine($"[red]Organization '{orgName}' not found.[/]");
return;
}
var workspaces = await workspaceStore.GetByNameOrganization(orgName, ct);
var workspace = workspaces.FirstOrDefault(w =>
w.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (workspace is null)
{
AnsiConsole.MarkupLine($"[red]Workspace '{name}' not found in organization '{orgName}'.[/]");
return;
}
if (workspaceContext.Current?.Id == workspace.Id)
{
AnsiConsole.MarkupLine("[red]Cannot remove the active workspace. Select another workspace first.[/]");
return;
}
if (!assumeYes && !AnsiConsole.Confirm($"Remove workspace [yellow]{workspace.Name}[/] from [yellow]{org.Name}[/]?"))
{
AnsiConsole.MarkupLine("[yellow]Cancelled.[/]");
return;
}
await workspaceStore.DeleteAsync(workspace.Id, ct);
AnsiConsole.MarkupLine($"[green]Removed workspace '{workspace.Name}' from organization '{org.Name}'.[/]");
}
}