|
| 1 | +using System.Runtime.Serialization; |
| 2 | +using System.Security.Cryptography; |
| 3 | +using CliFx.Infrastructure; |
| 4 | +using OwlCore.Storage; |
| 5 | +using SecureFolderFS.Core; |
| 6 | +using SecureFolderFS.Sdk.Helpers; |
| 7 | +using SecureFolderFS.Shared.Models; |
| 8 | +using SecureFolderFS.Storage.SystemStorageEx; |
| 9 | +using SecureFolderFS.Storage.VirtualFileSystem; |
| 10 | + |
| 11 | +namespace SecureFolderFS.Cli; |
| 12 | + |
| 13 | +internal static class CliCommandHelpers |
| 14 | +{ |
| 15 | + public static IFolder GetVaultFolder(string path) |
| 16 | + { |
| 17 | + var fullPath = Path.GetFullPath(path); |
| 18 | + return new SystemFolderEx(fullPath); |
| 19 | + } |
| 20 | + |
| 21 | + public static Dictionary<string, object> BuildMountOptions(string volumeName, bool readOnly, string? mountPoint) |
| 22 | + { |
| 23 | + var options = new Dictionary<string, object> |
| 24 | + { |
| 25 | + [nameof(VirtualFileSystemOptions.VolumeName)] = FormattingHelpers.SanitizeVolumeName(volumeName, "Vault"), |
| 26 | + [nameof(VirtualFileSystemOptions.IsReadOnly)] = readOnly |
| 27 | + }; |
| 28 | + |
| 29 | + if (!string.IsNullOrWhiteSpace(mountPoint)) |
| 30 | + options["MountPoint"] = Path.GetFullPath(mountPoint); |
| 31 | + |
| 32 | + return options; |
| 33 | + } |
| 34 | + |
| 35 | + public static VaultOptions BuildVaultOptions(string[] methods, string vaultId, string? contentCipher, string? fileNameCipher) |
| 36 | + { |
| 37 | + return new VaultOptions |
| 38 | + { |
| 39 | + UnlockProcedure = new AuthenticationMethod(methods, null), |
| 40 | + VaultId = vaultId, |
| 41 | + ContentCipherId = string.IsNullOrWhiteSpace(contentCipher) |
| 42 | + ? Core.Cryptography.Constants.CipherId.AES_GCM |
| 43 | + : contentCipher, |
| 44 | + FileNameCipherId = string.IsNullOrWhiteSpace(fileNameCipher) |
| 45 | + ? Core.Cryptography.Constants.CipherId.AES_SIV |
| 46 | + : fileNameCipher, |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + public static int HandleException(Exception ex, IConsole console, CliGlobalOptions options) |
| 51 | + { |
| 52 | + switch (ex) |
| 53 | + { |
| 54 | + case CryptographicException: |
| 55 | + case FormatException: |
| 56 | + CliOutput.Error(console, options, ex.Message); |
| 57 | + return CliExitCodes.AuthenticationFailure; |
| 58 | + |
| 59 | + case FileNotFoundException: |
| 60 | + case DirectoryNotFoundException: |
| 61 | + case SerializationException: |
| 62 | + CliOutput.Error(console, options, ex.Message); |
| 63 | + return CliExitCodes.VaultUnreadable; |
| 64 | + |
| 65 | + case NotSupportedException: |
| 66 | + CliOutput.Error(console, options, ex.Message); |
| 67 | + return CliExitCodes.MountFailure; |
| 68 | + |
| 69 | + case InvalidOperationException: |
| 70 | + case ArgumentException: |
| 71 | + CliOutput.Error(console, options, ex.Message); |
| 72 | + return CliExitCodes.BadArguments; |
| 73 | + |
| 74 | + default: |
| 75 | + CliOutput.Error(console, options, ex.ToString()); |
| 76 | + return CliExitCodes.GeneralError; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
0 commit comments