Skip to content

Commit

Permalink
Merge pull request #8 from marcos-venicius/aliases
Browse files Browse the repository at this point in the history
Aliases
  • Loading branch information
marcos-venicius committed Mar 26, 2023
2 parents 73dbf1a + c132e40 commit 067e175
Show file tree
Hide file tree
Showing 29 changed files with 705 additions and 237 deletions.
66 changes: 40 additions & 26 deletions GrpcTodo.CLI/ActionRunner.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using GrpcTodo.CLI.Enums;
using GrpcTodo.CLI.Lib;
using GrpcTodo.CLI.UseCases;
using GrpcTodo.CLI.Services;
using GrpcTodo.CLI.UseCases.AccountCreate;
using GrpcTodo.CLI.UseCases.AccountLogin;
using GrpcTodo.CLI.UseCases.AccountLogout;
using GrpcTodo.CLI.UseCases.AccountTokenUpdate;
using GrpcTodo.CLI.UseCases.AliasCreate;
using GrpcTodo.CLI.UseCases.AliasList;
using GrpcTodo.CLI.UseCases.AliasRemove;

namespace GrpcTodo.CLI;

Expand All @@ -10,54 +16,62 @@ internal class ActionRunner
private readonly AccountLoginUseCase _accountLoginUseCase;
private readonly AccountLogoutUseCase _accountLogoutUseCase;
private readonly AccountUpdateTokenUseCase _accountUpdateTokenUseCase;
private readonly AliasCreateUseCase _aliasCreateUseCase;
private readonly AliasListUseCase _aliasListUseCase;
private readonly AliasRemoveUseCase _aliasRemoveUseCase;

private readonly ConfigsManager _configsManager;
private readonly Parameters _parameters;
private readonly CommandReader _commandReader;

public ActionRunner(ConfigsManager configsManager, Parameters parameters)
public ActionRunner(ConfigsManager configsManager, CommandReader commandReader, Parameters parameters)
{
_configsManager = configsManager;
_parameters = parameters;
_commandReader = commandReader;

_createAccountUseCase = new AccountCreateUseCase(_configsManager);
_accountLoginUseCase = new AccountLoginUseCase(_configsManager);
_accountLogoutUseCase = new AccountLogoutUseCase(_configsManager);
_accountUpdateTokenUseCase = new AccountUpdateTokenUseCase(_configsManager);
_createAccountUseCase = new AccountCreateUseCase(configsManager, parameters);
_accountLoginUseCase = new AccountLoginUseCase(configsManager, parameters);
_accountLogoutUseCase = new AccountLogoutUseCase(configsManager);
_accountUpdateTokenUseCase = new AccountUpdateTokenUseCase(configsManager, parameters);
_aliasCreateUseCase = new AliasCreateUseCase(configsManager, commandReader);
_aliasListUseCase = new AliasListUseCase(configsManager, commandReader);
_aliasRemoveUseCase = new AliasRemoveUseCase(configsManager, commandReader);
}

