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

Import command #94

Merged
merged 1 commit into from
Dec 21, 2023
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
10 changes: 8 additions & 2 deletions src/Eppie.CLI/Eppie.CLI/Menu/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,16 @@ internal void ShowMessagesAction()
throw new NotImplementedException();
}

internal void ImportAction()
internal async Task ImportKeyBundleFromFileAsync(FileInfo file)
{
_logger.LogMethodCall();
throw new NotImplementedException();
await Task.Run(() => ImportBundle(file.FullName)).ConfigureAwait(false);
}

private void ImportBundle(string fileAddress)
{
using MemoryStream keyIn = new(File.ReadAllBytes(fileAddress));
_coreProvider.TuviMailCore.GetSecurityManager().ImportPgpKeyRingBundle(keyIn);
}

private async Task AddEmailAccountAsync()
Expand Down
5 changes: 3 additions & 2 deletions src/Eppie.CLI/Eppie.CLI/Menu/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ private IAsyncParser Create()
action: (cmd) => _actions.ShowMessageAction()),
CreateCommand(parser, MenuCommand.ShowMessages, string.Empty,
action: (cmd) => _actions.ShowMessagesAction()),
CreateCommand(parser, MenuCommand.Import, string.Empty,
action: (cmd) => _actions.ImportAction()),
CreateAsyncCommand(parser, MenuCommand.Import, _resourceLoader.Strings.ImportDescription,
action: (cmd) => _actions.ImportKeyBundleFromFileAsync(MenuCommand.CommandImportOptions.GetFileValue(cmd)),
options: MenuCommand.CommandImportOptions.GetOptions(parser, _resourceLoader)),
}
);

Expand Down
36 changes: 32 additions & 4 deletions src/Eppie.CLI/Eppie.CLI/Menu/MenuCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static IReadOnlyCollection<IOption> GetOptions(IAsyncParser parser, Resou

public static AccountType GetTypeValue(IAsyncCommand cmd)
{
return GetOptionValue<AccountType>(cmd, TypeOptionNames.First());
return GetRequiredOptionValue<AccountType>(cmd, TypeOptionNames.First());
}
}

Expand All @@ -88,20 +88,48 @@ public static IReadOnlyCollection<IOption> GetOptions(IAsyncParser parser, Resou

public static string GetSubjectValue(IAsyncCommand cmd)
{
return GetOptionValue<string>(cmd, SubjectOptionNames.First()) ?? string.Empty;
return GetRequiredOptionValue<string>(cmd, SubjectOptionNames.First());
}

public static string GetSenderValue(IAsyncCommand cmd)
{
return GetOptionValue<string>(cmd, SenderOptionNames.First()) ?? string.Empty;
return GetRequiredOptionValue<string>(cmd, SenderOptionNames.First());
}

public static string GetReceiverValue(IAsyncCommand cmd)
{
return GetOptionValue<string>(cmd, ReceiverOptionNames.First()) ?? string.Empty;
return GetRequiredOptionValue<string>(cmd, ReceiverOptionNames.First());
}
}

public static class CommandImportOptions
{
public static readonly IReadOnlyCollection<string> FileOptionNames = new[] { "-f", "--file", "/File" };

public static IReadOnlyCollection<IOption> GetOptions(IAsyncParser parser, ResourceLoader resourceLoader)
{
Debug.Assert(parser is not null);
Debug.Assert(resourceLoader is not null);

return new IOption[]
{
parser.CreateOption<FileInfo>(FileOptionNames, isRequired: true, description: resourceLoader.Strings.KeyBundleFileDescription)
};
}

public static FileInfo GetFileValue(IAsyncCommand cmd)
{
return GetRequiredOptionValue<FileInfo>(cmd, FileOptionNames.First());
}
}

