Skip to content

Commit

Permalink
add KeepOld option, fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
EdiWang committed Dec 28, 2023
1 parent 0bb9295 commit 97bbf06
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Edi.AzureBlobSync/Edi.AzureBlobSync.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Version>1.18.2</Version>
<Version>1.19.0</Version>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackAsTool>true</PackAsTool>
Expand Down
3 changes: 3 additions & 0 deletions src/Edi.AzureBlobSync/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
26 changes: 22 additions & 4 deletions src/Edi.AzureBlobSync/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.");
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 97bbf06

Please sign in to comment.