pw_kvs: Simple debug command line interface

Add the pw_kvs:debug_cli executable for performing very basic manual
testing locally.

Change-Id: Ifab6eb237f92a1b09ed7e3797485f9889e6cec64
diff --git a/pw_kvs/BUILD b/pw_kvs/BUILD
index 93d9bfd..cb9464b 100644
--- a/pw_kvs/BUILD
+++ b/pw_kvs/BUILD
@@ -91,3 +91,14 @@
         "//pw_log",
     ],
 )
+
+cc_binary(
+    name = "debug_cli",
+    srcs = ["debug_cli.cc"],
+    copts = ["-std=c++17"],
+    deps = [
+        ":crc16",
+        ":in_memory_fake_flash",
+        ":pw_kvs",
+    ],
+)
diff --git a/pw_kvs/BUILD.gn b/pw_kvs/BUILD.gn
index 17cc6b1..c82141e 100644
--- a/pw_kvs/BUILD.gn
+++ b/pw_kvs/BUILD.gn
@@ -68,6 +68,17 @@
   ]
 }
 
+executable("debug_cli") {
+  sources = [
+    "debug_cli.cc",
+  ]
+  deps = [
+    ":crc16",
+    ":in_memory_fake_flash",
+    ":pw_kvs",
+  ]
+}
+
 pw_test_group("tests") {
   tests = [
     ":checksum_test",
diff --git a/pw_kvs/debug_cli.cc b/pw_kvs/debug_cli.cc
new file mode 100644
index 0000000..547cfbd
--- /dev/null
+++ b/pw_kvs/debug_cli.cc
@@ -0,0 +1,78 @@
+// Copyright 2020 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include "pw_kvs/crc16_checksum.h"
+#include "pw_kvs/in_memory_fake_flash.h"
+#include "pw_kvs/key_value_store.h"
+
+namespace pw::kvs {
+namespace {
+
+using std::byte;
+
+ChecksumCrc16 checksum;
+constexpr EntryHeaderFormat format{.magic = 0xBAD'C0D3, .checksum = &checksum};
+
+void Run() {
+  // 4 x 4k sectors, 16 byte alignment
+  InMemoryFakeFlash<4 * 1024, 4> test_flash(16);
+
+  FlashPartition test_partition(&test_flash, 0, test_flash.sector_count());
+  test_partition.Erase(0, test_partition.sector_count());
+
+  KeyValueStore kvs(&test_partition, format);
+  kvs.Init();
+
+  std::string line;
+  while (std::getline(std::cin, line)) {
+    std::istringstream data(line);
+    std::string key, value;
+    data >> key >> value;
+
+    if (key == "Init()") {
+      printf("Init: %s\n", kvs.Init().str());
+    } else if (!key.empty()) {
+      printf("Adding %s='%s'\n", key.c_str(), value.c_str());
+
+      if (Status status = kvs.Put(key, as_bytes(span(value))); !status.ok()) {
+        printf("FAILED to Put key: %s\n", status.str());
+      }
+    }
+
+    int i = 0;
+    printf("KVS CONTENTS ----------------------------------------------\n");
+    for (auto& entry : kvs) {
+      char value[64] = {};
+      if (Status status = entry.Get(as_writable_bytes(span(value)));
+          status.ok()) {
+        printf("%2d: %s='%s'\n", ++i, entry.key().data(), value);
+      } else {
+        printf("FAILED to Get key %s: %s\n", entry.key().data(), status.str());
+      }
+    }
+    printf("---------------------------------------------- END CONTENTS\n");
+  }
+}
+
+}  // namespace
+}  // namespace pw::kvs
+
+int main() {
+  pw::kvs::Run();
+  return 0;
+}