Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Cache CLI input values (#1496)
Browse files Browse the repository at this point in the history
Signed-off-by: Uditha Atukorala <ua@nuked.zone>
  • Loading branch information
Uditha Atukorala authored and l4l committed Jun 26, 2018
1 parent e7e3094 commit 04c3bea
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 93 deletions.
58 changes: 42 additions & 16 deletions iroha-cli/interactive/impl/interactive_common_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <numeric>
#include <utility>

#include "common/types.hpp"
#include "interactive/interactive_common_cli.hpp"
#include "parser/parser.hpp"

Expand All @@ -35,14 +37,25 @@ namespace iroha_cli {
int default_port) {
return {
// commonParamsMap
{SAVE_CODE, {"Path to save json file"}},
{SAVE_CODE, makeParamsDescription({"Path to save json file"})},
{SEND_CODE,
{"Peer address (" + default_ip + ")",
"Peer port (" + std::to_string(default_port) + ")"}}
{ParamData({"Peer address", default_ip}),
ParamData({"Peer port", std::to_string(default_port)})}}
// commonParamsMap
};
}

ParamsDescription makeParamsDescription(
const std::vector<std::string> &params) {
return std::accumulate(params.begin(),
params.end(),
ParamsDescription{},
[](auto &&acc, auto &el) {
acc.push_back(ParamData({el, {}}));
return std::forward<decltype(acc)>(acc);
});
}

