internal sealed class ContentSourceCommands()

in src/tooling/docs-assembler/Cli/ContentSourceCommands.cs [15:74]


internal sealed class ContentSourceCommands(ICoreService githubActionsService, ILoggerFactory logFactory)
{
	[SuppressMessage("Usage", "CA2254:Template should be a static expression")]
	private void AssignOutputLogger()
	{
		var log = logFactory.CreateLogger<Program>();
		ConsoleApp.Log = msg => log.LogInformation(msg);
		ConsoleApp.LogError = msg => log.LogError(msg);
	}

	/// <summary>  </summary>
	/// <param name="repository"></param>
	/// <param name="branchOrTag"></param>
	/// <param name="ctx"></param>
	[Command("match")]
	public async Task<int> Match([Argument] string? repository = null, [Argument] string? branchOrTag = null, Cancel ctx = default)
	{
		AssignOutputLogger();
		var repo = repository ?? githubActionsService.GetInput("repository");
		if (string.IsNullOrEmpty(repo))
			throw new ArgumentNullException(nameof(repository));
		var refName = branchOrTag ?? githubActionsService.GetInput("ref_name");
		if (string.IsNullOrEmpty(refName))
			throw new ArgumentNullException(nameof(branchOrTag));

		await using var collector = new ConsoleDiagnosticsCollector(logFactory, githubActionsService)
		{
			NoHints = true
		};

		_ = collector.StartAsync(ctx);

		// environment does not matter to check the configuration, defaulting to dev
		var assembleContext = new AssembleContext("dev", collector, new FileSystem(), new FileSystem(), null, null)
		{
			Force = false,
			AllowIndexing = false
		};
		var logger = logFactory.CreateLogger<ContentSourceCommands>();
		var matches = assembleContext.Configuration.Match(repo, refName);
		if (matches == null)
		{
			logger.LogInformation("'{Repository}' '{BranchOrTag}' combination not found in configuration.", repository, branchOrTag);
			await githubActionsService.SetOutputAsync("content-source-match", "false");
			await githubActionsService.SetOutputAsync("content-source-name", "");
		}
		else
		{
			var name = matches.Value.ToStringFast(true);
			logger.LogInformation("'{Repository}' '{BranchOrTag}' is configured as '{Matches}' content-source", repository, branchOrTag, name);

			await githubActionsService.SetOutputAsync("content-source-match", "true");
			await githubActionsService.SetOutputAsync("content-source-name", name);
		}

		await collector.StopAsync(ctx);
		return matches != null && collector.Errors == 0 ? 0 : 1;
	}

}