-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgz.cc
937 lines (809 loc) · 25.1 KB
/
gz.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <curl/curl.h>
#include <string.h>
#include <tinyxml2.h>
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4251) // foo needs to have dll-interface
#endif
#include <google/protobuf/text_format.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#include <csignal>
#include <exception>
#include <gz/msgs/fuel_metadata.pb.h>
#include <gz/msgs/Utility.hh>
#include <algorithm>
#include <chrono>
#include <deque>
#include <future>
#include <iostream>
#include <map>
#include <optional>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include <gz/common/Console.hh>
#include <gz/common/Filesystem.hh>
#include <gz/common/SignalHandler.hh>
#include <gz/common/URI.hh>
#include "gz/fuel_tools/ClientConfig.hh"
#include "gz/fuel_tools/CollectionIdentifier.hh"
#include "gz/fuel_tools/config.hh"
#include "gz/fuel_tools/FuelClient.hh"
#include "gz/fuel_tools/Helpers.hh"
#include "gz/fuel_tools/Result.hh"
#include "gz.hh"
#include "gz/fuel_tools/WorldIdentifier.hh"
//////////////////////////////////////////////////
/// \brief Print resources in a human readable manner
/// \param[in] _serverConfig Server configuration
/// \param[in] _resourceMap Key is owner name, value is vector of resources
/// \param[in] _resourceType Type, such as "models"
extern "C" void prettyPrint(
const gz::fuel_tools::ServerConfig &_serverConfig,
const std::map<std::string, std::vector<std::string>> &_resourceMap,
const std::string &_resourceType)
{
unsigned int ownerCount{0};
unsigned int resourceCount{0};
std::cout << "\033[92m\033[1m" << _serverConfig.Url().Str()
<< "\033[39m\033[0m" << std::endl;
for (auto owner = _resourceMap.begin(); owner != _resourceMap.end(); ++owner)
{
ownerCount++;
// Owner indentation
bool lastOwner = owner == std::prev(_resourceMap.end());
if (lastOwner)
std::cout << "└──";
else
std::cout << "├──";
// Owner name
std::cout << " \033[93m\033[1m" << owner->first << "\033[39m\033[0m"
<< std::endl;
// Resources
for (auto resource = owner->second.begin(); resource != owner->second.end();
++resource)
{
resourceCount++;
// Resource indentation
if (lastOwner)
std::cout << " ";
else
std::cout << "│ ";
if (resource == std::prev(owner->second.end()))
std::cout << "└── ";
else
std::cout << "├── ";
// Resource name
std::cout << *resource << std::endl;
}
}
std::cout << "\033[36m" << ownerCount << " owners, " << resourceCount
<< " " << _resourceType << "\033[39m" << std::endl;
}
//////////////////////////////////////////////////
/// \brief Print resources in a machine readable manner
/// \param[in] _serverConfig Server configuration
/// \param[in] _resourceMap Key is owner name, value is vector of resources
/// \param[in] _resourceType Type, such as "models"
extern "C" void uglyPrint(
const gz::fuel_tools::ServerConfig &_serverConfig,
const std::map<std::string, std::vector<std::string>> &_resourceMap,
const std::string &_resourceType)
{
CURL *curl = curl_easy_init();
for (auto owner = _resourceMap.begin(); owner != _resourceMap.end(); ++owner)
{
for (auto resource : owner->second)
{
auto encodedRes =
curl_easy_escape(curl, resource.c_str(), resource.size());
std::cout << _serverConfig.Url().Str() << "/" << _serverConfig.Version()
<< "/" << owner->first << "/" << _resourceType << "/"
<< std::string(encodedRes) << std::endl;
}
}
}
//////////////////////////////////////////////////
/// \brief Fill a map with all models from a server
/// \param[in] _client Fuel client
/// \param[in] _server Server configuration
/// \param[out] _resourceMap Key is owner name, value is vector of resources
/// \return True if successful, will fail if there's a server error or if the
/// server has no models yet.
extern "C" bool getAllModels(
const gz::fuel_tools::FuelClient &_client,
const gz::fuel_tools::ServerConfig &_server,
std::map<std::string, std::vector<std::string>> &_resourceMap)
{
auto iter = _client.Models(_server);
if (!iter)
{
std::cout <<
"Either failed to fetch model list, or server has no models yet."
<< std::endl;
return false;
}
// Rearrange by user
// key: user name
// value: vector of model names
for (; iter; ++iter)
{
_resourceMap[iter->Identification().Owner()].push_back(
iter->Identification().Name());
}
return true;
}
//////////////////////////////////////////////////
/// \brief Fill a map with all worlds from a server
/// \param[in] _client Fuel client
/// \param[in] _server Server configuration
/// \param[out] _resourceMap Key is owner name, value is vector of resources
/// \return True if successful, will fail if there's a server error or if the
/// server has no worlds yet.
extern "C" bool getAllWorlds(
const gz::fuel_tools::FuelClient &_client,
const gz::fuel_tools::ServerConfig &_server,
std::map<std::string, std::vector<std::string>> &_resourceMap)
{
auto iter = _client.Worlds(_server);
if (!iter)
{
std::cout <<
"Either failed to fetch world list, or server has no worlds yet."
<< std::endl;
return false;
}
// Rearrange by user
// key: user name
// value: vector of world names
for (; iter; ++iter)
{
_resourceMap[iter->Owner()].push_back(iter->Name());
}
return true;
}
//////////////////////////////////////////////////
/// \brief Fill a map with all models from an owner
/// \param[in] _client Fuel client
/// \param[in] _modelId Identifier for models to be returned
/// \param[out] _resourceMap Key is owner name, value is vector of resources
/// \return True if successful, will fail if there's a server error or if the
/// server has no models yet.
extern "C" bool getOwnerModels(
const gz::fuel_tools::FuelClient &_client,
const gz::fuel_tools::ModelIdentifier &_modelId,
std::map<std::string, std::vector<std::string>> &_resourceMap)
{
gz::fuel_tools::ModelIter iter = _client.Models(_modelId);
if (!iter)
{
std::cout <<
"Either failed to fetch model list, or server has no models yet."
<< std::endl;
return false;
}
// Rearrange by user
// key: user name
// value: vector of model names
for (; iter; ++iter)
{
_resourceMap[iter->Identification().Owner()].push_back(
iter->Identification().Name());
}
return true;
}
//////////////////////////////////////////////////
/// \brief Fill a map with all worlds from an owner
/// \param[in] _client Fuel client
/// \param[in] _worldId Identifier for worlds to be returned
/// \param[out] _resourceMap Key is owner name, value is vector of resources
/// \return True if successful, will fail if there's a server error or if the
/// server has no worlds yet.
extern "C" bool getOwnerWorlds(
const gz::fuel_tools::FuelClient &_client,
const gz::fuel_tools::WorldIdentifier &_worldId,
std::map<std::string, std::vector<std::string>> &_resourceMap)
{
auto iter = _client.Worlds(_worldId);
if (!iter)
{
std::cout <<
"Either failed to fetch world list, or server has no worlds yet."
<< std::endl;
return false;
}
// Rearrange by user
// key: user name
// value: vector of world names
for (; iter; ++iter)
{
_resourceMap[iter->Owner()].push_back(iter->Name());
}
return true;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE char *gzVersion()
{
return strdup(GZ_FUEL_TOOLS_VERSION_FULL);
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int listModels(const char *_url,
const char *_owner, const char *_raw, const char *_configFile)
{
std::string urlStr{_url};
if (!urlStr.empty() && !gz::common::URI::Valid(_url))
{
std::cout << "Invalid URL [" << _url << "]" << std::endl;
return 0;
}
gz::common::URI url(urlStr);
std::string owner{_owner};
std::string rawStr{_raw};
std::transform(rawStr.begin(), rawStr.end(),
rawStr.begin(), ::tolower);
bool pretty = rawStr != "true";
// Client
gz::fuel_tools::ClientConfig conf;
if (_configFile && strlen(_configFile) > 0)
{
conf.Clear();
conf.LoadConfig(_configFile);
}
if (url.Valid())
{
conf.Clear();
gz::fuel_tools::ServerConfig serverConf;
serverConf.SetUrl(gz::common::URI(url));
conf.AddServer(serverConf);
}
conf.SetUserAgent("FuelTools " GZ_FUEL_TOOLS_VERSION_FULL);
// Filter
gz::fuel_tools::ModelIdentifier modelId;
if (!owner.empty())
modelId.SetOwner(owner);
gz::fuel_tools::FuelClient client(conf);
// Get models
for (auto server : conf.Servers())
{
modelId.SetServer(server);
if (pretty)
{
std::cout << "Fetching model list from " << server.Url().Str() << "..."
<< std::endl;
}
auto startTime = std::chrono::high_resolution_clock::now();
std::map<std::string, std::vector<std::string>> modelsMap;
// All models
if (owner.empty())
{
if (!getAllModels(client, server, modelsMap))
continue;
}
else
{
if (!getOwnerModels(client, modelId, modelsMap))
continue;
}
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
endTime - startTime);
if (pretty)
{
std::cout << "Received model list (took " << duration.count() << "ms)."
<< std::endl;
}
// Print all models
if (pretty)
prettyPrint(server, modelsMap, "models");
else
uglyPrint(server, modelsMap, "models");
}
return true;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int listWorlds(const char *_url,
const char *_owner, const char *_raw, const char *_configFile)
{
std::string urlStr{_url};
if (!urlStr.empty() && !gz::common::URI::Valid(_url))
{
std::cout << "Invalid URL [" << _url << "]" << std::endl;
return 0;
}
gz::common::URI url(urlStr);
std::string owner{_owner};
std::string rawStr{_raw};
std::transform(rawStr.begin(), rawStr.end(),
rawStr.begin(), ::tolower);
bool pretty = rawStr != "true";
// Client
gz::fuel_tools::ClientConfig conf;
if (_configFile && strlen(_configFile) > 0)
{
conf.Clear();
conf.LoadConfig(_configFile);
}
if (url.Valid())
{
conf.Clear();
gz::fuel_tools::ServerConfig serverConf;
serverConf.SetUrl(url);
conf.AddServer(serverConf);
}
conf.SetUserAgent("FuelTools " GZ_FUEL_TOOLS_VERSION_FULL);
// Filter
gz::fuel_tools::WorldIdentifier worldId;
if (!owner.empty())
worldId.SetOwner(owner);
gz::fuel_tools::FuelClient client(conf);
// Get worlds
for (auto server : conf.Servers())
{
worldId.SetServer(server);
if (pretty)
{
std::cout << "Fetching world list from " << server.Url().Str() << "..."
<< std::endl;
}
auto startTime = std::chrono::high_resolution_clock::now();
std::map<std::string, std::vector<std::string>> worldsMap;
// All worlds
if (owner.empty())
{
if (!getAllWorlds(client, server, worldsMap))
continue;
}
else
{
if (!getOwnerWorlds(client, worldId, worldsMap))
continue;
}
auto endTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
endTime - startTime);
if (pretty)
{
std::cout << "Received world list (took " << duration.count() << "ms)."
<< std::endl;
}
// Print all worlds
if (pretty)
prettyPrint(server, worldsMap, "worlds");
else
uglyPrint(server, worldsMap, "worlds");
}
return true;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int downloadUrl(const char *_url,
const char *_configFile, const char *_header, const char *_type, int _jobs)
{
// Add signal handler for SIGTERM and SIGINT. Ctrl-C doesn't work without this
// handler.
gz::common::SignalHandler sigHandler;
sigHandler.AddCallback([&](int _sig) {
if (SIGTERM == _sig || SIGINT == _sig)
{
std::exit(1);
}
});
std::string urlStr{_url};
gz::common::URI url(urlStr);
if (!url.Valid())
{
std::cout << "Download failed: Malformed URL" << std::endl;
return false;
}
// Client
gz::fuel_tools::ClientConfig conf;
if (_configFile && strlen(_configFile) > 0)
{
conf.Clear();
conf.LoadConfig(_configFile);
}
conf.SetUserAgent("FuelTools " GZ_FUEL_TOOLS_VERSION_FULL);
gz::fuel_tools::FuelClient client(conf);
gz::fuel_tools::ModelIdentifier model;
gz::fuel_tools::WorldIdentifier world;
gz::fuel_tools::CollectionIdentifier collection;
// Model?
if (client.ParseModelUrl(url, model))
{
// Download
if (gz::common::Console::Verbosity() >= 3)
{
std::cout << "Downloading model: " << "\033[36m" << std::endl
<< model.AsPrettyString(" ") << "\033[39m" << std::endl;
}
if (model.Version() != 0)
{
gzwarn << "Requested version [" << model.VersionStr() << "], but "
<< "currently only the model's latest (tip) version is supported."
<< std::endl;
}
int result = 0;
if (_header && strlen(_header) > 0)
{
std::vector<std::string> headers;
headers.push_back(_header);
result = client.DownloadModel(model, headers);
}
else
{
result = client.DownloadModel(model);
}
if (!result)
{
std::cout << "Download failed." << std::endl;
return false;
}
}
// World?
else if (client.ParseWorldUrl(url, world))
{
// Download
if (gz::common::Console::Verbosity() >= 3)
{
std::cout << "Downloading world: " << "\033[36m" << std::endl
<< world.AsPrettyString(" ") << "\033[39m" << std::endl;
}
if (world.Version() != 0)
{
gzwarn << "Requested version [" << world.VersionStr() << "], but "
<< "currently only the world's latest (tip) version is supported."
<< std::endl;
}
gz::fuel_tools::Result result = client.DownloadWorld(world);
if (!result)
{
std::cout << "Download failed because " << result.ReadableResult()
<< std::endl;
return false;
}
}
// Collection?
else if (client.ParseCollectionUrl(url, collection))
{
if (gz::common::Console::Verbosity() >= 3)
{
std::cout << "Downloading collection: " << "\033[36m" << std::endl
<< collection.AsPrettyString(" ") << "\033[39m" << std::endl;
}
bool downloadModels = true;
bool downloadWorlds = true;
if (nullptr !=_type)
{
if (strcmp(_type, "model") == 0)
{
downloadWorlds = false;
}
else if (strcmp(_type, "world") == 0)
{
downloadModels = false;
}
else
{
gzerr << "Unknown resource type [" << _type << "] sepcified.\n";
return false;
}
}
std::vector<gz::fuel_tools::ModelIdentifier> modelIds;
std::vector<gz::fuel_tools::WorldIdentifier> worldIds;
if (downloadModels)
{
// Get list of model identifiers in collection
auto modelsIter = client.Models(collection);
for (; modelsIter; ++modelsIter)
{
modelIds.push_back(modelsIter->Identification());
}
gzmsg << "Found " << modelIds.size() << " models in collection ["
<< collection.Name() << "]" << std::endl;
}
if (downloadWorlds)
{
// Get list of world identifiers in collection
auto worldIter = client.Worlds(collection);
for (; worldIter; ++worldIter)
{
worldIds.push_back(worldIter);
}
gzmsg << "Found " << worldIds.size() << " worlds in collection ["
<< collection.Name() << "]" << std::endl;
}
const std::size_t totalItemCount = modelIds.size() + worldIds.size();
if (totalItemCount == 0)
{
std::cout << "There are no items in collection [" << collection.Name()
<< "]" << std::endl;
return false;
}
if (downloadModels)
{
auto result = client.DownloadModels(modelIds, _jobs);
}
if (downloadWorlds)
{
auto result = client.DownloadWorlds(worldIds, _jobs);
}
}
else
{
std::cout << "Invalid URL: only models and worlds or collections can be "
<< "downloaded so far." << std::endl;
return false;
}
if (gz::common::Console::Verbosity() >= 3)
{
std::cout << "Download succeeded." << std::endl;
}
return true;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE void cmdVerbosity(const char *_verbosity)
{
gz::common::Console::SetVerbosity(std::atoi(_verbosity));
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int upload(const char *_path,
const char *_url, const char *_header, const char *_private,
const char *_owner)
{
gz::common::SignalHandler handler;
bool sigKilled{false};
handler.AddCallback([&sigKilled](const int)
{
sigKilled = true;
});
gz::fuel_tools::ClientConfig conf;
conf.SetUserAgent("FuelTools " GZ_FUEL_TOOLS_VERSION_FULL);
gz::fuel_tools::FuelClient client(conf);
gz::fuel_tools::ModelIdentifier model;
// Set the server URL, if present.
if (_url && std::strlen(_url) != 0)
model.Server().SetUrl(gz::common::URI(_url));
// Store header information
std::vector<std::string> headers;
if (_header && strlen(_header) > 0)
headers.push_back(_header);
// Determine if the resource should be private.
bool privateBool = false;
if (_private && std::strlen(_private) != 0)
{
std::string privateStr = gz::common::lowercase(_private);
privateBool = privateStr == "1" || privateStr == "true";
}
if (!gz::common::exists(_path))
{
gzerr << "The model path[" << _path << "] doesn't exist.\n";
return 0;
}
// Get the set of licenses from the server. This license information will
// be used during the upload process.
client.PopulateLicenses(model.Server());
if (gz::common::exists(
gz::common::joinPaths(_path, "metadata.pbtxt")) ||
gz::common::exists(
gz::common::joinPaths(_path, "model.config")))
{
std::cout << "Uploading a model[" << _path << "]\n";
// Upload the model
return client.UploadModel(_path, model, headers, privateBool, _owner);
}
// If a model.config or metadata.pbtxt file does not exist, then assume
// that the given path is a directory containing multiple models.
gz::common::DirIter dirIter(_path);
gz::common::DirIter end;
while (!sigKilled && dirIter != end)
{
if (gz::common::isDirectory(*dirIter) &&
(gz::common::exists(
gz::common::joinPaths(*dirIter, "metadata.pbtxt")) ||
gz::common::exists(
gz::common::joinPaths(*dirIter, "model.config"))))
{
if (!client.UploadModel(*dirIter, model, headers, privateBool, _owner))
{
gzerr << "Failed to upload model[" << *dirIter << "]\n";
}
}
++dirIter;
}
return 1;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int deleteUrl(
const char *_url, const char *_header)
{
gz::fuel_tools::ClientConfig conf;
conf.SetUserAgent("FuelTools " GZ_FUEL_TOOLS_VERSION_FULL);
gz::fuel_tools::FuelClient client(conf);
// Store header information
std::vector<std::string> headers;
if (_header && strlen(_header) > 0)
headers.push_back(_header);
gz::common::URI url(_url);
if (gz::common::Console::Verbosity() >= 3)
{
gz::fuel_tools::ModelIdentifier model;
gz::fuel_tools::WorldIdentifier world;
if (client.ParseModelUrl(url, model))
{
std::cout << "Deleting model: " << "\033[36m" << std::endl
<< model.AsPrettyString(" ") << "\033[39m" << std::endl;
}
else if (client.ParseWorldUrl(url, world))
{
std::cout << "Deleting world: " << "\033[36m" << std::endl
<< world.AsPrettyString(" ") << "\033[39m" << std::endl;
}
else
{
std::cout << "Invalid URL: only models and worlds can be delete so far."
<< std::endl;
}
}
client.DeleteUrl(url, headers);
return 1;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int config2Pbtxt(const char *_path)
{
gz::msgs::FuelMetadata meta;
std::ifstream inputFile(_path);
std::string inputStr((std::istreambuf_iterator<char>(inputFile)),
std::istreambuf_iterator<char>());
if (!gz::msgs::ConvertFuelMetadata(inputStr, meta))
{
gzerr << "Unable to convert model config[" << _path << "].\n";
return 0;
}
// Output the result.
std::cout << meta.DebugString() << std::endl;
return 1;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int pbtxt2Config(const char *_path)
{
gz::msgs::FuelMetadata meta;
// Read the pbtxt file.
std::ifstream inputFile(_path);
std::string inputStr((std::istreambuf_iterator<char>(inputFile)),
std::istreambuf_iterator<char>());
// Parse the file into the fuell metadata message
google::protobuf::TextFormat::ParseFromString(inputStr, &meta);
std::string modelConfig;
if (!gz::msgs::ConvertFuelMetadata(meta, modelConfig))
{
std::cerr << "Unable to convert Fuel metadata to model.config\n";
return 0;
}
std::cout << modelConfig << std::endl;
return 1;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int editUrl(
const char *_url, const char *_header, const char *_private,
const char *_path)
{
gz::fuel_tools::ClientConfig conf;
conf.SetUserAgent("FuelTools " GZ_FUEL_TOOLS_VERSION_FULL);
gz::fuel_tools::FuelClient client(conf);
// Store header information
std::vector<std::string> headers;
if (_header && strlen(_header) > 0)
headers.push_back(_header);
gz::common::URI url(_url);
// Get a privacy change.
std::optional<bool> privateBool;
if (_private && std::strlen(_private) != 0)
{
std::string privateStr = gz::common::lowercase(_private);
privateBool = privateStr == "1" || privateStr == "true";
}
gz::fuel_tools::ModelIdentifier model;
std::string modelPath;
if (_path && std::strlen(_path) != 0)
{
if (!gz::common::exists(_path))
{
gzerr << "The model path[" << _path << "] doesn't exist.\n";
return 0;
}
modelPath = _path;
}
// Check to see if a model has been specified in the the URI.
if (client.ParseModelUrl(url, model))
{
if (gz::common::Console::Verbosity() >= 3)
{
std::cout << "Editing model: " << "\033[36m" << std::endl
<< model.AsPrettyString(" ") << "\033[39m" << std::endl;
}
// Get the model details from the server
gz::fuel_tools::ModelIdentifier details;
if (!client.ModelDetails(model, details, headers))
{
gzerr << "Failed to fetch model details for model["
<< model.Name() << "]\n";
return 0;
}
// Change the privacy setting, if a change is present.
if (privateBool.has_value() || !modelPath.empty())
{
details.SetPrivate(*privateBool);
if (!client.PatchModel(details, headers, modelPath))
{
gzerr << "Failed to patch model[" << model.Name() << "].\n";
return 0;
}
}
}
else
{
std::cout << "Invalid URL: only models can be edited right now."
<< std::endl;
return 0;
}
return 1;
}
//////////////////////////////////////////////////
extern "C" GZ_FUEL_TOOLS_VISIBLE int update(
const char *_onlyModels, const char *_onlyWorlds, const char *_header)
{
// Add signal handler for SIGTERM and SIGINT. Ctrl-C doesn't work without this
// handler.
gz::common::SignalHandler sigHandler;
sigHandler.AddCallback([&](int _sig) {
if (SIGTERM == _sig || SIGINT == _sig)
{
std::exit(1);
}
});
bool onlyModelsBool = false;
if (_onlyModels && std::strlen(_onlyModels) != 0)
{
std::string str = gz::common::lowercase(_onlyModels);
onlyModelsBool = str == "1" || str == "true";
}
bool onlyWorldsBool = false;
if (_onlyWorlds && std::strlen(_onlyWorlds) != 0)
{
std::string str = gz::common::lowercase(_onlyWorlds);
onlyWorldsBool = str == "1" || str == "true";
}
// Client
gz::fuel_tools::ClientConfig conf;
conf.SetUserAgent("FuelTools " GZ_FUEL_TOOLS_VERSION_FULL);
gz::fuel_tools::FuelClient client(conf);
// Headers
std::vector<std::string> headers;
if (_header && strlen(_header) > 0)
headers.push_back(_header);
if (!onlyWorldsBool && !client.UpdateModels(headers)) {
return 0;
}
if (!onlyModelsBool && !client.UpdateWorlds(headers)) {
return 0;
}
return 1;
}