-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveChildPublisherCommand.cs
More file actions
77 lines (60 loc) · 3.36 KB
/
RemoveChildPublisherCommand.cs
File metadata and controls
77 lines (60 loc) · 3.36 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.CommandLine;
using CommunityToolkit.Diagnostics;
using OwlCore.Diagnostics;
using OwlCore.Storage;
using WindowsAppCommunity.Sdk;
using WindowsAppCommunity.Sdk.Nomad;
namespace WindowsAppCommunity.CommandLine.Publisher.ChildPublishers;
/// <summary>
/// Wacsdk remove child publisher command for Publisher.
/// </summary>
public class RemoveChildPublisherCommand : Command
{
/// <summary>
/// Initializes a new instance of the <see cref="RemoveChildPublisherCommand"/> class.
/// </summary>
public RemoveChildPublisherCommand(WacsdkCommandConfig config, Option<string> repoOption, Option<string> idOption, Option<string> publisherIdOption)
: base("remove", "Removes a child publisher from the Publisher.")
{
AddOption(repoOption);
AddOption(idOption);
AddOption(publisherIdOption);
this.SetHandler(InvokeAsync, repoOption, idOption, publisherIdOption);
this.Config = config;
}
protected WacsdkCommandConfig Config { get; init; }
public async Task InvokeAsync(string repo, string id, string publisherId)
{
Guard.IsNotNullOrWhiteSpace(publisherId);
var cancellationToken = Config.CancellationToken;
cancellationToken.ThrowIfCancellationRequested();
Logger.LogInformation($"Getting modifiable publisher");
var publisher = (ModifiablePublisher)await GetPublisherByIdAsync(repo, id, cancellationToken);
Logger.LogInformation($"Got {nameof(publisher.Id)}: {publisher.Id}");
Logger.LogInformation($"Getting child publisher to remove");
var childPublisher = await publisher.ChildPublishers.GetPublishersAsync(cancellationToken).FirstAsync(p => p.Id == publisherId, cancellationToken);
Logger.LogInformation($"Got {nameof(childPublisher.Id)}: {childPublisher.Id}");
Logger.LogInformation($"Removing child publisher from collection");
await publisher.ChildPublishers.RemovePublisherAsync(childPublisher, cancellationToken);
Logger.LogInformation($"Publishing changes");
await PublishAsync(publisher, cancellationToken);
Logger.LogInformation($"Child publisher removed successfully");
}
public async Task<IReadOnlyPublisher> GetPublisherByIdAsync(string repoId, string publisherId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var repoFolder = (IModifiableFolder)await Config.RepositoryStorage.CreateFolderAsync(repoId, overwrite: false, cancellationToken);
var repoSettings = new WacsdkNomadSettings(repoFolder);
await repoSettings.LoadAsync(cancellationToken);
var repositoryContainer = new RepositoryContainer(Config.KuboOptions, Config.Client, repoSettings.ManagedKeys, repoSettings.ManagedUserConfigs, repoSettings.ManagedProjectConfigs, repoSettings.ManagedPublisherConfigs);
var publisher = await repositoryContainer.PublisherRepository.GetAsync(publisherId, cancellationToken);
return publisher;
}
public async Task PublishAsync(IModifiablePublisher publisher, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Logger.LogInformation($"Publishing changes made to {publisher.Id}");
ModifiablePublisher modifiablePublisher = (ModifiablePublisher)publisher;
await modifiablePublisher.FlushAsync(cancellationToken);
}
}