void handleEmptyCommand() {
std::cout << "Put not empty command" << std::endl;
}
Expand All @@ -57,16 +70,15 @@ namespace iroha_cli {

bool isBackOption(std::string line) {
auto command = parser::parseFirstCommand(std::move(line));
return command
and (*command == "0" or *command == BACK_CODE);
return command and (*command == "0" or *command == BACK_CODE);
}

void printCommandParameters(std::string &command,
std::vector<std::string> parameters) {
const ParamsDescription &parameters) {
std::cout << "Run " << command
<< " with following parameters: " << std::endl;
std::for_each(parameters.begin(), parameters.end(), [](auto el) {
std::cout << " " << el << std::endl;
std::cout << " " << el.message << std::endl;
});
}

Expand All @@ -87,12 +99,20 @@ namespace iroha_cli {
return line;
}

boost::optional<std::string> promptString(const ParamData &param) {
std::string message = param.message;
if (not param.cache.empty()) {
message += " (" + param.cache + ")";
}
return promptString(message);
}

void printEnd() {
std::cout << "--------------------" << std::endl;
}

boost::optional<std::pair<std::string, uint16_t>> parseIrohaPeerParams(
ParamsDescription params,
std::vector<std::string> params,
const std::string &default_ip,
int default_port) {
const auto &address = params[0].empty() ? default_ip : params[0];
Expand All @@ -107,9 +127,8 @@ namespace iroha_cli {
}

boost::optional<std::vector<std::string>> parseParams(
std::string line, std::string command_name, ParamsMap params_map) {
auto params_description =
findInHandlerMap(command_name, std::move(params_map));
std::string line, std::string command_name, ParamsMap &params_map) {
auto params_description = findInHandlerMap(command_name, params_map);
if (not params_description) {
// Report no params where found for this command
std::cout << "Command params not found" << std::endl;
Expand All @@ -122,11 +141,18 @@ namespace iroha_cli {
std::vector<std::string> params;
std::for_each(params_description.value().begin(),
params_description.value().end(),
[&params](auto param) {
auto val = promptString(param);
if (val and not val.value().empty()) {
params.push_back(val.value());
}
[&params](auto &param) {
using namespace iroha;
promptString(param) | [&](auto &val) {
if (not val.empty()) {
// Update input cache
param.cache = val;
params.push_back(val);
} else if (not param.cache.empty()) {
// Input cache is not empty, use cached value
params.push_back(param.cache);
}
};
});
if (params.size() != params_description.value().size()) {
// Wrong params passed
Expand Down
43 changes: 21 additions & 22 deletions iroha-cli/interactive/impl/interactive_query_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,24 @@ namespace iroha_cli {
const auto role_id = "Requested role name";
const auto tx_hashes = "Requested tx hashes";

query_params_descriptions_ = {
{GET_ACC, {acc_id}},
{GET_ACC_AST, {acc_id, ast_id}},
{GET_ACC_AST_TX, {acc_id, ast_id}},
{GET_ACC_TX, {acc_id}},
{GET_TX, {tx_hashes}},
{GET_ACC_SIGN, {acc_id}},
{GET_ROLES, {}},
{GET_AST_INFO, {ast_id}},
{GET_ROLE_PERM, {role_id}}
// query_params_descriptions_
query_params_map_ = {
{GET_ACC, makeParamsDescription({acc_id})},
{GET_ACC_AST, makeParamsDescription({acc_id, ast_id})},
{GET_ACC_AST_TX, makeParamsDescription({acc_id, ast_id})},
{GET_ACC_TX, makeParamsDescription({acc_id})},
{GET_TX, makeParamsDescription({tx_hashes})},
{GET_ACC_SIGN, makeParamsDescription({acc_id})},
{GET_ROLES, makeParamsDescription({})},
{GET_AST_INFO, makeParamsDescription({ast_id})},
{GET_ROLE_PERM, makeParamsDescription({role_id})}
// query_params_map_
};

query_handlers_ = {
{GET_ACC, &InteractiveQueryCli::parseGetAccount},
{GET_ACC_AST, &InteractiveQueryCli::parseGetAccountAssets},
{GET_ACC_AST_TX, &InteractiveQueryCli::parseGetAccountAssetTransactions},
{GET_ACC_AST_TX,
&InteractiveQueryCli::parseGetAccountAssetTransactions},
{GET_ACC_TX, &InteractiveQueryCli::parseGetAccountTransactions},
{GET_TX, &InteractiveQueryCli::parseGetTransactions},
{GET_ACC_SIGN, &InteractiveQueryCli::parseGetSignatories},
Expand All @@ -84,21 +85,19 @@ namespace iroha_cli {
// query_handlers_
};

menu_points_ = formMenu(
query_handlers_, query_params_descriptions_, description_map_);
menu_points_ =
formMenu(query_handlers_, query_params_map_, description_map_);
// Add "go back" option
addBackOption(menu_points_);
}

void InteractiveQueryCli::create_result_menu() {
result_handlers_ = {{SAVE_CODE, &InteractiveQueryCli::parseSaveFile},
{SEND_CODE, &InteractiveQueryCli::parseSendToIroha}};
result_params_descriptions_ =
getCommonParamsMap(default_peer_ip_, default_port_);
result_params_map_ = getCommonParamsMap(default_peer_ip_, default_port_);

result_points_ = formMenu(result_handlers_,
result_params_descriptions_,
getCommonDescriptionMap());
result_points_ = formMenu(
result_handlers_, result_params_map_, getCommonDescriptionMap());
addBackOption(result_points_);
}

Expand Down Expand Up @@ -159,7 +158,7 @@ namespace iroha_cli {
}

auto res = handleParse<std::shared_ptr<iroha::model::Query>>(
this, line, query_handlers_, query_params_descriptions_);
this, line, query_handlers_, query_params_map_);
if (not res) {
// Continue parsing
return true;
Expand Down Expand Up @@ -259,8 +258,8 @@ namespace iroha_cli {
return true;
}

auto res = handleParse<bool>(
this, line, result_handlers_, result_params_descriptions_);
auto res =
handleParse<bool>(this, line, result_handlers_, result_params_map_);

return res.get_value_or(true);
}
Expand Down
5 changes: 3 additions & 2 deletions iroha-cli/interactive/impl/interactive_status_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ namespace iroha_cli {
descriptionMap_ = {{GET_TX_INFO, "Get status of transaction"}};
const auto tx_id = "Requested tx hash";

requestParamsDescriptions_ = {{GET_TX_INFO, {tx_id}}};
requestParamsDescriptions_ = {
{GET_TX_INFO, makeParamsDescription({tx_id})}};
actionHandlers_ = {{GET_TX_INFO, &InteractiveStatusCli::parseGetHash}};

menuPoints_ = formMenu(
Expand All @@ -76,7 +77,7 @@ namespace iroha_cli {
printMenu("Choose action: ", menuPoints_);
while (isParsing) {
auto line = promptString("> ");
if (not line){
if (not line) {
// line has terminating symbol
isParsing = false;
break;
Expand Down
83 changes: 42 additions & 41 deletions iroha-cli/interactive/impl/interactive_transaction_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,40 @@ namespace iroha_cli {
const auto can_asset_creator = "Can create/add new assets";
const auto can_roles = "Can create/append roles";

command_params_descriptions_ = {
{ADD_ASSET_QTY, {acc_id, ast_id, amount_a, amount_b}},
{ADD_PEER, {peer_id, pub_key}},
{ADD_SIGN, {acc_id, pub_key}},
{CREATE_ACC, {acc_name, dom_id, pub_key}},
{CREATE_DOMAIN, {dom_id, std::string("Default ") + role}},
{CREATE_ASSET, {ast_name, dom_id, ast_precision}},
{REMOVE_SIGN, {acc_id, pub_key}},
{SET_QUO, {acc_id, quorum}},
{SUB_ASSET_QTY, {}},
command_params_map_ = {
{ADD_ASSET_QTY,
makeParamsDescription({acc_id, ast_id, amount_a, amount_b})},
{ADD_PEER, makeParamsDescription({peer_id, pub_key})},
{ADD_SIGN, makeParamsDescription({acc_id, pub_key})},
{CREATE_ACC, makeParamsDescription({acc_name, dom_id, pub_key})},
{CREATE_DOMAIN,
makeParamsDescription({dom_id, std::string("Default ") + role})},
{CREATE_ASSET,
makeParamsDescription({ast_name, dom_id, ast_precision})},
{REMOVE_SIGN, makeParamsDescription({acc_id, pub_key})},
{SET_QUO, makeParamsDescription({acc_id, quorum})},
{SUB_ASSET_QTY, makeParamsDescription({})},
{TRAN_ASSET,
{std::string("Src") + acc_id,
std::string("Dest") + acc_id,
ast_id,
amount_a,
amount_b}},
makeParamsDescription({std::string("Src") + acc_id,
std::string("Dest") + acc_id,
ast_id,
amount_a,
amount_b})},
{CREATE_ROLE,
{role,
can_read_self,
can_edit_self,
can_read_all,
can_transfer_receive,
can_asset_creator,
can_create_domain,
can_roles,
can_create_account}},
{APPEND_ROLE, {acc_id, role}},
{DETACH_ROLE, {acc_id, role}},
{GRANT_PERM, {acc_id, perm}},
{REVOKE_PERM, {acc_id, perm}},
{SET_ACC_KV, {acc_id, "key", "value"}}
makeParamsDescription({role,
can_read_self,
can_edit_self,
can_read_all,
can_transfer_receive,
can_asset_creator,
can_create_domain,
can_roles,
can_create_account})},
{APPEND_ROLE, makeParamsDescription({acc_id, role})},
{DETACH_ROLE, makeParamsDescription({acc_id, role})},
{GRANT_PERM, makeParamsDescription({acc_id, perm})},
{REVOKE_PERM, makeParamsDescription({acc_id, perm})},
{SET_ACC_KV, makeParamsDescription({acc_id, "key", "value"})}
// command parameters descriptions
};

Expand All @@ -141,9 +144,8 @@ namespace iroha_cli {
// Command parsers
};

commands_menu_ = formMenu(command_handlers_,
command_params_descriptions_,
commands_description_map_);
commands_menu_ = formMenu(
command_handlers_, command_params_map_, commands_description_map_);
// Add "go back" option
addBackOption(commands_menu_);
}
Expand All @@ -159,11 +161,10 @@ namespace iroha_cli {
result_desciption.insert(
{BACK_CODE, "Go back and start a new transaction"});

result_params_descriptions =
getCommonParamsMap(default_peer_ip_, default_port_);
result_params_map = getCommonParamsMap(default_peer_ip_, default_port_);

result_params_descriptions.insert({ADD_CMD, {}});
result_params_descriptions.insert({BACK_CODE, {}});
result_params_map.insert({ADD_CMD, {}});
result_params_map.insert({BACK_CODE, {}});

result_handlers_ = {
{SAVE_CODE, &InteractiveTransactionCli::parseSaveFile},
Expand All @@ -173,8 +174,8 @@ namespace iroha_cli {
// Parsers for result
};

result_menu_ = formMenu(
result_handlers_, result_params_descriptions, result_desciption);
result_menu_ =
formMenu(result_handlers_, result_params_map, result_desciption);
}

InteractiveTransactionCli::InteractiveTransactionCli(
Expand Down Expand Up @@ -227,7 +228,7 @@ namespace iroha_cli {
}

auto res = handleParse<std::shared_ptr<iroha::model::Command>>(
this, line, command_handlers_, command_params_descriptions_);
this, line, command_handlers_, command_params_map_);

if (not res) {
// Continue parsing
Expand Down Expand Up @@ -457,8 +458,8 @@ namespace iroha_cli {

bool InteractiveTransactionCli::parseResult(std::string line) {
// Find in result handler map
auto res = handleParse<bool>(
this, line, result_handlers_, result_params_descriptions);
auto res =
handleParse<bool>(this, line, result_handlers_, result_params_map);
return res.get_value_or(true);
}

Expand Down
Loading

0 comments on commit 04c3bea

Please sign in to comment.