Skip to content

Commit

Permalink
Merge pull request #6 from BinaryPatrick/feat/improve-run-safety
Browse files Browse the repository at this point in the history
Add more run safety and require retention values
  • Loading branch information
binarypatrick authored Jul 4, 2023
2 parents 11fc578 + 5f5b314 commit 071fb8b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
24 changes: 12 additions & 12 deletions src/Models/PruneOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,38 @@ public class PruneOptions
/// <summary>
/// Number of archives to keep at a minimum
/// </summary>
[Option('l', "keep-last", Required = false, HelpText = "Number of archives to keep at a minimum", Default = (uint)5)]
public uint KeepLastCount { get; set; } = 5;
[Option('l', "keep-last", Required = false, HelpText = "Number of archives to keep at a minimum", Group = "retention")]
public uint KeepLastCount { get; set; }

/// <summary>
/// Number of hourly archives to keep
/// </summary>
[Option('h', "keep-hourly", Required = false, HelpText = "Number of hourly archives to keep", Default = (uint)0)]
public uint KeepHourlyCount { get; set; } = 0;
[Option('h', "keep-hourly", Required = false, HelpText = "Number of hourly archives to keep", Group = "retention")]
public uint KeepHourlyCount { get; set; }

/// <summary>
/// Number of daily archives to keep
/// </summary>
[Option('d', "keep-daily", Required = false, HelpText = "Number of daily archives to keep", Default = (uint)0)]
public uint KeepDailyCount { get; set; } = 0;
[Option('d', "keep-daily", Required = false, HelpText = "Number of daily archives to keep", Group = "retention")]
public uint KeepDailyCount { get; set; }

/// <summary>
/// Number of weekly archives to keep
/// </summary>
[Option('w', "keep-weekly", Required = false, HelpText = "Number of weekly archives to keep", Default = (uint)0)]
public uint KeepWeeklyCount { get; set; } = 0;
[Option('w', "keep-weekly", Required = false, HelpText = "Number of weekly archives to keep", Group = "retention")]
public uint KeepWeeklyCount { get; set; }

/// <summary>
/// Number of monthly archives to keep
/// </summary>
[Option('m', "keep-monthly", Required = false, HelpText = "Number of monthly archives to keep", Default = (uint)0)]
public uint KeepMonthlyCount { get; set; } = 0;
[Option('m', "keep-monthly", Required = false, HelpText = "Number of monthly archives to keep", Group = "retention")]
public uint KeepMonthlyCount { get; set; }

/// <summary>
/// Number of yearly archives to keep
/// </summary>
[Option('y', "keep-yearly", Required = false, HelpText = "Number of yearly archives to keep", Default = (uint)0)]
public uint KeepYearlyCount { get; set; } = 0;
[Option('y', "keep-yearly", Required = false, HelpText = "Number of yearly archives to keep", Group = "retention")]
public uint KeepYearlyCount { get; set; }

#endregion

Expand Down
4 changes: 2 additions & 2 deletions src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"BinaryPatrick.Prune": {
"commandName": "Project",
"commandLineArgs": ""
"commandLineArgs": "--path ./ --keep-last 0 --create-files 15"
}
}
}
}
3 changes: 2 additions & 1 deletion src/Services/DirectoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ public void CreateFiles()
logger.LogInformation($"Creating {range.Count} files between {range.First().Timestamp:s} and {range.Last().Timestamp:s}");

string prefix = Guid.NewGuid().ToString("N")[..6];
string fileExtension = options.FileExtension ?? "tar.gz";
foreach ((string row, DateTime timestamp) in range)
{
string filename = $"{options.Path}{prefix}_{timestamp:yyyyMMdd_HHmmss}.txt";
string filename = $"{options.Path}{prefix}_{timestamp:yyyyMMdd_HHmmss}.{fileExtension}";
File.Create(filename).Dispose();
File.SetLastWriteTime(filename, timestamp);

Expand Down
16 changes: 8 additions & 8 deletions tests/Tests/PruneOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using BinaryPatrick.Prune.Models;
using System.Reflection;
using BinaryPatrick.Prune.Models;
using BinaryPatrick.Prune.Models.Constants;
using CommandLine;
using FluentAssertions;
using System.Reflection;
using Xunit;

namespace BinaryPatrick.Prune.Unit.Tests;
Expand Down Expand Up @@ -136,7 +136,7 @@ public void PruneOptions_KeepLastCount()
pathAttribute!.LongName.Should().Be("keep-last");
pathAttribute!.Required.Should().BeFalse();
pathAttribute!.HelpText.Should().NotBeNullOrEmpty();
pathAttribute!.Default.Should().Be(5);
pathAttribute!.Default.Should().BeNull();
}

[Fact]
Expand All @@ -154,7 +154,7 @@ public void PruneOptions_KeepHourlyCount()
pathAttribute!.LongName.Should().Be("keep-hourly");
pathAttribute!.Required.Should().BeFalse();
pathAttribute!.HelpText.Should().NotBeNullOrEmpty();
pathAttribute!.Default.Should().Be(0);
pathAttribute!.Default.Should().BeNull();
}

[Fact]
Expand All @@ -172,7 +172,7 @@ public void PruneOptions_KeepDailyCount()
pathAttribute!.LongName.Should().Be("keep-daily");
pathAttribute!.Required.Should().BeFalse();
pathAttribute!.HelpText.Should().NotBeNullOrEmpty();
pathAttribute!.Default.Should().Be(0);
pathAttribute!.Default.Should().BeNull();
}

[Fact]
Expand All @@ -190,7 +190,7 @@ public void PruneOptions_KeepWeeklyCount()
pathAttribute!.LongName.Should().Be("keep-weekly");
pathAttribute!.Required.Should().BeFalse();
pathAttribute!.HelpText.Should().NotBeNullOrEmpty();
pathAttribute!.Default.Should().Be(0);
pathAttribute!.Default.Should().BeNull();
}

[Fact]
Expand All @@ -208,7 +208,7 @@ public void PruneOptions_KeepMonthlyCount()
pathAttribute!.LongName.Should().Be("keep-monthly");
pathAttribute!.Required.Should().BeFalse();
pathAttribute!.HelpText.Should().NotBeNullOrEmpty();
pathAttribute!.Default.Should().Be(0);
pathAttribute!.Default.Should().BeNull();
}

[Fact]
Expand All @@ -226,6 +226,6 @@ public void PruneOptions_KeepYearlyCount()
pathAttribute!.LongName.Should().Be("keep-yearly");
pathAttribute!.Required.Should().BeFalse();
pathAttribute!.HelpText.Should().NotBeNullOrEmpty();
pathAttribute!.Default.Should().Be(0);
pathAttribute!.Default.Should().BeNull();
}
}

0 comments on commit 071fb8b

Please sign in to comment.