public async Task Run(Command? action, string command)
public Task Run(Command? action)
{
switch (action)
{
case null:
throw new InvalidCommandException(@$"command ""{command}"" does not exists");
throw new InvalidCommandException(@$"command/alias ""{_commandReader}"" does not exists");
case Command.Logout:
_accountLogoutUseCase.Execute();
break;
return _accountLogoutUseCase.ExecuteAsync();
case Command.UpdateToken:
await _accountUpdateTokenUseCase.ExecuteAsync(_parameters);
break;
return _accountUpdateTokenUseCase.ExecuteAsync();
case Command.CreateAccount:
await _createAccountUseCase.ExecuteAsync(_parameters);
break;
return _createAccountUseCase.ExecuteAsync();
case Command.Login:
await _accountLoginUseCase.ExecuteAsync(_parameters);
break;
return _accountLoginUseCase.ExecuteAsync();
case Command.CreateAlias:
return _aliasCreateUseCase.ExecuteAsync();
case Command.ListAliases:
return _aliasListUseCase.ExecuteAsync();
case Command.RemoveAlias:
return _aliasRemoveUseCase.ExecuteAsync();
case Command.ListAllTasks:
Console.WriteLine("list all tasks");
Console.WriteLine("no implemented yet: list all tasks");
break;
case Command.CreateTask:
Console.WriteLine("create task");
Console.WriteLine("no implemented yet: create task");
break;
case Command.CompleteTask:
Console.WriteLine("complete task");
Console.WriteLine("no implemented yet: complete task");
break;
case Command.UncompleteTask:
Console.WriteLine("uncomplete task");
Console.WriteLine("no implemented yet: uncomplete task");
break;
case Command.DeleteTask:
Console.WriteLine("delete task");
Console.WriteLine("no implemented yet: delete task");
break;
}

return Task.CompletedTask;
}
}
6 changes: 3 additions & 3 deletions GrpcTodo.CLI/CLI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ public async Task Run()
{
var parameters = _argsParams.Read();

var commandReader = new CommandReader(Menu, _args);
var commandReader = new CommandReader(Menu, _args, _configsManager);

try
{
var actionRunner = new ActionRunner(_configsManager, parameters);
var actionRunner = new ActionRunner(_configsManager, commandReader, parameters);

if (commandReader.HasPossibleOptions())
{
var menuOption = commandReader.Read();

await actionRunner.Run(menuOption?.Command, commandReader.ToString());
await actionRunner.Run(menuOption?.Command);
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions GrpcTodo.CLI/Enums/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace GrpcTodo.CLI.Enums;

public enum Command
{
ListAliases,
RemoveAlias,
CreateAlias,
CreateAccount,
Login,
Logout,
Expand Down
7 changes: 7 additions & 0 deletions GrpcTodo.CLI/Enums/ConfigKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace GrpcTodo.CLI.Enums;

public enum ConfigKey
{
Item = 0,
Alias
}
81 changes: 66 additions & 15 deletions GrpcTodo.CLI/Lib/ConfigsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,30 @@ private void CreateConfigFilesIfNotExists()
{
using var file = File.CreateText(_configsFilePath);

file.Write("# please, do not edit this file manually");
file.WriteLine("# please, do not edit this file manually");
file.WriteLine("@alias logout=account logout");
file.WriteLine("@alias login=account login");
file.WriteLine("@alias signup=account create");

file.Close();
}
}

private (string value, ushort index) GetKey(string line)
private (string value, ushort index) GetKey(ConfigKey prefix, string line)
{
line = line.Replace(prefix.Translate(), "").Trim();

var key = "";
ushort index = 2;
var chr = line[2];
ushort index = 0;
var chr = line[index];

while (chr != '=')
{
key += chr;
chr = line[++index];
}

return (key, (ushort)(index + 1));
return (key, (ushort)(index + 1 + prefix.Translate().Length));
}

private string GetValue(string line, ushort startIndex)
Expand All @@ -65,7 +71,7 @@ private string ReadFile(string filePath)
return Encoding.UTF8.GetString(bytes);
}

private Info? GetDetailed(string key)
private Info? GetDetailed(ConfigKey prefix, string key)
{
var text = ReadFile(_configsFilePath);

Expand All @@ -75,9 +81,9 @@ private string ReadFile(string filePath)
{
var line = lines[i];

if (line.StartsWith("@ "))
if (line.StartsWith(prefix.Translate()))
{
var lineKey = GetKey(line);
var lineKey = GetKey(prefix, line);

if (lineKey.value == key)
{
Expand All @@ -91,18 +97,50 @@ private string ReadFile(string filePath)
return null;
}

public string? Get(string key)
public string? GetItem(ConfigKey prefix, string key)
{
CreateConfigFilesIfNotExists();

return GetDetailed(prefix, key)?.Value;
}

public List<(string key, string value)> ReadPrefixes(ConfigKey prefix)
{
CreateConfigFilesIfNotExists();

return GetDetailed(key)?.Value;
var items = new List<(string, string)>();

var text = ReadFile(_configsFilePath);

string[] lines = text.Replace("\r\n", "\n").Split("\n");

foreach (var line in lines)
{
if (line.StartsWith(prefix.Translate()))
{
var lineWithoutPrefix = line.Replace(prefix.Translate(), "").Trim();

var keyValuePair = lineWithoutPrefix.Split("=");

// prevent to try parse a wrong item
if (keyValuePair.Length != 2)
continue;

var key = keyValuePair[0].Trim();
var value = keyValuePair[1].Trim();

items.Add((key, value));
}
}

return items;
}

public void Remove(string key)
public void RemoveItem(ConfigKey prefix, string key)
{
CreateConfigFilesIfNotExists();

var keyOnFile = GetDetailed(key);
var keyOnFile = GetDetailed(prefix, key);

if (keyOnFile is null)
return;
Expand All @@ -128,11 +166,11 @@ public void Remove(string key)
fs.Close();
}

public void Set(string key, string value)
public void SetItem(ConfigKey prefix, string key, string value)
{
CreateConfigFilesIfNotExists();

var keyOnFile = GetDetailed(key);
var keyOnFile = GetDetailed(prefix, key);

StringBuilder newFileContent = new();

Expand All @@ -157,7 +195,7 @@ public void Set(string key, string value)
var currentFileContent = ReadFile(_configsFilePath);

newFileContent.AppendLine(currentFileContent);
newFileContent.AppendLine($"@ {key}={value}");
newFileContent.AppendLine($"{prefix.Translate()}{key}={value}");
}

using var fs = new FileStream(_configsFilePath, FileMode.Create, FileAccess.Write);
Expand All @@ -168,4 +206,17 @@ public void Set(string key, string value)

fs.Close();
}
}

public static class ConfigsManagerExtension
{
public static string Translate(this ConfigKey configKey)
{
return configKey switch
{
ConfigKey.Alias => "@alias ",
ConfigKey.Item => "@item ",
_ => "@item "
};
}
}
Loading

0 comments on commit 067e175

Please sign in to comment.