This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-nitride-cil/src/MfGames.Nitride.Temporal/Cli/ExpiresPipelineCommandOptio...

77 lines
2.1 KiB
C#

using System.CommandLine;
using System.CommandLine.Invocation;
using System.Globalization;
using MfGames.Nitride.Commands;
using NodaTime;
using Serilog;
using TimeSpanParserUtil;
namespace MfGames.Nitride.Temporal.Cli;
/// <summary>
/// A factory to inject the "--expires=XXXX" argument into the build
/// and other pipeline commands.
/// </summary>
public class ExpiresPipelineCommandOption : IPipelineCommandOption
{
private readonly TimeService clock;
private readonly ILogger logger;
public ExpiresPipelineCommandOption(
ILogger logger,
TimeService clock,
string? defaultValue = null)
{
this.logger = logger.ForContext<Instant>();
this.clock = clock;
this.Option = new Option<string?>("--expires", () => defaultValue)
{
Description =
"Sets the expiration time as time before the current date",
ArgumentHelpName = "TIMESPAN",
};
}
/// <inheritdoc />
public Option Option { get; }
/// <inheritdoc />
public void Handle(InvocationContext context)
{
// If we have a format, then we are going to set one. If the format
// is blank or never, then we are going to treat it as not
// expiring anything.
string? value =
(string?)context.ParseResult.GetValueForOption(this.Option);
if (value != null && value.ToLowerInvariant() != "never")
{
// Parse the format using TimeSpanParser.
this.clock.Expires = TimeSpanParser.Parse(value);
}
// If we don't have anything, then just report and we're done.
if (this.clock.Expiration == null)
{
this.logger.Information("No entities will be expired");
return;
}
// Figure out when we are going to be expiring.
Instant when = this.clock.Expiration.Value;
ZonedDateTime dateTime = when.InZone(this.clock.DateTimeZone);
string formatted = dateTime.ToString(
"yyyy-MM-dd HH:mm:ss x",
CultureInfo.CurrentCulture);
this.logger.Information("Expiring entries before {When:l}", formatted);
}
}