Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Client per I/O #27

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions Copy/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,23 @@ public static string GetDefault()
Tasks = [
new CopyTask()
{
Client = "FTP",
Source = "source",
Destination = "destination",
Source = new CopyIO("FTP", "source"),
Destination = new CopyIO("FTP", "destination"),
Delete = true
},
new CopyTask()
{
Source = "source",
Destination = "destination",
Client = "SFTP",
Source = new CopyIO("SFTP", "source"),
Destination = new CopyIO("SFTP", "destination"),
Filter = new CopyFilter()
{
Name = ".*\\.txt"
}
},
new CopyTask()
{
Source = "source",
Destination = "destination",
Client = "Local",
Source = new CopyIO("Local", "source"),
Destination = new CopyIO("Local", "destination"),
Delete = true,
Filter = new CopyFilter()
{
Expand All @@ -129,9 +126,8 @@ public static string GetDefault()
},
new CopyTask()
{
Source = "source",
Destination = "destination",
Client = "Exchange"
Source = new CopyIO("Exchange", "source"),
Destination = new CopyIO("Exchange", "destination"),
}
]
}, Formatting.Indented);
Expand Down
54 changes: 39 additions & 15 deletions Copy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,60 @@ public static void Main(string[] args)

foreach (CopyTask task in Config.Tasks)
{
Logger.Info($"Copying files from {task.Source} to {task.Destination} using {task.Client}");
if (!Clients.TryGetValue(task.Client, out IClient? client))
Logger.Info($"Copying files from {task.Source.Path} ({task.Source.Client}) to {task.Destination.Path} ({task.Destination.Client})");
if (!Clients.TryGetValue(task.Source.Client, out IClient? sourceClient))
{
Logger.Error($"Client {task.Source} not found");
throw new ClientNotFoundException($"Client {task.Source} not found");
Logger.Error($"Source client {task.Source.Client} not found");
throw new ClientNotFoundException($"Source client {task.Source.Client} not found");
}
if (!Clients.TryGetValue(task.Destination.Client, out IClient? destinationClient))
{
Logger.Error($"Destination client {task.Destination.Client} not found");
throw new ClientNotFoundException($"Destination client {task.Destination.Client} not found");
}

bool sameClient = task.Source.Client == task.Destination.Client;
Logger.Debug($"Source and destination {(sameClient ? "are" : "aren't")} the same client.");

string[] files = client.ListFiles(task.Source, task.Filter);
string[] files = sourceClient.ListFiles(task.Source.Path, task.Filter);

Logger.Info($"Treating {files.Length} files");

foreach (string file in files)
{
string destination = Path.Combine(task.Destination, Path.GetFileName(file));
Logger.Debug($"Copying {file} to {task.Destination}");
Stream content = client.GetFile(file);
client.PutFile(destination, content);
string destination = Path.Combine(task.Destination.Path, Path.GetFileName(file));
Logger.Debug($"Copying {file} to {task.Destination.Path}");

if (task.Delete)
if (sameClient)
{
Logger.Debug($"Deleting {file}");
client.DeleteFile(file);
if (task.Delete)
{
Logger.Debug($"Moving {file} to {destination}");
sourceClient.MoveFile(file, destination);
}
else
{
Logger.Debug($"Copying {file} to {destination}");
sourceClient.CopyFile(file, destination);
}
}
else
{
Stream content = sourceClient.GetFile(file);
Logger.Debug($"Putting {file} ({task.Source.Client}) to {task.Destination.Path} ({destination})");
destinationClient.PutFile(destination, content);

content.Close();
if (task.Delete)
{
Logger.Debug($"Deleting {file}");
sourceClient.DeleteFile(file);
}

Logger.Debug($"Copied {file} to {task.Destination}");
content.Close();
}
}

Logger.Info($"Copied {files.Length} files from {task.Source} to {task.Destination} using {task.Client}");
Logger.Info("All files treated");
}

Logger.Info("All tasks executed");
Expand Down
32 changes: 32 additions & 0 deletions Copy/Types/CopyIO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Newtonsoft.Json;

namespace Copy.Types
{
/// <summary>
/// Input/output for copying files.
/// </summary>
public class CopyIO
{
/// <summary>
/// Create a new instance of the class.
/// </summary>
/// <param name="client">Client to use.</param>
/// <param name="path">Path to the folder.</param>
public CopyIO(string client, string path)
{
Client = client;
Path = path;
}

/// <summary>
/// Client to use.
/// </summary>
[JsonProperty(PropertyName = "Client", Required = Required.Always)]
public string Client { get; set; }
/// <summary>
/// Path to the folder.
/// </summary>
[JsonProperty(PropertyName = "Path", Required = Required.Always)]
public string Path { get; set; }
}
}
9 changes: 2 additions & 7 deletions Copy/Types/CopyTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ namespace Copy.Types
/// </summary>
public class CopyTask
{
/// <summary>
/// Client to use for the task.
/// </summary>
[JsonProperty(PropertyName = "Client", Required = Required.Always)]
public required string Client { get; set; }
/// <summary>
/// Source of files to copy.
/// </summary>
[JsonProperty(PropertyName = "Source", Required = Required.Always)]
public required string Source { get; set; }
public required CopyIO Source { get; set; }
/// <summary>
/// Destination of files to copy.
/// </summary>
[JsonProperty(PropertyName = "Destination", Required = Required.Always)]
public required string Destination { get; set; }
public required CopyIO Destination { get; set; }
/// <summary>
/// Whether to delete files after copying.
/// </summary>
Expand Down