private static T GetRequiredOptionValue<T>(IAsyncCommand cmd, string name)
{
ArgumentNullException.ThrowIfNull(cmd);
return cmd.GetRequiredValue<T>(name) ?? throw new InvalidOperationException($"The required '{name}' option is missing.");
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "For future use.")]
private static T? GetOptionValue<T>(IAsyncCommand cmd, string name)
{
ArgumentNullException.ThrowIfNull(cmd);
Expand Down
30 changes: 19 additions & 11 deletions src/Eppie.CLI/Eppie.CLI/Resources/Program.resx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<comment>Application description and additional information.</comment>
</data>
<data name="Header.LogoFormat" xml:space="preserve">
<value>Hello, I'm {0} (version {1})</value>
<value>Hello, I'm {0} (version {1}).</value>
<comment>The startup banner or the copyright message.
{0} is a parameter containing the title of the application;
{1} is a parameter containing the version of the application.</comment>
Expand Down Expand Up @@ -235,16 +235,16 @@
<comment>This message prompts the user to confirm to reset the application.</comment>
</data>
<data name="MenuOption.ReceiverDescription" xml:space="preserve">
<value>Receiver address</value>
<comment>Description of the send command option in the help.</comment>
<value>Receiver address.</value>
<comment>Description of the 'send' command option in the help.</comment>
</data>
<data name="MenuOption.SenderDescription" xml:space="preserve">
<value>Sender address</value>
<comment>Description of the send command option in the help.</comment>
<value>Sender address.</value>
<comment>Description of the 'send' command option in the help.</comment>
</data>
<data name="MenuOption.SubjectDescription" xml:space="preserve">
<value>Message subject</value>
<comment>Description of the send command option in the help.</comment>
<value>Message subject.</value>
<comment>Description of the 'send' command option in the help.</comment>
</data>
<data name="Message.AskNewPassword" xml:space="preserve">
<value>Enter your new vault password: </value>
Expand All @@ -259,15 +259,15 @@
<comment>Description of the 'restore' command in the help.</comment>
</data>
<data name="Menu.SendDescription" xml:space="preserve">
<value>Send a message</value>
<value>Send a message.</value>
<comment>Description of the 'send' command in the help.</comment>
</data>
<data name="MenuOption.AccountTypeDescription" xml:space="preserve">
<value>Type of account</value>
<comment>Description of the add-account command option in the help.</comment>
<value>Type of account.</value>
<comment>Description of the 'add-account' command option in the help.</comment>
</data>
<data name="Menu.Description" xml:space="preserve">
<value>{0} (version {1})</value>
<value>{0} (version {1}).</value>
<comment>Description of the main menu in the help.
{0} is a parameter containing the title of the application;
{1} is a parameter containing the version of the application.</comment>
Expand All @@ -276,4 +276,12 @@
<value>Enter a text message: </value>
<comment>This message prompts the user to enter a text message.</comment>
</data>
<data name="Menu.ImportDescription" xml:space="preserve">
<value>Import a key bundle from a file.</value>
<comment>Description of the 'import' command in the help.</comment>
</data>
<data name="MenuOption.KeyBundleFileDescription" xml:space="preserve">
<value>A key bundle file.</value>
<comment>Description of the 'import' command option in the help.</comment>
</data>
</root>
6 changes: 6 additions & 0 deletions src/Eppie.CLI/Eppie.CLI/Services/ResourceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ internal string GetMenuDescription(string name, string version)
private string? _sendDescription;
internal string SendDescription => _sendDescription ??= _localizer.LoadString(GetStringResourceName(category: "Menu"));

private string? _importDescription;
internal string ImportDescription => _importDescription ??= _localizer.LoadString(GetStringResourceName(category: "Menu"));

private string? _accountTypeDescription;
internal string AccountTypeDescription => _accountTypeDescription ??= _localizer.LoadString(GetStringResourceName(category: "MenuOption"));

Expand All @@ -176,6 +179,9 @@ internal string GetMenuDescription(string name, string version)
private string? _subjectDescription;
internal string SubjectDescription => _subjectDescription ??= _localizer.LoadString(GetStringResourceName(category: "MenuOption"));

private string? _keyBundleFileDescription;
internal string KeyBundleFileDescription => _keyBundleFileDescription ??= _localizer.LoadString(GetStringResourceName(category: "MenuOption"));

private static string GetStringResourceName(string category = "Message", [CallerMemberName] string name = "")
{
return string.Join('.', new string?[] { category, name });
Expand Down
Loading