Add get_zone_ids, get_active_zone_id and get_zones_status to cli 70/33270/7
authorMateusz Malicki <m.malicki2@samsung.com>
Wed, 7 Jan 2015 13:29:30 +0000 (14:29 +0100)
committerJan Olszak <j.olszak@samsung.com>
Tue, 13 Jan 2015 08:51:44 +0000 (00:51 -0800)
[Feature]       Add get_zone_ids, get_active_zone_id and get_zones_status to cli
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, run appropriate functions (through cli)

Change-Id: If6823243d66606d28bf6b45c5b2ece8ab7c06e49

cli/command-line-interface.cpp
cli/command-line-interface.hpp
cli/main.cpp

index 1222a86..06be364 100644 (file)
 #include <functional>
 #include <ostream>
 #include <iostream>
+#include <sstream>
+#include <iomanip>
+#include <algorithm>
+#include <vector>
 #include <fcntl.h>
+#include <cassert>
 
 using namespace std;
 
@@ -82,6 +87,14 @@ finish:
     }
 }
 
+template<typename T>
+string stringAsInStream(const T& value)
+{
+    std::ostringstream stream;
+    stream << value;
+    return stream.str();
+}
+
 ostream& operator<<(ostream& out, const VsmZoneState& state)
 {
     const char* name;
@@ -113,6 +126,30 @@ ostream& operator<<(ostream& out, const VsmZone& zone)
     return out;
 }
 
+typedef vector<vector<string>> Table;
+
+ostream& operator<<(ostream& out, const Table& table)
+{
+    vector<size_t> sizes;
+    for (const auto& row : table) {
+        if (sizes.size() < row.size()) {
+            sizes.resize(row.size());
+        }
+        for (size_t i = 0; i < row.size(); ++i) {
+            sizes[i] = max(sizes[i], row[i].length());
+        }
+    }
+
+    for (const auto& row : table) {
+        for (size_t i = 0; i < row.size(); ++i) {
+            out << left << setw(sizes[i]+2) << row[i];
+        }
+        out << "\n";
+    }
+
+    return out;
+}
+
 } // namespace
 
 void CommandLineInterface::printUsage(std::ostream& out) const
@@ -210,6 +247,58 @@ void unlock_zone(int pos, int argc, const char** argv)
     one_shot(bind(vsm_unlock_zone, _1, argv[pos + 1]));
 }
 
+void get_zones_status(int /* pos */, int /* argc */, const char** /* argv */)
+{
+    using namespace std::placeholders;
+
+    VsmArrayString ids;
+    VsmString activeId;
+    Table table;
+
+    one_shot(bind(vsm_get_zone_ids, _1, &ids));
+    one_shot(bind(vsm_get_active_zone_id, _1, &activeId));
+    table.push_back({"Active", "Id", "State", "Terminal", "Root"});
+    for (VsmString* id = ids; *id; ++id) {
+        VsmZone zone;
+        one_shot(bind(vsm_lookup_zone_by_id, _1, *id, &zone));
+        assert(string(zone->id) == string(*id));
+        table.push_back({string(zone->id) == string(activeId) ? "*" : "",
+                         zone->id,
+                         stringAsInStream(zone->state),
+                         to_string(zone->terminal),
+                         zone->rootfs_path});
+        vsm_zone_free(zone);
+    }
+    vsm_string_free(activeId);
+    vsm_array_string_free(ids);
+    cout << table << endl;
+}
+
+void get_zone_ids(int /* pos */, int /* argc */, const char** /* argv */)
+{
+    using namespace std::placeholders;
+
+    VsmArrayString ids;
+    one_shot(bind(vsm_get_zone_ids, _1, &ids));
+    string delim;
+    for (VsmString* id = ids; *id; ++id) {
+        cout << delim << *id;
+        delim = ", ";
+    }
+    cout << endl;
+    vsm_array_string_free(ids);
+}
+
+void get_active_zone_id(int /* pos */, int /* argc */, const char** /* argv */)
+{
+    using namespace std::placeholders;
+
+    VsmString id;
+    one_shot(bind(vsm_get_active_zone_id, _1, &id));
+    cout << id << endl;
+    vsm_string_free(id);
+}
+
 void lookup_zone_by_id(int pos, int argc, const char** argv)
 {
     using namespace std::placeholders;
index 6a655d8..055bb1e 100644 (file)
@@ -147,6 +147,26 @@ void lock_zone(int pos, int argc, const char** argv);
 void unlock_zone(int pos, int argc, const char** argv);
 
 /**
+ * Parses command line arguments and prints list of zone with
+ * some useful informations (id, state, terminal, root path)
+ */
+void get_zones_status(int pos, int argc, const char** argv);
+
+/**
+ * Parses command line arguments and call vsm_get_zone_ids
+ *
+ * @see vsm_get_zone_ids
+ */
+void get_zone_ids(int pos, int argc, const char** argv);
+
+/**
+ * Parses command line arguments and call vsm_get_active_zone_id
+ *
+ * @see vsm_get_active_zone_id
+ */
+void get_active_zone_id(int pos, int argc, const char** argv);
+
+/**
  * Parses command line arguments and call vsm_lookup_zone_by_id
  *
  * @see vsm_lookup_zone_by_id
index 1afe134..d003d10 100644 (file)
@@ -92,6 +92,30 @@ std::map<std::string, CommandLineInterface> commands = {
         }
     },
     {
+        "get_zones_status", {
+            get_zones_status,
+            "get_zones_status",
+            "Get list of zone with some useful informations (id, state, terminal, root path)",
+            {}
+        }
+    },
+    {
+        "get_zone_ids", {
+            get_zone_ids,
+            "get_zone_ids",
+            "Get all zone ids",
+            {}
+        }
+    },
+    {
+        "get_active_zone_id", {
+            get_active_zone_id,
+            "get_active_zone_id",
+            "Get active (foreground) zone ids",
+            {}
+        }
+    },
+    {
         "lookup_zone_by_id", {
             lookup_zone_by_id,
             "lookup_zone_by_id zone_id",