Skip to content

Commit 5daed67

Browse files
ElestriasMarkuu-s
authored andcommitted
retireve args parsing
Signed-off-by: elestrias <[email protected]>
1 parent 84e8bbc commit 5daed67

File tree

3 files changed

+108
-23
lines changed

3 files changed

+108
-23
lines changed

core/cli/cli.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#pragma once
7+
8+
#include <boost/optional.hpp>
9+
#include <boost/program_options/options_description.hpp>
10+
#include <map>
11+
#include <memory>
12+
#include <typeindex>
13+
14+
#define CLI_TRY(valueName, maybeResult) \
15+
auto valueName##OUTCOME_TRY = maybeResult; \
16+
if (valueName##OUTCOME_TRY.has_error()) throw std::invalid_argument(""); \
17+
auto valueName = valueName##OUTCOME_TRY.value();
18+
19+
#define CLI_TRY_TEXT(valueName, maybeResult, textError) \
20+
auto valueName##OUTCOME_TRY = maybeResult; \
21+
if (valueName##OUTCOME_TRY.has_error()) throw std::invalid_argument(textError); \
22+
auto valueName = valueName##OUTCOME_TRY.value();
23+
24+
#define CLI_OPTS() ::fc::cli::Opts opts()
25+
#define CLI_RUN() \
26+
static ::fc::cli::RunResult run(const ::fc::cli::ArgsMap &argm, \
27+
const Args &args, \
28+
const ::fc::cli::Argv &argv)
29+
#define CLI_NO_RUN() constexpr static nullptr_t run{nullptr};
30+
31+
namespace fc::cli {
32+
namespace po = boost::program_options;
33+
using Opts = po::options_description;
34+
35+
using RunResult = void;
36+
struct ArgsMap {
37+
std::map<std::type_index, std::shared_ptr<void>> _;
38+
template <typename Args>
39+
void add(Args &&v) {
40+
_.emplace(typeid(Args), std::make_shared<Args>(std::forward<Args>(v)));
41+
}
42+
template <typename Cmd>
43+
const typename Cmd::Args &of() const {
44+
return *reinterpret_cast<const typename Cmd::Args *>(
45+
_.at(typeid(typename Cmd::Args)).get());
46+
}
47+
};
48+
// note: Args is defined inside command
49+
using Argv = std::vector<std::string>;
50+
51+
struct Empty {
52+
struct Args {
53+
CLI_OPTS() {
54+
return {};
55+
}
56+
};
57+
CLI_NO_RUN();
58+
};
59+
using Group = Empty;
60+
61+
struct ShowHelp {};
62+
} // namespace fc::cli

core/cli/node/_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace fc::cli::_node {
1717
})},
1818
{"client",
1919
tree<Group>({
20-
{"retrieve", tree<clientRetrive>()},
20+
{"retrieve", tree<clientRetrieve>()},
2121
})},
2222
})};
2323
} // namespace fc::cli::_node

core/cli/node/client.hpp

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,56 @@
44
#pragma once
55
#include "cli/node/node.hpp"
66

7-
namespace fc::cli::_node{
8-
9-
10-
struct clientRetrive{
11-
struct Args{
12-
bool car{};
13-
bool export_merkle_root{};
14-
std::string data_selector;
15-
16-
CLI_OPTS(){
17-
Opts opts;
7+
namespace fc::cli::_node {
8+
using api::RetrievalOrder;
9+
using primitives::BigInt;
10+
using primitives::address::Address;
11+
using primitives::address::decodeFromString;
12+
13+
struct clientRetrieve {
14+
struct Args {
15+
std::string from;
16+
std::string piece_cid;
17+
BigInt max_funds;
18+
boost::filesystem::path path;
19+
std::string provider;
20+
21+
CLI_OPTS() {
22+
Opts opts;
1823
auto option{opts.add_options()};
19-
option("car", po::bool_switch(&car), "Export to a car file instead of a regular file");
20-
option("data-selector", po::value(&data_selector), "IPLD datamodel text-path selector, or IPLD json selector");
21-
option("car-export-merkle-proof", po::bool_switch(&export_merkle_root), "(requires --data-selector and --car) Export data-selector merkle proof");
24+
option("from", po::value(&from), "transaction from");
25+
option("pieceCID", po::value(&piece_cid), "cid of piece to retrieve");
26+
option(
27+
"maxPrice",
28+
po::value(&max_funds),
29+
"specifies the maximum token amount that client ready to spend");
30+
option("path",
31+
po::value(&path)->required(),
32+
"specifies the path to retrieve");
33+
option("provider",
34+
po::value(&provider)->required(),
35+
"specifies retrieval provider to perform a deal");
2236
return opts;
2337
}
2438
};
2539

26-
27-
CLI_RUN(){
28-
std::cout<<args.data_selector<<"\n";
40+
CLI_RUN() {
41+
Node::Api api{argm};
42+
CLI_TRY_TEXT(cid,
43+
CID::fromString(args.piece_cid),
44+
"Invalid piece CID: " + args.piece_cid)
45+
if (!args.provider.empty()) {
46+
CLI_TRY_TEXT(miner,
47+
decodeFromString(args.provider),
48+
"Invalid Provider Address: " + args.provider)
49+
}
50+
if (!args.from.empty()) {
51+
CLI_TRY_TEXT(client,
52+
decodeFromString(args.from),
53+
"Invalid Client Address: " + args.from)
54+
}
55+
// TODO: continue function
2956
}
3057
};
3158

32-
33-
34-
35-
36-
} //fc::cli::node
59+
} // namespace fc::cli::_node

0 commit comments

Comments
 (0)