Add the describe command

...which should, hopefully, show useful information about a particular
YANG node, such as the YANG-level description, the units, type info,
etc.

Change-Id: Id430ae58fe353124e5132fa5a69378e98932ebce
diff --git a/src/interpreter.cpp b/src/interpreter.cpp
index f112b52..5b13337 100644
--- a/src/interpreter.cpp
+++ b/src/interpreter.cpp
@@ -8,6 +8,7 @@
 
 #include <boost/mpl/for_each.hpp>
 #include <iostream>
+#include <sstream>
 #include "datastore_access.hpp"
 #include "interpreter.hpp"
 #include "utils.hpp"
@@ -79,6 +80,60 @@
         std::cout << it << std::endl;
 }
 
+std::string Interpreter::buildTypeInfo(const std::string& path) const
+{
+    std::ostringstream ss;
+    switch (m_datastore.schema()->nodeType(path)) {
+    case yang::NodeTypes::Container:
+        ss << "container";
+        break;
+    case yang::NodeTypes::PresenceContainer:
+        ss << "presence container";
+        break;
+    case yang::NodeTypes::Leaf:
+    {
+        auto leafType = m_datastore.schema()->leafType(path);
+        auto typedefName = m_datastore.schema()->leafTypeName(path);
+        std::string baseTypeStr;
+        if (leafType == yang::LeafDataTypes::LeafRef) {
+            ss << "-> ";
+            ss << m_datastore.schema()->leafrefPath(path) << " ";
+            baseTypeStr = leafDataTypeToString(m_datastore.schema()->leafrefBaseType(path));
+        } else {
+            baseTypeStr = leafDataTypeToString(leafType);
+        }
+
+        if (typedefName) {
+            ss << *typedefName << " (" << baseTypeStr << ")";
+        } else {
+            ss << baseTypeStr;
+        }
+
+        if (auto units = m_datastore.schema()->units(path)) {
+            ss << " [" + *units + "]";
+        }
+
+        if (m_datastore.schema()->leafIsKey(path)) {
+            ss << " (key)";
+        }
+        break;
+    }
+    case yang::NodeTypes::List:
+        ss << "list";
+        break;
+    }
+    return ss.str();
+}
+
+void Interpreter::operator()(const describe_& describe) const
+{
+    auto path = absolutePathFromCommand(describe);
+    std::cout << path << ": " << buildTypeInfo(path) << std::endl;
+    if (auto description = m_datastore.schema()->description(path)) {
+        std::cout << std::endl << *description << std::endl;
+    }
+}
+
 struct commandLongHelpVisitor : boost::static_visitor<const char*> {
     template <typename T>
     auto constexpr operator()(boost::type<T>) const
@@ -156,6 +211,15 @@
     }
 }
 
+std::string Interpreter::absolutePathFromCommand(const describe_& describe) const
+{
+    auto pathStr = boost::apply_visitor(pathToStringVisitor(), describe.m_path);
+    if (boost::apply_visitor(getPathScopeVisitor(), describe.m_path) == Scope::Absolute)
+        return pathStr;
+    else
+        return joinPaths(m_parser.currentNode(), pathStr);
+}
+
 Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore)
     : m_parser(parser)
     , m_datastore(datastore)