X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Fcli%2Fgetcommand.cpp;fp=src%2Fcli%2Fgetcommand.cpp;h=2c34fd4bf127b618333853354f2bb050c7adc71d;hb=fefde502cd4b1d2517f9de30a009d2dea59a7cf1;hp=0000000000000000000000000000000000000000;hpb=63869e10ac4d8572238989e1b582c0314da91f9c;p=ric-plt%2Fsdl.git diff --git a/src/cli/getcommand.cpp b/src/cli/getcommand.cpp new file mode 100644 index 0000000..2c34fd4 --- /dev/null +++ b/src/cli/getcommand.cpp @@ -0,0 +1,75 @@ +#include +#include +#include "private/cli/commandmap.hpp" +#include "private/cli/common.hpp" +#include + +using namespace shareddatalayer; +using namespace shareddatalayer::cli; + +namespace +{ + std::shared_ptr createSyncStorage(const SyncStorage::Namespace& ns, + std::ostream& out) + { + try + { + auto sdl(shareddatalayer::SyncStorage::create()); + sdl->waitReady(ns, std::chrono::minutes(1)); + return sdl; + } + catch (const shareddatalayer::Exception& error) + { + out << "SyncStorage create failed: " << error.what() << std::endl; + } + + return nullptr; + } + + void get(shareddatalayer::SyncStorage& sdl, + const SyncStorage::Namespace& ns, + const SyncStorage::Key& key, + std::ostream& out) + { + try + { + auto data(sdl.get(ns, {key})); + out << data << std::endl; + } + catch (const shareddatalayer::Exception& error) + { + out << "get(" << ns << ", " << key << ") failed: " + << error.what() << std::endl; + } + } + + int getCommand(std::ostream& out, const boost::program_options::variables_map& map) + { + auto ns(map["ns"].as()); + auto key(map["key"].as()); + + auto sdl(createSyncStorage(ns, out)); + if (nullptr == sdl) + return EXIT_FAILURE; + sdl->setOperationTimeout(std::chrono::seconds(5)); + + get(std::ref(*sdl), ns, key, out); + + return EXIT_SUCCESS; + } +} + +const char *longHelpGetCmd = + "Use get SDL API to read data from storage under the namespace.\n\n" + "Example: sdltool get --ns 'sdltool' --key 'key1'"; + +AUTO_REGISTER_COMMAND(std::bind(getCommand, std::placeholders::_1, std::placeholders::_3), + "get", + "get data with SDL API under the namespace", + longHelpGetCmd, + CommandMap::Category::UTIL, + 30050, + ("ns,n", boost::program_options::value()->default_value("sdltoolns"), "namespace to use") + ("key,k", boost::program_options::value()->default_value("key1"), "key value") + ); +