diff --git a/PWManager.Application/Services/Interfaces/IClipboard.cs b/PWManager.Application/Services/Interfaces/IClipboard.cs new file mode 100644 index 0000000..335d16d --- /dev/null +++ b/PWManager.Application/Services/Interfaces/IClipboard.cs @@ -0,0 +1,8 @@ +namespace PWManager.Application.Services.Interfaces; + +public interface IClipboard { + + void WriteClipboard(string val); + + void ClearClipboard(); +} \ No newline at end of file diff --git a/PWManager.CLI/Abstractions/Clipboard.cs b/PWManager.CLI/Abstractions/Clipboard.cs new file mode 100644 index 0000000..80b15c6 --- /dev/null +++ b/PWManager.CLI/Abstractions/Clipboard.cs @@ -0,0 +1,31 @@ +using PWManager.Application.Exceptions; +using PWManager.Application.Services.Interfaces; + +namespace PWManager.CLI.Abstractions; + +public class Clipboard : IClipboard { + + public void WriteClipboard(string val) { + if (OperatingSystem.IsWindows()) { + var escaped = val.Replace("\n", "\\n").Replace("\"", "\"\""); + $"echo \"{escaped}\" | Set-Clipboard" + .PowerShell(); + } + else if(OperatingSystem.IsMacOS()) { + var escaped = val.Replace("\n", "\\n").Replace("\"", "\\\""); + $"echo -n \"{escaped}\" | pbcopy" + .Bash(); + }else if (OperatingSystem.IsLinux()) { + var escaped = val.Replace("\n", "\\n").Replace("\"", "\\\""); + $"echo -n \"{escaped}\" | xclip -selection c" + .Bash(); + } + else { + throw new UserFeedbackException("Your Operating System does not support the clipboard functionality"); + } + } + + public void ClearClipboard() { + WriteClipboard(" "); + } +} \ No newline at end of file diff --git a/PWManager.CLI/Abstractions/OperatingSystem.cs b/PWManager.CLI/Abstractions/OperatingSystem.cs new file mode 100644 index 0000000..005cff5 --- /dev/null +++ b/PWManager.CLI/Abstractions/OperatingSystem.cs @@ -0,0 +1,14 @@ +using System.Runtime.InteropServices; + +namespace PWManager.CLI.Abstractions; + +public static class OperatingSystem { + public static bool IsWindows() => + RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + + public static bool IsMacOS() => + RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + + public static bool IsLinux() => + RuntimeInformation.IsOSPlatform(OSPlatform.Linux); +} \ No newline at end of file diff --git a/PWManager.CLI/Abstractions/Shell.cs b/PWManager.CLI/Abstractions/Shell.cs new file mode 100644 index 0000000..be5e3b3 --- /dev/null +++ b/PWManager.CLI/Abstractions/Shell.cs @@ -0,0 +1,40 @@ +using System.Diagnostics; + +namespace PWManager.CLI.Abstractions; + +public static class Shell { + + public static string Bash(this string cmd) { + var escapedArgs = cmd.Replace("\"", "\\\""); + string result = Run("/bin/bash", $"-c \"{escapedArgs}\""); + return result; + } + + public static string Bat(this string cmd) { + var escapedArgs = cmd.Replace("\"", "\\\""); + string result = Run("cmd.exe", $"/c \"{escapedArgs}\""); + return result; + } + + public static string PowerShell(this string cmd) { + var escapedArgs = cmd.Replace("\"", "\\\""); + string result = Run("powershell.exe", $"/c \"{escapedArgs}\""); + return result; + } + + private static string Run (string filename, string arguments){ + var process = new Process() { + StartInfo = new ProcessStartInfo { + FileName = filename, + Arguments = arguments, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = false, + } + }; + process.Start(); + string result = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return result; + } +} \ No newline at end of file diff --git a/PWManager.CLI/Program.cs b/PWManager.CLI/Program.cs index 0c2b374..26bf29b 100644 --- a/PWManager.CLI/Program.cs +++ b/PWManager.CLI/Program.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using PWManager.Application; using PWManager.Application.Context; +using PWManager.Application.Services.Interfaces; using PWManager.CLI; using PWManager.CLI.Abstractions; using PWManager.CLI.ExtensionMethods; @@ -12,6 +13,7 @@ services.AddSingleton(); // Add all services to DI services.AddSingleton(); +services.AddTransient(); // Add Layers services.AddApplicationServices(); services.AddDataServices();