Skip to content

Commit 10d80b6

Browse files
ElestriasMarkuu-s
authored andcommitted
client methods cli updates
Signed-off-by: elestrias <[email protected]>
1 parent 124e74b commit 10d80b6

File tree

4 files changed

+259
-48
lines changed

4 files changed

+259
-48
lines changed

core/cli/cli.hpp

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,73 @@
1111
#include <memory>
1212
#include <typeindex>
1313

14-
#define CLI_TRY(valueName, maybeResult) \
14+
#define CLI_TRY(valueName, maybeResult) \
1515
auto valueName##OUTCOME_TRY = maybeResult; \
1616
if (valueName##OUTCOME_TRY.has_error()) throw std::invalid_argument(""); \
1717
auto valueName = valueName##OUTCOME_TRY.value();
1818

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); \
19+
#define CLI_TRY_TEXT(valueName, maybeResult, textError) \
20+
auto valueName##OUTCOME_TRY = maybeResult; \
21+
if (valueName##OUTCOME_TRY.has_error()) \
22+
throw std::invalid_argument(textError); \
2223
auto valueName = valueName##OUTCOME_TRY.value();
2324

25+
<<<<<<< HEAD
26+
=======
27+
#include "cli/try.hpp"
28+
29+
#define CLI_BOOL(NAME, DESCRIPTION) \
30+
struct { \
31+
bool _{}; \
32+
void operator()(Opts &opts) { \
33+
opts.add_options()(NAME, po::bool_switch(&_), DESCRIPTION); \
34+
} \
35+
operator bool() const { \
36+
return _; \
37+
} \
38+
}
39+
#define CLI_DEFAULT(NAME, DESCRIPTION, TYPE, INIT) \
40+
struct { \
41+
TYPE _ INIT; \
42+
void operator()(Opts &opts) { \
43+
opts.add_options()(NAME, po::value(&_), DESCRIPTION); \
44+
} \
45+
auto &operator*() const { \
46+
return _; \
47+
} \
48+
auto *operator->() const { \
49+
return &_; \
50+
} \
51+
}
52+
#define CLI_OPTIONAL(NAME, DESCRIPTION, TYPE) \
53+
struct { \
54+
boost::optional<TYPE> _; \
55+
void operator()(Opts &opts) { \
56+
opts.add_options()(NAME, po::value(&_), DESCRIPTION); \
57+
} \
58+
void operator=(TYPE &&rhs) { \
59+
_ = std::move(rhs); \
60+
} \
61+
operator bool() const { \
62+
return _.operator bool(); \
63+
} \
64+
auto &operator*() const { \
65+
if (!_) { \
66+
throw ::fc::cli::CliError{"--{} argument is required but missing", \
67+
NAME}; \
68+
} \
69+
return *_; \
70+
} \
71+
auto *operator->() const { \
72+
return &**this; \
73+
} \
74+
}
75+
76+
>>>>>>> 172a64dc (client methods cli updates)
2477
#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)
78+
#define CLI_RUN() \
79+
static ::fc::cli::RunResult run( \
80+
const ::fc::cli::ArgsMap &argm, Args &args, const ::fc::cli::Argv &argv)
2981
#define CLI_NO_RUN() constexpr static nullptr_t run{nullptr};
3082

3183
namespace fc::cli {
@@ -34,7 +86,7 @@ namespace fc::cli {
3486

3587
using RunResult = void;
3688
struct ArgsMap {
37-
std::map<std::type_index, std::shared_ptr<void>> _;
89+
mutable std::map<std::type_index, std::shared_ptr<void>> _;
3890
template <typename Args>
3991
void add(Args &&v) {
4092
_.emplace(typeid(Args), std::make_shared<Args>(std::forward<Args>(v)));

core/cli/example/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_executable(cli_example main.cpp)
7+
target_link_libraries(cli_example
8+
cli
9+
rpc
10+
car
11+
file
12+
memory_indexed_car
13+
unixfs
14+
)

core/cli/node/client.hpp

Lines changed: 145 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33

44
#pragma once
55
#include "cli/node/node.hpp"
6+
#include "storage/car/car.hpp"
7+
#include "storage/ipld/memory_indexed_car.hpp"
8+
#include "storage/unixfs/unixfs.hpp"
9+
#include "storage/ipld/memory_indexed_car.hpp"
610

711
namespace fc::cli::_node {
812
using api::FileRef;
913
using api::ImportRes;
1014
using api::RetrievalOrder;
11-
using primitives::BigInt;
1215
using primitives::address::Address;
13-
using primitives::address::decodeFromString;
16+
using proofs::padPiece;
17+
using ::fc::storage::car::makeCar;
18+
using ::fc::storage::unixfs::wrapFile;
19+
1420

1521
struct clientRetrieve {
1622
struct Args {
@@ -47,51 +53,48 @@ namespace fc::cli::_node {
4753

4854
CLI_RUN() {
4955
Node::Api api{argm};
50-
CLI_TRY_TEXT(root, CID::fromString(args.root), "Invalid root CID: " + args.root);
51-
CLI_TRY_TEXT(cid,
52-
CID::fromString(args.piece_cid),
53-
"Invalid piece CID: " + args.piece_cid)
54-
if (!args.provider.empty()) {
55-
CLI_TRY_TEXT(miner,
56-
decodeFromString(args.provider),
57-
"Invalid Provider Address: " + args.provider)
58-
}
59-
if (!args.from.empty()) {
60-
CLI_TRY_TEXT(client,
61-
decodeFromString(args.from),
62-
"Invalid Client Address: " + args.from)
56+
RetrievalOrder order{};
57+
auto data_cid{cliArgv<CID>(argv, 0, "dataCid")};
58+
auto path{cliArgv<boost::filesystem::path>(argv, 1, "path")};
59+
order.client = (args.from ? *args.from : cliTry(api._->WalletDefaultAddress(), "Getting address of default wallet..."));
60+
order.miner = (args.provider ? *args.provider : Address{});
61+
if (args.max_price) {
62+
fmt::print("max price is {}fil ({}attofil)\n",
63+
args.max_price->fil,
64+
args.max_price->atto());
6365
}
66+
fmt::print("retrieving {} to {} {}\n",
67+
data_cid,
68+
args.car ? "car" : "file",
69+
path);
70+
6471
// TODO: continue function
65-
// TODO: positional args
6672
}
6773
};
6874

6975
struct clientImportData {
7076
struct Args {
71-
bool car{};
72-
std::string path;
77+
CLI_BOOL("car", "import from a car file instead of a regular file")car;
7378

7479
CLI_OPTS() {
75-
Opts opts;
76-
auto option{opts.add_options()};
77-
option("car",
78-
po::bool_switch(&car),
79-
"import from a car file instead of a regular file");
80-
option("path,p", po::value(&path), "path to import");
81-
return opts;
80+
Opts opts;
81+
car(opts);
82+
return opts;
8283
}
8384
};
8485

8586
CLI_RUN() {
8687
Node::Api api{argm};
87-
FileRef file_ref{args.path, args.car};
88-
CLI_TRY_TEXT(result, api._->ClientImport(file_ref), "Fail of data import")
89-
std::cout << "File Root CID: " << result.root.toString().value() << "\n";
90-
std::cout << "Data Import Success\n";
88+
auto path{cliArgv<std::string>(argv, 0, "path to file to import")};
89+
FileRef file_ref{path, args.car};
90+
auto result = cliTry(api._->ClientImport(file_ref), "Processing data import");
91+
fmt::print("File Root CID: " + result.root.toString().value());
92+
fmt::print("Data Import Success");
9193
}
9294
};
9395

9496

97+
<<<<<<< HEAD
9598
struct clientGenerateCar{
9699
struct Args{
97100
std::string in_path;
@@ -102,42 +105,145 @@ namespace fc::cli::_node {
102105
option("input-path, inp", po::value(&in_path), "specifies path to source file");
103106
option("output-path, outp", po::value(&in_path), "specifies path to generated file");
104107
}
105-
106-
CLI_RUN(){
107-
std::cout<<"Does not supported yet\n";
108-
}
109-
};
108+
=======
109+
struct Node_client_generateCar: Empty{
110+
CLI_RUN() {
111+
auto path_to{cliArgv<boost::filesystem::path>(argv, 0, "input-path")};
112+
auto path_out{cliArgv<boost::filesystem::path>(argv, 1, "output-path")};
113+
>>>>>>> 172a64dc (client methods cli updates)
114+
115+
auto tmp_path = path_to.string() + ".unixfs-tmp.car";
116+
auto ipld = cliTry(MemoryIndexedCar::make(tmp_path, true), "Creting IPLD instance of {}", tmp_path);
117+
std::ifstream file{path_to.string()};
118+
auto root = cliTry(wrapFile(*ipld, file));
119+
cliTry(::fc::storage::car::makeSelectiveCar(
120+
*ipld, {{root, {}}}, path_out.string()));
121+
boost::filesystem::remove(tmp_path);
122+
padPiece(path_out);
123+
}
110124
};
111125

112126
struct clientLocal: Empty{
113127
CLI_RUN(){
114128
Node::Api api {argm};
115129
CLI_TRY_TEXT(result, api._->ClientListImports(), "Fail of getting imports list");
116130
for(auto it = result.begin(); it != result.end(); it++){
117-
std::cout<<"Root CID: "<<it->root.toString().value()<<"\n";
118-
std::cout<<"Source: "<<it->source<<"\n";
119-
std::cout<<"Path: "<<it->path<<"\n";
131+
fmt::print("Root CID: {} \n", it->root.toString().value());
132+
fmt::print("Source: {}\n", it->source);
133+
fmt::print("Path: {}\n", it->path);
120134
}
121135
}
122136
};
123137

138+
<<<<<<< HEAD
124139
struct clientFind{
125140
struct Args{
141+
=======
142+
struct Node_client_find{
143+
144+
};
145+
146+
struct Node_client_listRetrieval{
147+
struct Args {
148+
CLI_BOOL("verbose", "print verbose deal details")verbose;
149+
CLI_BOOL("show-failed", "show failed/failing deals")failed_show;
150+
CLI_BOOL("completed", "show completed retrievals")completed_show;
151+
>>>>>>> 172a64dc (client methods cli updates)
126152

127153
CLI_OPTS(){
128154
Opts opts;
129-
auto option{opts.add_options()};
130-
155+
verbose(opts);
156+
failed_show(opts);
157+
completed_show(opts);
158+
return opts;
131159
}
132160
};
133161

162+
CLI_RUN(){
163+
Node::Api api{argm};
164+
bool failed_show = !args.failed_show;
165+
//TODO: continue;
166+
// chan = ()
167+
// if(flag){
168+
// chan->read([](){
169+
// print(api->stateRetrivalsDeals)
170+
//
171+
// })
172+
while(true){
173+
sleep(10000000000);
174+
}
175+
//
176+
}
177+
134178
};
135179

136180

137181

182+
<<<<<<< HEAD
138183

139184

140185

186+
=======
187+
struct Node_client_inspectDeal{
188+
struct Args {
189+
CLI_OPTIONAL("proposal-cid", "proposal cid of deal to be inspected", CID)
190+
proposal_cid;
191+
CLI_OPTIONAL("deal-id", "id of deal to be inspected", int) deal_id;
192+
CLI_OPTS() {
193+
Opts opts;
194+
proposal_cid(opts);
195+
deal_id(opts);
196+
return opts;
197+
}
198+
};
199+
CLI_RUN(){
200+
Node::Api api{argm};
201+
202+
203+
204+
}
205+
206+
};
207+
struct Node_client_list_deals{
208+
struct Args {
209+
CLI_BOOL("show-failed", "show failed/failing deals") failed_show;
210+
CLI_OPTS(){
211+
Opts opts;
212+
failed_show(opts);
213+
return opts;
214+
}
215+
};
216+
217+
CLI_RUN(){
218+
Node::Api api{argm};
219+
fmt::print("Not supported yet\n");
220+
auto local_deals = cliTry(api._->ClientListDeals(), "Getting local client deals...");
221+
// TODO(Markuus): make output;
222+
}
223+
};
224+
225+
struct Node_client_balances{
226+
struct Args{
227+
CLI_OPTIONAL("client", "specifies market client", Address)client;
228+
CLI_OPTS(){
229+
Opts opts;
230+
client(opts);
231+
return opts;
232+
}
233+
};
234+
235+
CLI_RUN(){
236+
Node::Api api{argm};
237+
Address addr = (args.client ? *args.client : cliTry(api._->WalletDefaultAddress(), "Getting address of default wallet..."));
238+
auto balance = cliTry(api._->StateMarketBalance(addr, TipsetKey()));
239+
240+
241+
fmt::print(" Escrowed Funds: {}\n", AttoFil{balance.escrow});
242+
fmt::print(" Locked Funds: {}\n", AttoFil{balance.locked});
243+
//TODO: Reserved and Avaliable
244+
}
245+
};
246+
>>>>>>> 172a64dc (client methods cli updates)
141247

142248

143249
} // namespace fc::cli::_node

0 commit comments

Comments
 (0)