cli: Add a command line flag to specify datastore

GCC doesn't like multiline raw string literals in macros, so I head to
get rid of the macro.

Change-Id: I1fe2c2f45f2b5700e682c229fdd182edc20e4d77
diff --git a/src/cli.cpp b/src/cli.cpp
index 7384f33..c142850 100644
--- a/src/cli.cpp
+++ b/src/cli.cpp
@@ -15,25 +15,21 @@
 #if defined(SYSREPO_CLI)
 #include "sysrepo_access.hpp"
 #define PROGRAM_NAME "sysrepo-cli"
-#define PROGRAM_DESCRIPTION R"(CLI interface to sysrepo \
-\
-Usage: \
-  sysrepo-cli \
-  sysrepo-cli (-h | --help) \
-  sysrepo-cli --version \
-)"
+static const auto usage = R"(CLI interface to sysrepo
+
+Usage:
+  sysrepo-cli [-d <datastore>]
+  sysrepo-cli (-h | --help)
+  sysrepo-cli --version
+
+Options:
+  -d <datastore>   can be "running" or "startup" [default: running])";
 #else
 #error "Unknown CLI backend"
 #endif
 
-
-
-
 const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history";
 
-static const char usage[] = PROGRAM_DESCRIPTION;
-
-
 int main(int argc, char* argv[])
 {
     auto args = docopt::docopt(usage,
@@ -41,10 +37,21 @@
                                true,
                                PROGRAM_NAME " " NETCONF_CLI_VERSION,
                                true);
-    std::cout << "Welcome to " PROGRAM_NAME << std::endl;
 
 #if defined(SYSREPO_CLI)
-    SysrepoAccess datastore(PROGRAM_NAME);
+    auto datastoreType = Datastore::Running;
+    if (const auto& ds = args["-d"]) {
+        if (ds.asString() == "startup") {
+            datastoreType = Datastore::Startup;
+        } else if (ds.asString() == "running") {
+            datastoreType = Datastore::Running;
+        } else {
+            std::cerr << PROGRAM_NAME <<  ": unknown datastore: " << ds.asString() << "\n";
+            return 1;
+        }
+    }
+    SysrepoAccess datastore(PROGRAM_NAME, datastoreType);
+    std::cout << "Connected to sysrepo [datastore: " << (datastoreType == Datastore::Startup ? "startup" : "running") << "]" << std::endl;
 #else
 #error "Unknown CLI backend"
 #endif
diff --git a/src/sysrepo_access.cpp b/src/sysrepo_access.cpp
index 2d1a1b4..7115ad7 100644
--- a/src/sysrepo_access.cpp
+++ b/src/sysrepo_access.cpp
@@ -130,12 +130,23 @@
 
 SysrepoAccess::~SysrepoAccess() = default;
 
-SysrepoAccess::SysrepoAccess(const std::string& appname)
+sr_datastore_t toSrDatastore(Datastore datastore)
+{
+    switch (datastore) {
+    case Datastore::Running:
+        return SR_DS_RUNNING;
+    case Datastore::Startup:
+        return SR_DS_STARTUP;
+    }
+    __builtin_unreachable();
+}
+
+SysrepoAccess::SysrepoAccess(const std::string& appname, const Datastore datastore)
     : m_connection(new sysrepo::Connection(appname.c_str()))
     , m_schema(new YangSchema())
 {
     try {
-        m_session = std::make_shared<sysrepo::Session>(m_connection);
+        m_session = std::make_shared<sysrepo::Session>(m_connection, toSrDatastore(datastore));
     } catch (sysrepo::sysrepo_exception& ex) {
         reportErrors();
     }
@@ -267,17 +278,6 @@
     return res;
 }
 
-sr_datastore_t toSrDatastore(Datastore datastore)
-{
-    switch (datastore) {
-    case Datastore::Running:
-        return SR_DS_RUNNING;
-    case Datastore::Startup:
-        return SR_DS_STARTUP;
-    }
-    __builtin_unreachable();
-}
-
 void SysrepoAccess::copyConfig(const Datastore source, const Datastore destination)
 {
     m_session->copy_config(nullptr, toSrDatastore(source), toSrDatastore(destination));
diff --git a/src/sysrepo_access.hpp b/src/sysrepo_access.hpp
index da5fb75..1d72525 100644
--- a/src/sysrepo_access.hpp
+++ b/src/sysrepo_access.hpp
@@ -27,7 +27,7 @@
 class SysrepoAccess : public DatastoreAccess {
 public:
     ~SysrepoAccess() override;
-    SysrepoAccess(const std::string& appname);
+    SysrepoAccess(const std::string& appname, const Datastore datastore);
     Tree getItems(const std::string& path) override;
     void setLeaf(const std::string& path, leaf_data_ value) override;
     void createPresenceContainer(const std::string& path) override;
diff --git a/tests/data_query.cpp b/tests/data_query.cpp
index e0e2875..28c51e8 100644
--- a/tests/data_query.cpp
+++ b/tests/data_query.cpp
@@ -45,7 +45,7 @@
     SysrepoSubscription subscriptionOther("other-module");
 
 #ifdef sysrepo_BACKEND
-    SysrepoAccess datastore("netconf-cli-test");
+    SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
 #elif defined(netconf_BACKEND)
     NetconfAccess datastore(NETOPEER_SOCKET_PATH);
 #else
diff --git a/tests/datastore_access.cpp b/tests/datastore_access.cpp
index a1f9322..d5f3e12 100644
--- a/tests/datastore_access.cpp
+++ b/tests/datastore_access.cpp
@@ -40,7 +40,7 @@
     SysrepoSubscription subscription("example-schema", &mock);
 
 #ifdef sysrepo_BACKEND
-    SysrepoAccess datastore("netconf-cli-test");
+    SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
 #elif defined(netconf_BACKEND)
     NetconfAccess datastore(NETOPEER_SOCKET_PATH);
 #else
@@ -406,7 +406,7 @@
     srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE);
 
 #ifdef sysrepo_BACKEND
-    SysrepoAccess datastore("netconf-cli-test");
+    SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
 #elif defined(netconf_BACKEND)
     NetconfAccess datastore(NETOPEER_SOCKET_PATH);
 #else