From edf87fae27b287724c245e109c6823536d242a8f Mon Sep 17 00:00:00 2001 From: FRFlo <62435410+FRFlo@users.noreply.github.com> Date: Tue, 12 Mar 2024 10:56:47 +0100 Subject: [PATCH] feat!: Client per I/O (#27) * feat(Config.cs): Usage of CopyIO * feat(Program.cs): Adaptation to the new system --- Copy/Config.cs | 20 +++++++--------- Copy/Program.cs | 54 ++++++++++++++++++++++++++++++------------ Copy/Types/CopyIO.cs | 32 +++++++++++++++++++++++++ Copy/Types/CopyTask.cs | 9 ++----- 4 files changed, 81 insertions(+), 34 deletions(-) create mode 100644 Copy/Types/CopyIO.cs diff --git a/Copy/Config.cs b/Copy/Config.cs index 093f2ba..f09ace6 100644 --- a/Copy/Config.cs +++ b/Copy/Config.cs @@ -101,16 +101,14 @@ 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" @@ -118,9 +116,8 @@ public static string GetDefault() }, new CopyTask() { - Source = "source", - Destination = "destination", - Client = "Local", + Source = new CopyIO("Local", "source"), + Destination = new CopyIO("Local", "destination"), Delete = true, Filter = new CopyFilter() { @@ -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); diff --git a/Copy/Program.cs b/Copy/Program.cs index 9fcc73b..cf0a300 100644 --- a/Copy/Program.cs +++ b/Copy/Program.cs @@ -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"); diff --git a/Copy/Types/CopyIO.cs b/Copy/Types/CopyIO.cs new file mode 100644 index 0000000..9efcd09 --- /dev/null +++ b/Copy/Types/CopyIO.cs @@ -0,0 +1,32 @@ +using Newtonsoft.Json; + +namespace Copy.Types +{ + /// + /// Input/output for copying files. + /// + public class CopyIO + { + /// + /// Create a new instance of the class. + /// + /// Client to use. + /// Path to the folder. + public CopyIO(string client, string path) + { + Client = client; + Path = path; + } + + /// + /// Client to use. + /// + [JsonProperty(PropertyName = "Client", Required = Required.Always)] + public string Client { get; set; } + /// + /// Path to the folder. + /// + [JsonProperty(PropertyName = "Path", Required = Required.Always)] + public string Path { get; set; } + } +} diff --git a/Copy/Types/CopyTask.cs b/Copy/Types/CopyTask.cs index b95ced9..d7d15bc 100644 --- a/Copy/Types/CopyTask.cs +++ b/Copy/Types/CopyTask.cs @@ -7,21 +7,16 @@ namespace Copy.Types /// public class CopyTask { - /// - /// Client to use for the task. - /// - [JsonProperty(PropertyName = "Client", Required = Required.Always)] - public required string Client { get; set; } /// /// Source of files to copy. /// [JsonProperty(PropertyName = "Source", Required = Required.Always)] - public required string Source { get; set; } + public required CopyIO Source { get; set; } /// /// Destination of files to copy. /// [JsonProperty(PropertyName = "Destination", Required = Required.Always)] - public required string Destination { get; set; } + public required CopyIO Destination { get; set; } /// /// Whether to delete files after copying. ///