2022-06-05 18:44:51 +00:00
|
|
|
using System.CommandLine;
|
|
|
|
using System.CommandLine.Invocation;
|
|
|
|
using System.Globalization;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Nitride.Commands;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
using TimeSpanParserUtil;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
namespace MfGames.Nitride.Temporal.Cli;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A factory to inject the "--expires=XXXX" argument into the build
|
|
|
|
/// and other pipeline commands.
|
|
|
|
/// </summary>
|
|
|
|
public class ExpiresPipelineCommandOption : IPipelineCommandOption
|
|
|
|
{
|
2023-01-21 07:52:52 +00:00
|
|
|
private readonly TimeService clock;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
public ExpiresPipelineCommandOption(
|
|
|
|
ILogger logger,
|
2023-01-21 07:52:52 +00:00
|
|
|
TimeService clock,
|
2022-07-09 04:52:10 +00:00
|
|
|
string? defaultValue = null)
|
2022-06-05 18:44:51 +00:00
|
|
|
{
|
|
|
|
this.logger = logger.ForContext<Instant>();
|
|
|
|
this.clock = clock;
|
2022-07-09 04:52:10 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
this.Option = new Option<string?>("--expires", () => defaultValue)
|
|
|
|
{
|
2023-01-15 00:19:42 +00:00
|
|
|
Description =
|
|
|
|
"Sets the expiration time as time before the current date",
|
2022-06-05 18:44:51 +00:00
|
|
|
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.
|
2023-01-15 00:19:42 +00:00
|
|
|
string? value =
|
|
|
|
(string?)context.ParseResult.GetValueForOption(this.Option);
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
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");
|
2022-07-09 04:52:10 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out when we are going to be expiring.
|
|
|
|
Instant when = this.clock.Expiration.Value;
|
|
|
|
ZonedDateTime dateTime = when.InZone(this.clock.DateTimeZone);
|
2023-01-15 00:19:42 +00:00
|
|
|
string formatted = dateTime.ToString(
|
|
|
|
"yyyy-MM-dd HH:mm:ss x",
|
|
|
|
CultureInfo.CurrentCulture);
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
this.logger.Information("Expiring entries before {When:l}", formatted);
|
|
|
|
}
|
|
|
|
}
|