Skip to content

Commit

Permalink
add "send" command (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-kau authored Dec 20, 2023
1 parent 1d8186c commit 8f3987e
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 7 deletions.
20 changes: 18 additions & 2 deletions src/Eppie.CLI/Eppie.CLI/Menu/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,26 @@ internal async Task RestoreActionAsync()
}
}

internal void SendAction()
internal async Task SendActionAsync(string sender, string receiver, string subject)
{
_logger.LogMethodCall();
throw new NotImplementedException();

EmailAddress senderAddress = new(sender);
EmailAddress receiverAddress = new(receiver);

IAccountService accountService = await _coreProvider.TuviMailCore.GetAccountServiceAsync(senderAddress).ConfigureAwait(false);

string body = _application.AskMessageBody();

Message message = new()
{
Subject = subject,
TextBody = body,
};
message.From.Add(senderAddress);
message.To.Add(receiverAddress);

await accountService.SendMessageAsync(message, false, false).ConfigureAwait(false);
}

internal void ListContactsAction()
Expand Down
9 changes: 6 additions & 3 deletions src/Eppie.CLI/Eppie.CLI/Menu/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ private IAsyncParser Create()
action: (cmd) => _actions.ListAccountsActionAsync()),
CreateAsyncCommand(parser, MenuCommand.AddAccount, _resourceLoader.Strings.AddAccountDescription,
action: (cmd) => _actions.AddAccountActionAsync(MenuCommand.CommandAddAccountOptions.GetTypeValue(cmd)),
options: MenuCommand.CommandAddAccountOptions.GetOptions(parser)),
options: MenuCommand.CommandAddAccountOptions.GetOptions(parser, _resourceLoader)),
CreateAsyncCommand(parser, MenuCommand.Restore, _resourceLoader.Strings.RestoreDescription,
action: (cmd) => _actions.RestoreActionAsync()),
CreateCommand(parser, MenuCommand.Send, string.Empty,
action: (cmd) => _actions.SendAction()),
CreateAsyncCommand(parser, MenuCommand.Send, _resourceLoader.Strings.SendDescription,
action: (cmd) => _actions.SendActionAsync(MenuCommand.CommandSendOptions.GetSenderValue(cmd),
MenuCommand.CommandSendOptions.GetReceiverValue(cmd),
MenuCommand.CommandSendOptions.GetSubjectValue(cmd)),
options: MenuCommand.CommandSendOptions.GetOptions(parser, _resourceLoader)),
CreateCommand(parser, MenuCommand.ListContacts, string.Empty,
action: (cmd) => _actions.ListContactsAction()),
CreateCommand(parser, MenuCommand.ShowMessage, string.Empty,
Expand Down
42 changes: 40 additions & 2 deletions src/Eppie.CLI/Eppie.CLI/Menu/MenuCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

using System.Diagnostics;

using Eppie.CLI.Services;

using Tuvi.Toolkit.Cli.CommandLine;

namespace Eppie.CLI.Menu
Expand Down Expand Up @@ -48,13 +50,14 @@ public enum AccountType

public static readonly IReadOnlyCollection<string> TypeOptionNames = new[] { "-t", "--type", "/Type" };

public static IReadOnlyCollection<IOption> GetOptions(IAsyncParser parser)
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<AccountType>(TypeOptionNames, isRequired: true)
parser.CreateOption<AccountType>(TypeOptionNames, isRequired: true, description: resourceLoader.Strings.AccountTypeDescription)
};
}

Expand All @@ -64,6 +67,41 @@ public static AccountType GetTypeValue(IAsyncCommand cmd)
}
}

public static class CommandSendOptions
{
public static readonly IReadOnlyCollection<string> SenderOptionNames = new[] { "-s", "--sender", "/Sender" };
public static readonly IReadOnlyCollection<string> ReceiverOptionNames = new[] { "-r", "--receiver", "/Receiver" };
public static readonly IReadOnlyCollection<string> SubjectOptionNames = new[] { "-t", "--subject", "/Subject" };

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<string>(SenderOptionNames, isRequired: true, description: resourceLoader.Strings.SenderDescription),
parser.CreateOption<string>(ReceiverOptionNames, isRequired: true, description: resourceLoader.Strings.ReceiverDescription),
parser.CreateOption<string>(SubjectOptionNames, isRequired: true, description: resourceLoader.Strings.SubjectDescription),
};
}

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

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

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

private static T? GetOptionValue<T>(IAsyncCommand cmd, string name)
{
ArgumentNullException.ThrowIfNull(cmd);
Expand Down
24 changes: 24 additions & 0 deletions src/Eppie.CLI/Eppie.CLI/Resources/Program.resx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@
<value>All local data will be lost. Do you want to continue? (y/n): </value>
<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>
</data>
<data name="MenuOption.SenderDescription" xml:space="preserve">
<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>
</data>
<data name="Message.AskNewPassword" xml:space="preserve">
<value>Enter your new vault password: </value>
<comment>This message prompts the user to enter a new vault password.</comment>
Expand All @@ -246,10 +258,22 @@
<value>Restore your Eppie account.</value>
<comment>Description of the 'restore' command in the help.</comment>
</data>
<data name="Menu.SendDescription" xml:space="preserve">
<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>
</data>
<data name="Menu.Description" xml:space="preserve">
<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>
</data>
<data name="Message.AskMessageBody" xml:space="preserve">
<value>Enter a text message: </value>
<comment>This message prompts the user to enter a text message.</comment>
</data>
</root>
7 changes: 7 additions & 0 deletions src/Eppie.CLI/Eppie.CLI/Services/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ internal bool ConfirmReset()
return ReadBoolValue(_resourceLoader.Strings.ConfirmReset);
}

internal string AskMessageBody()
{
_logger.LogMethodCall();

return ReadValue(_resourceLoader.Strings.AskMessageBody);
}

internal void PrintAccounts(IReadOnlyCollection<Account> accounts)
{
_logger.LogMethodCall();
Expand Down
18 changes: 18 additions & 0 deletions src/Eppie.CLI/Eppie.CLI/Services/ResourceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ internal string GetLogo(string name, string version)
private string? _askRestorePath;
internal string AskRestorePath => _askRestorePath ??= _localizer.LoadString(GetStringResourceName());

private string? _askMessageBody;
internal string AskMessageBody => _askMessageBody ??= _localizer.LoadString(GetStringResourceName());

private string? _invalidPassword;
internal string InvalidPassword => _invalidPassword ??= _localizer.LoadString(GetStringResourceName(category: "Warning"));

Expand Down Expand Up @@ -158,6 +161,21 @@ internal string GetMenuDescription(string name, string version)
private string? _restoreDescription;
internal string RestoreDescription => _restoreDescription ??= _localizer.LoadString(GetStringResourceName(category: "Menu"));

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

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

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

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

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

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

0 comments on commit 8f3987e

Please sign in to comment.