-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddChildPublisherCommand.cs
More file actions
91 lines (71 loc) · 3.98 KB
/
AddChildPublisherCommand.cs
File metadata and controls
91 lines (71 loc) · 3.98 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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 add child publisher command for Publisher.
/// </summary>
public class AddChildPublisherCommand : Command
{
/// <summary>
/// Initializes a new instance of the <see cref="AddChildPublisherCommand"/> class.
/// </summary>
public AddChildPublisherCommand(WacsdkCommandConfig config, Option<string> repoOption, Option<string> idOption, Option<string> publisherIdOption, Option<string> roleIdOption, Option<string> roleNameOption, Option<string> roleDescriptionOption)
: base("add", "Adds a child publisher to the Publisher.")
{
AddOption(repoOption);
AddOption(idOption);
AddOption(publisherIdOption);
AddOption(roleIdOption);
AddOption(roleNameOption);
AddOption(roleDescriptionOption);
this.SetHandler(InvokeAsync, repoOption, idOption, publisherIdOption, roleIdOption, roleNameOption, roleDescriptionOption);
this.Config = config;
}
protected WacsdkCommandConfig Config { get; init; }
public async Task InvokeAsync(string repo, string id, string publisherId, string roleId, string roleName, string roleDescription)
{
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");
var childPublisher = await GetPublisherByIdAsync(repo, publisherId, cancellationToken);
Logger.LogInformation($"Got {nameof(childPublisher.Id)}: {childPublisher.Id}");
var role = new Role
{
Id = roleId,
Name = roleName,
Description = roleDescription
};
var readOnlyPublisher = childPublisher is ModifiablePublisher modifiablePublisher ? modifiablePublisher.InnerPublisher : (ReadOnlyPublisher)childPublisher;
var publisherRole = new ReadOnlyPublisherRole { Role = role, InnerPublisher = readOnlyPublisher };
Logger.LogInformation($"Adding child publisher to collection");
await publisher.ChildPublishers.AddPublisherAsync(publisherRole, cancellationToken);
Logger.LogInformation($"Publishing changes");
await PublishAsync(publisher, cancellationToken);
Logger.LogInformation($"Child publisher added 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);
}
}