Petri Ovaska | fefde50 | 2021-09-17 12:33:24 +0300 | [diff] [blame] | 1 | #include <ostream> |
| 2 | #include <cstdlib> |
| 3 | #include "private/cli/commandmap.hpp" |
| 4 | #include "private/cli/common.hpp" |
| 5 | #include <sdl/syncstorage.hpp> |
| 6 | |
| 7 | using namespace shareddatalayer; |
| 8 | using namespace shareddatalayer::cli; |
| 9 | |
| 10 | namespace |
| 11 | { |
| 12 | std::shared_ptr<shareddatalayer::SyncStorage> createSyncStorage(const SyncStorage::Namespace& ns, |
| 13 | std::ostream& out) |
| 14 | { |
| 15 | try |
| 16 | { |
| 17 | auto sdl(shareddatalayer::SyncStorage::create()); |
| 18 | sdl->waitReady(ns, std::chrono::minutes(1)); |
| 19 | return sdl; |
| 20 | } |
| 21 | catch (const shareddatalayer::Exception& error) |
| 22 | { |
| 23 | out << "SyncStorage create failed: " << error.what() << std::endl; |
| 24 | } |
| 25 | |
| 26 | return nullptr; |
| 27 | } |
| 28 | |
| 29 | void set(shareddatalayer::SyncStorage& sdl, |
| 30 | const SyncStorage::Namespace& ns, |
| 31 | const SyncStorage::DataMap& dataMap, |
| 32 | std::ostream& out) |
| 33 | { |
| 34 | try |
| 35 | { |
| 36 | sdl.set(ns, dataMap); |
| 37 | out << "set {" << ns << "}," << dataMap << std::endl; |
| 38 | } |
| 39 | catch (const shareddatalayer::Exception& error) |
| 40 | { |
| 41 | out << "Error in set(" << ns << ", " << dataMap << ") failed: " |
| 42 | << error.what() << std::endl; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | int setCommand(std::ostream& out, const boost::program_options::variables_map& map) |
| 47 | { |
| 48 | auto ns(map["ns"].as<std::string>()); |
| 49 | auto key(map["key"].as<std::string>()); |
| 50 | auto data(map["data"].as<std::string>()); |
| 51 | auto debug(map["debug"].as<bool>()); |
| 52 | SyncStorage::Data dataVector(data.begin(), data.end()); |
| 53 | |
| 54 | auto sdl(createSyncStorage(ns, out)); |
| 55 | if (nullptr == sdl) |
| 56 | return EXIT_FAILURE; |
| 57 | sdl->setOperationTimeout(std::chrono::seconds(5)); |
| 58 | |
| 59 | if (debug) |
| 60 | { |
| 61 | out << "DEBUG setCommand ns: " << ns |
| 62 | << " key: " << key << " data: " << data << std::endl; |
| 63 | } |
| 64 | |
| 65 | SyncStorage::DataMap datamap({{key, dataVector}}); |
| 66 | set(std::ref(*sdl), ns, datamap, out); |
| 67 | |
| 68 | return EXIT_SUCCESS; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const char *longHelpSetCmd = |
| 73 | "Use set SDL API to write data to storage under the namespace.\n\n" |
| 74 | "Example: sdltool set --ns 'sdltool' --key 'key1' --data '1'"; |
| 75 | |
| 76 | AUTO_REGISTER_COMMAND(std::bind(setCommand, std::placeholders::_1, std::placeholders::_3), |
| 77 | "set", |
| 78 | "set key value with SDL API", |
| 79 | longHelpSetCmd, |
| 80 | CommandMap::Category::UTIL, |
| 81 | 30040, |
| 82 | ("ns,n", boost::program_options::value<std::string>()->default_value("sdltoolns"), "namespace to use") |
| 83 | ("key,k", boost::program_options::value<std::string>()->default_value("key1"), "key value") |
| 84 | ("data,d", boost::program_options::value<std::string>()->default_value("1"), "data") |
| 85 | ("debug", boost::program_options::bool_switch()->default_value(false), "Enable debug logs") |
| 86 | ); |
| 87 | |