From 97bbf060e6dd9519bfbc14ad7a083cbabb04733b Mon Sep 17 00:00:00 2001 From: Edi Wang Date: Thu, 28 Dec 2023 12:05:12 +0800 Subject: [PATCH] add KeepOld option, fix #1 --- .../Edi.AzureBlobSync.csproj | 2 +- src/Edi.AzureBlobSync/Options.cs | 3 +++ src/Edi.AzureBlobSync/Program.cs | 26 ++++++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Edi.AzureBlobSync/Edi.AzureBlobSync.csproj b/src/Edi.AzureBlobSync/Edi.AzureBlobSync.csproj index 3d2fa77..e45146f 100644 --- a/src/Edi.AzureBlobSync/Edi.AzureBlobSync.csproj +++ b/src/Edi.AzureBlobSync/Edi.AzureBlobSync.csproj @@ -1,7 +1,7 @@ Exe - 1.18.2 + 1.19.0 net8.0 true true diff --git a/src/Edi.AzureBlobSync/Options.cs b/src/Edi.AzureBlobSync/Options.cs index e01d5cd..fab6527 100644 --- a/src/Edi.AzureBlobSync/Options.cs +++ b/src/Edi.AzureBlobSync/Options.cs @@ -19,6 +19,9 @@ internal class Options [Option(longName: "silence", Default = false, Required = false, HelpText = "Silence mode")] public bool Silence { get; set; } + [Option(longName: "keepold", Default = false, Required = false, HelpText = "Keep local old file versions, do not override when receving a new version of file from Azure")] + public bool KeepOld { get; set; } + [Option(longName: "comparehash", Default = (bool)true, Required = false, HelpText = "Compare file hash")] public bool? CompareHash { get; set; } } \ No newline at end of file diff --git a/src/Edi.AzureBlobSync/Program.cs b/src/Edi.AzureBlobSync/Program.cs index c54f26e..d933ee7 100644 --- a/src/Edi.AzureBlobSync/Program.cs +++ b/src/Edi.AzureBlobSync/Program.cs @@ -143,11 +143,18 @@ await AnsiConsole.Status() if (Options.Silence || AnsiConsole.Confirm("[yellow]Do you want to delete these files?[/]")) { - AnsiConsole.WriteLine("Deleting local redundancy files..."); - foreach (var fi in localExcepts) + if (!Options.KeepOld) { - File.Delete(Path.Combine(Options.Path, fi.FileName)); - deleteCount++; + AnsiConsole.WriteLine("Deleting local redundancy files..."); + foreach (var fi in localExcepts) + { + File.Delete(Path.Combine(Options.Path, fi.FileName)); + deleteCount++; + } + } + else + { + AnsiConsole.WriteLine("Skipping deleting local redundancy files because `KeepOld` is set to `true`."); } } } @@ -181,6 +188,7 @@ private static void WriteParameterTable() table.AddRow(new Markup("[blue]Container Name[/]"), new Text(Options.Container)); table.AddRow(new Markup("[blue]Download Threads[/]"), new Text(Options.Threads.ToString())); table.AddRow(new Markup("[blue]Local Path[/]"), new Text(Options.Path)); + table.AddRow(new Markup("[blue]Keep Old[/]"), new Text(Options.KeepOld.ToString())); table.AddRow(new Markup("[blue]Compare Hash[/]"), new Text(Options.CompareHash.ToString())); AnsiConsole.Write(table); @@ -232,6 +240,16 @@ private static async Task DownloadBlob(string remoteFileName) { var client = new BlobClient(Options.ConnectionString, Options.Container, remoteFileName); var newFilePath = Path.Combine(Options.Path, remoteFileName); + + if (File.Exists(newFilePath) && Options.KeepOld) + { + // Rename old file, add a timestamp suffix, e.g. test.txt -> test_20210901_123456.txt + var newFileName = $"{Path.GetFileNameWithoutExtension(newFilePath)}_{DateTime.Now:yyyyMMdd_HHmmss}{Path.GetExtension(newFilePath)}"; + var newFilePathWithTimestamp = Path.Combine(Options.Path, newFileName); + File.Move(newFilePath, newFilePathWithTimestamp); + AnsiConsole.Write(new Markup($"[yellow]Renamed old file '{newFilePath}' to '{newFilePathWithTimestamp}'.[/]\n")); + } + await client.DownloadToAsync(newFilePath); }