Skip to content

Commit

Permalink
chore: Fix code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
svrooij committed May 1, 2024
1 parent c2cc040 commit 63b35f7
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/WingetIntune.Cli/Commands/GenerateIndexCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ internal class GenerateIndexCommand : Command
public GenerateIndexCommand() : base(name, description)
{
IsHidden = true;
AddOption(new Option<string>(["--output-path", "-o" ], "The path to the output file") { IsRequired = true });
AddOption(new Option<Uri>(["--source-uri", "-s" ], () => new Uri(Winget.CommunityRepository.WingetRepository.DefaultIndexUri), "The source URI to use for the index.json file"));
AddOption(new Option<int>(["--timeout", "-t" ], () => 600000, "The timeout for the operation in milliseconds"));
AddOption(new Option<string>(["--update-json" ], "Create JSON file with only the updates") { IsHidden = true });
AddOption(new Option<string>(["--update-csv" ], "Create CSV file with only the updates") { IsHidden = true });
AddOption(new Option<bool>(["--update-github" ], "Create GitHub Action step summary") { IsHidden = true });
AddOption(new Option<Uri?>(["--update-uri" ], "Post updates to this url") { IsHidden = true });
AddOption(new Option<string>(["--output-path", "-o"], "The path to the output file") { IsRequired = true });
AddOption(new Option<Uri>(["--source-uri", "-s"], () => new Uri(Winget.CommunityRepository.WingetRepository.DefaultIndexUri), "The source URI to use for the index.json file"));
AddOption(new Option<int>(["--timeout", "-t"], () => 600000, "The timeout for the operation in milliseconds"));
AddOption(new Option<string>(["--update-json"], "Create JSON file with only the updates") { IsHidden = true });
AddOption(new Option<string>(["--update-csv"], "Create CSV file with only the updates") { IsHidden = true });
AddOption(new Option<bool>(["--update-github"], "Create GitHub Action step summary") { IsHidden = true });
AddOption(new Option<Uri?>(["--update-uri"], "Post updates to this url") { IsHidden = true });

this.Handler = CommandHandler.Create<GenerateIndexCommandOptions, InvocationContext>(HandleCommand);
}
Expand Down Expand Up @@ -50,68 +50,80 @@ private async Task<int> HandleCommand(GenerateIndexCommandOptions options, Invoc
return 0;
}

private static async Task HandleChanges(ILogger logger, GenerateIndexCommandOptions options, IEnumerable<Winget.CommunityRepository.Models.WingetEntry> packages, CancellationToken cancellationToken){
private static async Task HandleChanges(ILogger logger, GenerateIndexCommandOptions options, IEnumerable<Winget.CommunityRepository.Models.WingetEntry> packages, CancellationToken cancellationToken)
{
logger.LogInformation("Detecting changes from existing index.json file at {outputPath}", options.OutputPath);
var existingJson = await File.ReadAllTextAsync(Path.GetFullPath(options.OutputPath), cancellationToken);
var existingPackages = JsonSerializer.Deserialize<IEnumerable<Winget.CommunityRepository.Models.WingetEntry>>(existingJson);
if (existingPackages is not null) {
if (existingPackages is not null)
{
var updates = packages
.Where(p => !existingPackages.Any(ep => ep.PackageId == p.PackageId && ep.Version == p.Version))
.OrderBy(p => p.PackageId);
if (updates.Any()) {
if (updates.Any())
{
var lastWriteTime = File.GetLastWriteTimeUtc(Path.GetFullPath(options.OutputPath));
logger.LogInformation("Detected {count} updates since {lastWriteTime:yyyy-MM-dd HH:mm} UTC", updates.Count(), lastWriteTime);
if (!string.IsNullOrEmpty(options.UpdateJson)) {
if (!string.IsNullOrEmpty(options.UpdateJson))
{
var updatesJson = JsonSerializer.Serialize(updates);
await File.WriteAllTextAsync(Path.GetFullPath(options.UpdateJson), updatesJson, cancellationToken);
logger.LogInformation("Generated updates.json file at {outputPath}", options.UpdateJson);
}

if (!string.IsNullOrEmpty(options.UpdateCsv)) {
if (!string.IsNullOrEmpty(options.UpdateCsv))
{
var csv = new StringBuilder();
csv.AppendLine("\"PackageId\",\"Version\"");
foreach (var update in updates) {
foreach (var update in updates)
{
csv.AppendLine($"\"{update.PackageId}\",\"{update.Version}\"");
}
await File.WriteAllTextAsync(Path.GetFullPath(options.UpdateCsv), csv.ToString(), cancellationToken);
logger.LogInformation("Generated updates.csv file at {outputPath}", options.UpdateCsv);
}

if (options.UpdateGithub == true) {
if (options.UpdateGithub == true)
{
// Write markdown table with update summary to environment variable GITHUB_STEP_SUMMARY
// get last file write date from the existing file

var markdown = new StringBuilder();
markdown.AppendLine($"Detected **{updates.Count()}** updates since `{lastWriteTime:yyyy-MM-dd HH:mm:ss} UTC`");
markdown.AppendLine("");
markdown.AppendLine("| PackageId | Version |");
markdown.AppendLine("| --- | --- |");
foreach (var update in updates) {
foreach (var update in updates)
{
markdown.AppendLine($"| {update.PackageId} | {update.Version} |");
}
Environment.SetEnvironmentVariable("GITHUB_STEP_SUMMARY", markdown.ToString(), EnvironmentVariableTarget.Process);
logger.LogInformation("Generated GitHub Action step summary");
}

if (options.UpdateUri is not null && options.UpdateUri.IsAbsoluteUri) {
if (options.UpdateUri is not null && options.UpdateUri.IsAbsoluteUri)
{
await PostUpdatesToUri(logger, options.UpdateUri, updates, cancellationToken);
}
}
else {
else
{
logger.LogInformation("No updates detected");
}
}
}

private static async Task PostUpdatesToUri(ILogger logger, Uri uri, IEnumerable<Winget.CommunityRepository.Models.WingetEntry> updates, CancellationToken cancellationToken)
{
try {
try
{
var json = JsonSerializer.Serialize(updates);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var client = new HttpClient();
await client.PostAsync(uri, content, cancellationToken);
}
catch (Exception ex) {
catch (Exception ex)
{
logger.LogError(ex, "Failed to post updates to {host}", uri.Host);
}
}
Expand Down

0 comments on commit 63b35f7

Please sign in to comment.