Repository navigation
Expand file tree
/
Copy pathBaseCommand.cs
More file actions
60 lines (52 loc) · 2.32 KB
/
Copy pathBaseCommand.cs
File metadata and controls
60 lines (52 loc) · 2.32 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
// Copyright 2026 Entex Interactive
using Perforce.P4;
using Spectre.Console;
using Spectre.Console.Cli;
using UShell.Perforce.Settings;
namespace UShell.Perforce
{
public abstract class BaseCommand<TSettings> : AsyncCommand<TSettings> where TSettings : PerforceSettings
{
protected Repository Repository { get; private set; } = null!;
protected Connection Connection { get; private set; } = null!;
protected Client Client { get; private set; } = null!;
public sealed override async Task<int> ExecuteAsync(CommandContext context, TSettings settings, CancellationToken cancellationToken)
{
try
{
string address = settings.Host ?? new ServerAddress(string.Empty).Uri;
string workspace = settings.Workspace ?? Environment.GetEnvironmentVariable("P4CLIENT") ?? string.Empty;
string user = settings.User ?? Environment.GetEnvironmentVariable("P4USER") ?? string.Empty;
Server server = new Server(new ServerAddress(address));
Repository = new Repository(server);
Connection = Repository.Connection;
if (!string.IsNullOrWhiteSpace(user)) Connection.UserName = user;
if (!string.IsNullOrWhiteSpace(workspace))
{
Connection.Client = new Client
{
Name = workspace
};
}
Connection.Connect(new Options());
Client = Connection.Client ?? throw new InvalidOperationException("No Perforce workspace is selected.");
return await ExecuteP4Async(context, settings, cancellationToken);
}
catch (OperationCanceledException)
{
AnsiConsole.MarkupLine("\n[yellow]Command cancelled.[/]");
return 130;
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Perforce command failed:[/] " + $"{Markup.Escape(ex.Message)}");
return 1;
}
finally
{
Connection.Disconnect();
}
}
protected abstract Task<int> ExecuteP4Async(CommandContext context, TSettings settings, CancellationToken cancellationToken);
}
}