Skip to content

Commit

Permalink
Fine tuned console output routines
Browse files Browse the repository at this point in the history
  • Loading branch information
andresharpe committed Jul 28, 2024
1 parent 2f21c60 commit 5f3b225
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
25 changes: 15 additions & 10 deletions source/Cute/Commands/GetDataCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Contentful.Core.Errors;
using Contentful.Core.Models;
using Contentful.Core.Search;
using Cute.Constants;
using Cute.Lib.Contentful;
using Cute.Lib.Exceptions;
using Cute.Lib.GetDataAdapter;
Expand Down Expand Up @@ -102,7 +103,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se

foreach (var getDataEntry in _cronTasks.Values)
{
await ProcessGetDataEntry(getDataEntry, settings);
await ProcessGetDataEntry(getDataEntry);
}

return 0;
Expand Down Expand Up @@ -240,7 +241,7 @@ private async Task RefreshSchedule([FromForm] string command, HttpContext contex

private async Task ProcessGetDataEntryAndDisplaySchedule(Entry<JObject> entry, Settings settings)
{
await ProcessGetDataEntry(entry, settings);
await ProcessGetDataEntry(entry);

DisplaySchedule();
}
Expand All @@ -261,26 +262,28 @@ private void DisplaySchedule()

var cronSchedule = frequency?.ToCronExpression().ToString();

_logger.LogInformation("Get data '{getDataId}' scheduled to run on schedule '{frequency} ({cronSchedule})'",
_console.WriteNormal("Get data '{getDataId}' scheduled to run on schedule '{frequency} ({cronSchedule})'",
getDataId, frequency, cronSchedule);
}
}

private async Task ProcessGetDataEntry(Entry<JObject> getDataEntry, Settings settings)
private async Task ProcessGetDataEntry(Entry<JObject> getDataEntry)
{
if (_settings is null) return;

var yamlDeserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();

var dataAdapter = new HttpDataAdapter(ContentfulManagementClient, _console.WriteNormal);

var getDataId = GetString(getDataEntry, settings.GetDataIdField);
var getDataId = GetString(getDataEntry, _settings.GetDataIdField);

if (getDataId is null) return;

_logger.LogInformation("Started '{getDataId}'", getDataId);
_console.WriteNormal("Started '{getDataId}'", getDataId);

var yaml = GetString(getDataEntry, settings.GetDataYamlField);
var yaml = GetString(getDataEntry, _settings.GetDataYamlField);

if (yaml is null) return;

Expand All @@ -298,7 +301,7 @@ private async Task ProcessGetDataEntry(Entry<JObject> getDataEntry, Settings set

if (dataResults is null) return;

_console.WriteNormal($"{dataResults.Count} new {contentType.SystemProperties.Id} entries...");
_console.WriteNormal("{Count} new {Id} entries...", dataResults.Count, contentType.SystemProperties.Id);

_ = await CompareAndUpdateResults(
dataResults, serializer,
Expand All @@ -308,7 +311,7 @@ private async Task ProcessGetDataEntry(Entry<JObject> getDataEntry, Settings set
ignoreFields: ignoreFields
);

_logger.LogInformation("Completed '{getDataId}'", getDataId);
_console.WriteNormal("Completed '{getDataId}'", getDataId);
}

private static void ValidateDataAdapter(string fileName, HttpDataAdapterConfig adapter, ContentType contentType, EntrySerializer serializer)
Expand Down Expand Up @@ -347,7 +350,9 @@ private async Task<Dictionary<string, string>> CompareAndUpdateResults(List<Dict

if (newRecord is null) continue;

_console.WriteNormal($"Contentful {contentTypeId} '{key}' matched with new entry '{newRecord[contentDisplayField]}'");
var entryName = newRecord[contentDisplayField];

_console.WriteNormal("Contentful {contentTypeId} '{key}' matched with new entry '{entryName}'", contentTypeId, key, entryName);

var isChanged = false;
Dictionary<string, (string?, string?)> changedFields = [];
Expand Down
2 changes: 1 addition & 1 deletion source/Cute/Commands/WebCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task StartWebServer()

webApp.MapHealthChecks("/healthz");

_console.WriteNormal("Waiting for Contentful webhook trigger...");
_console.WriteNormal("Ready...");

try
{
Expand Down
6 changes: 3 additions & 3 deletions source/Cute/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

var isGettingVersion = args.Contains("version");

var isWebServer = (args.Contains("webserver") || args.Contains("--as-server")) && !args.Contains("--console");
var isServer = args.Contains("--as-server") && !args.Contains("--console-output");

// Get config from protected settings file and environment

Expand All @@ -45,7 +45,7 @@
});
}

if (isWebServer)
if (isServer)
{
loggerConfig.WriteTo.Console(
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}",
Expand All @@ -64,7 +64,7 @@

// Surpress pretty console for webserver

if (isWebServer)
if (isServer)
{
ConsoleWriter.EnableConsole = false;
}
Expand Down
12 changes: 7 additions & 5 deletions source/Cute/Services/ConsoleWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Cute.Constants;
using Spectre.Console;
using Spectre.Console.Rendering;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -33,20 +32,23 @@ public void WriteHeading(string text)

public void WriteHeading(string textTemplate, params object?[] args)
{
Console?.WriteLine(Format(textTemplate, args), Globals.StyleHeading);
Console?.MarkupLine($"Thus is a [underline {Globals.StyleAlertAccent.Foreground}]test[/]");
Console?.WriteLine(Format(textTemplate, Globals.StyleHeading, args));
Logger?.LogInformation(message: textTemplate, args: args);
}

private static string Format(string textTemplate, params object?[] args)
private static string Format(string textTemplate, Style style, params object?[] args)
{
var matches = ParameterMatch().Matches(textTemplate);
var sb = new StringBuilder(textTemplate);
var i = 0;
foreach (Match m in matches)
{
if (i > args.Length - 1) break;
sb.Replace(m.Value, args[i++]?.ToString());
sb.Replace(m.Value, $"[{Globals.StyleHeading.Foreground}]{args[i++]?.ToString()}[/]");
}
sb.Insert(0, $"[{style.Foreground}]");
sb.Append("[/]");
return sb.ToString();
}

Expand Down Expand Up @@ -97,7 +99,7 @@ public void WriteNormal(string text)

public void WriteNormal(string textTemplate, params object?[] args)
{
Console?.WriteLine(Format(textTemplate, args), Globals.StyleNormal);
Console?.MarkupLine(Format(textTemplate, Globals.StyleNormal, args));
Logger?.LogInformation(message: textTemplate, args: args);
}

Expand Down

0 comments on commit 5f3b225

Please sign in to comment.