tools: Allow verbose option in ndnsec-list/ndnsec-ls-identity command line tools
Change-Id: I019ae8b8ee049529fbdc3c2ed42ffebddf664d78
Refs: #2197
diff --git a/tools/ndnsec-list.hpp b/tools/ndnsec-list.hpp
index 0406fca..4194cd3 100644
--- a/tools/ndnsec-list.hpp
+++ b/tools/ndnsec-list.hpp
@@ -26,222 +26,149 @@
#include "ndnsec-util.hpp"
+void
+printCertificate(ndn::KeyChain& keyChain,
+ const ndn::Name& certName,
+ bool isDefault,
+ int verboseLevel)
+{
+ if (isDefault)
+ std::cout << " +->* ";
+ else
+ std::cout << " +-> ";
+
+ std::cout << certName << std::endl;
+
+ if (verboseLevel >= 3) {
+ ndn::shared_ptr<ndn::IdentityCertificate> certificate = keyChain.getCertificate(certName);
+ if (static_cast<bool>(certificate))
+ certificate->printCertificate(std::cout, " ");
+ }
+}
+
+void
+printKey(ndn::KeyChain& keyChain,
+ const ndn::Name& keyName,
+ bool isDefault,
+ int verboseLevel)
+{
+ if (isDefault)
+ std::cout << " +->* ";
+ else
+ std::cout << " +-> ";
+
+ std::cout << keyName << std::endl;
+
+ if (verboseLevel >= 2) {
+ std::vector<ndn::Name> defaultCertificates;
+ keyChain.getAllCertificateNamesOfKey(keyName, defaultCertificates, true);
+
+ for (const auto& certName : defaultCertificates)
+ printCertificate(keyChain, certName, true, verboseLevel);
+
+ std::vector<ndn::Name> otherCertificates;
+ keyChain.getAllCertificateNamesOfKey(keyName, otherCertificates, false);
+ for (const auto& certName : otherCertificates)
+ printCertificate(keyChain, certName, false, verboseLevel);
+ }
+}
+
+void
+printIdentity(ndn::KeyChain& keyChain,
+ const ndn::Name& identity,
+ bool isDefault,
+ int verboseLevel)
+{
+ if (isDefault)
+ std::cout << "* ";
+ else
+ std::cout << " ";
+
+ std::cout << identity << std::endl;
+
+ if (verboseLevel >= 1) {
+ std::vector<ndn::Name> defaultKeys;
+ keyChain.getAllKeyNamesOfIdentity(identity, defaultKeys, true);
+ for (const auto& keyName : defaultKeys)
+ printKey(keyChain, keyName, true, verboseLevel);
+
+ std::vector<ndn::Name> otherKeys;
+ keyChain.getAllKeyNamesOfIdentity(identity, otherKeys, false);
+ for (const auto& keyName : otherKeys) {
+ printKey(keyChain, keyName, false, verboseLevel);
+ }
+
+ std::cout << std::endl;
+ }
+}
+
int
ndnsec_list(int argc, char** argv)
{
using namespace ndn;
namespace po = boost::program_options;
- bool isGetId = true;
- bool isGetKey = false;
- bool isGetCert = false;
+ int verboseLevel = 0; // 0 print identity only
+ // 1 print key name
+ // 2 print cert name
+ // 3 print cert content
- po::options_description description("General Usage\n ndnsec list [-h] [-k|c]\nGeneral options");
- description.add_options()
- ("help,h", "produce help message")
- ("key,k", "granularity: key")
- ("key2,K", "granularity: key")
- ("cert,c", "granularity: certificate")
- ("cert2,C", "granularity: certificate")
+ po::options_description options("General Usage\n ndnsec list [-h] [-k|c]\nGeneral options");
+ options.add_options()
+ ("help,h", "produce help message")
+ ("key,k", "granularity: key")
+ ("cert,c", "granularity: certificate")
+ ("verbose,v", accumulator<int>(&verboseLevel),
+ "verbose mode: -v is equivalent to -k, -vv is equivalent to -c")
;
+ po::options_description oldOptions;
+ oldOptions.add_options()
+ ("key2,K", "granularity: key")
+ ("cert2,C", "granularity: certificate");
+
+ po::options_description allOptions;
+ allOptions.add(options).add(oldOptions);
+
po::variables_map vm;
- try
- {
- po::store(po::parse_command_line(argc, argv, description), vm);
- po::notify(vm);
- }
- catch (const std::exception& e)
- {
- std::cerr << "ERROR: " << e.what() << std::endl;
- std::cerr << description << std::endl;
- return 1;
- }
+ try {
+ po::store(po::parse_command_line(argc, argv, allOptions), vm);
+ po::notify(vm);
+ }
+ catch (const std::exception& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ std::cerr << options << std::endl;
+ return 1;
+ }
- if (vm.count("help") != 0)
- {
- std::cerr << description << std::endl;;
- return 0;
- }
+ if (vm.count("help") != 0) {
+ std::cerr << options << std::endl;;
+ return 0;
+ }
+ int tmpVerboseLevel = 0;
if (vm.count("cert") != 0 || vm.count("cert2") != 0)
- {
- isGetCert = true;
- isGetId = false;
- }
+ tmpVerboseLevel = 2;
else if(vm.count("key") != 0 || vm.count("key2") != 0)
- {
- isGetKey = true;
- isGetId = false;
- }
+ tmpVerboseLevel = 1;
+
+ verboseLevel = std::max(verboseLevel, tmpVerboseLevel);
KeyChain keyChain;
- if (isGetId)
- {
- std::vector<Name> defaultIdentities;
- keyChain.getAllIdentities(defaultIdentities, true);
+ std::vector<Name> defaultIdentities;
+ keyChain.getAllIdentities(defaultIdentities, true);
+ for (const auto& identity : defaultIdentities) {
+ printIdentity(keyChain, identity, true, verboseLevel);
+ }
- for (size_t i = 0; i < defaultIdentities.size(); i++)
- std::cout << "* " << defaultIdentities[i] << std::endl;
+ std::vector<Name> otherIdentities;
+ keyChain.getAllIdentities(otherIdentities, false);
+ for (const auto& identity : otherIdentities) {
+ printIdentity(keyChain, identity, false, verboseLevel);
+ }
- std::vector<Name> otherIdentities;
- keyChain.getAllIdentities(otherIdentities, false);
- for (size_t i = 0; i < otherIdentities.size(); i++)
- std::cout << " " << otherIdentities[i] << std::endl;
-
- return 0;
- }
- if (isGetKey)
- {
- std::vector<Name> defaultIdentities;
- keyChain.getAllIdentities(defaultIdentities, true);
-
- for (size_t i = 0; i < defaultIdentities.size(); i++)
- {
- std::cout << "* " << defaultIdentities[i] << std::endl;
-
- std::vector<Name> defaultKeys;
- keyChain.getAllKeyNamesOfIdentity(defaultIdentities[i], defaultKeys, true);
-
- for (size_t j = 0; j < defaultKeys.size(); j++)
- std::cout << " +->* " << defaultKeys[j] << std::endl;
-
- std::vector<Name> otherKeys;
- keyChain.getAllKeyNamesOfIdentity(defaultIdentities[i], otherKeys, false);
-
- for (size_t j = 0; j < otherKeys.size(); j++)
- std::cout << " +-> " << otherKeys[j] << std::endl;
-
- std::cout << std::endl;
- }
-
- std::vector<Name> otherIdentities;
- keyChain.getAllIdentities(otherIdentities, false);
-
- for (size_t i = 0; i < otherIdentities.size(); i++)
- {
- std::cout << " " << otherIdentities[i] << std::endl;
-
- std::vector<Name> defaultKeys;
- keyChain.getAllKeyNamesOfIdentity(otherIdentities[i], defaultKeys, true);
-
- for (size_t j = 0; j < defaultKeys.size(); j++)
- std::cout << " +->* " << defaultKeys[j] << std::endl;
-
- std::vector<Name> otherKeys;
- keyChain.getAllKeyNamesOfIdentity(otherIdentities[i], otherKeys, false);
-
- for (size_t j = 0; j < otherKeys.size(); j++)
- std::cout << " +-> " << otherKeys[j] << std::endl;
-
- std::cout << std::endl;
- }
- return 0;
- }
- if (isGetCert)
- {
- std::vector<Name> defaultIdentities;
- keyChain.getAllIdentities(defaultIdentities, true);
-
- for (size_t i = 0; i < defaultIdentities.size(); i++)
- {
- std::cout << "* " << defaultIdentities[i] << std::endl;
-
- std::vector<Name> defaultKeys;
- keyChain.getAllKeyNamesOfIdentity(defaultIdentities[i], defaultKeys, true);
-
- for (size_t j = 0; j < defaultKeys.size(); j++)
- {
- std::cout << " +->* " << defaultKeys[j] << std::endl;
-
- std::vector<Name> defaultCertificates;
- keyChain.getAllCertificateNamesOfKey(defaultKeys[j], defaultCertificates, true);
-
- for (size_t k = 0; k < defaultCertificates.size(); k++)
- std::cout << " +->* " << defaultCertificates[k] << std::endl;
-
- std::vector<Name> otherCertificates;
- keyChain.getAllCertificateNamesOfKey(defaultKeys[j], otherCertificates, false);
-
- for (size_t k = 0; k < otherCertificates.size(); k++)
- std::cout << " +-> " << otherCertificates[k] << std::endl;
- }
-
- std::vector<Name> otherKeys;
- keyChain.getAllKeyNamesOfIdentity(defaultIdentities[i], otherKeys, false);
-
- for (size_t j = 0; j < otherKeys.size(); j++)
- {
- std::cout << " +-> " << otherKeys[j] << std::endl;
-
- std::vector<Name> defaultCertificates;
- keyChain.getAllCertificateNamesOfKey(otherKeys[j], defaultCertificates, true);
-
- for (size_t k = 0; k < defaultCertificates.size(); k++)
- std::cout << " +->* " << defaultCertificates[k] << std::endl;
-
- std::vector<Name> otherCertificates;
- keyChain.getAllCertificateNamesOfKey(otherKeys[j], otherCertificates, false);
-
- for (size_t k = 0; k < otherCertificates.size(); k++)
- std::cout << " +-> " << otherCertificates[k] << std::endl;
- }
- std::cout << std::endl;
- }
-
- std::vector<Name> otherIdentities;
- keyChain.getAllIdentities(otherIdentities, false);
-
- for (size_t i = 0; i < otherIdentities.size(); i++)
- {
- std::cout << " " << otherIdentities[i] << std::endl;
-
- std::vector<Name> defaultKeys;
- keyChain.getAllKeyNamesOfIdentity(otherIdentities[i], defaultKeys, true);
-
- for (size_t j = 0; j < defaultKeys.size(); j++)
- {
- std::cout << " +->* " << defaultKeys[j] << std::endl;
-
- std::vector<Name> defaultCertificates;
- keyChain.getAllCertificateNamesOfKey(defaultKeys[j], defaultCertificates, true);
-
- for (size_t k = 0; k < defaultCertificates.size(); k++)
- std::cout << " +->* " << defaultCertificates[k] << std::endl;
-
- std::vector<Name> otherCertificates;
- keyChain.getAllCertificateNamesOfKey(defaultKeys[j], otherCertificates, false);
-
- for (size_t k = 0; k < otherCertificates.size(); k++)
- std::cout << " +-> " << otherCertificates[k] << std::endl;
- }
-
- std::vector<Name> otherKeys;
- keyChain.getAllKeyNamesOfIdentity(otherIdentities[i], otherKeys, false);
-
- for (size_t j = 0; j < otherKeys.size(); j++)
- {
- std::cout << " +-> " << otherKeys[j] << std::endl;
-
- std::vector<Name> defaultCertificates;
- keyChain.getAllCertificateNamesOfKey(otherKeys[j], defaultCertificates, true);
-
- for (size_t k = 0; k < defaultCertificates.size(); k++)
- std::cout << " +->* " << defaultCertificates[k] << std::endl;
-
- std::vector<Name> otherCertificates;
- keyChain.getAllCertificateNamesOfKey(otherKeys[j], otherCertificates, false);
-
- for (size_t k = 0; k < otherCertificates.size(); k++)
- std::cout << " +-> " << otherCertificates[k] << std::endl;
- }
-
- std::cout << std::endl;
- }
- return 0;
- }
- return 1;
+ return 0;
}
-#endif //NDNSEC_LIST_HPP
+#endif // NDNSEC_LIST_HPP
diff --git a/tools/ndnsec-util.hpp b/tools/ndnsec-util.hpp
index 1503065..14ddf48 100644
--- a/tools/ndnsec-util.hpp
+++ b/tools/ndnsec-util.hpp
@@ -90,4 +90,136 @@
return ndn::io::load<ndn::IdentityCertificate>(fileName);
}
-#endif //NDNSEC_UTIL_HPP
+
+/**
+ * @brief An accumulating option value to handle multiple incrementing options.
+ *
+ * Based on https://gitorious.org/bwy/bwy/source/8753148c324ddfacb1f3cdc315650586bd7b75a4:use/accumulator.hpp
+ * @sa http://benjaminwolsey.de/node/103
+ */
+template<typename T>
+class AccumulatorType : public boost::program_options::value_semantic
+{
+public:
+ explicit
+ AccumulatorType(T* store)
+ : m_store(store)
+ , m_interval(1)
+ , m_default(0)
+ {
+ }
+
+ virtual
+ ~AccumulatorType()
+ {
+ }
+
+ /// @brief Set the default value for this option.
+ AccumulatorType*
+ setDefaultValue(const T& t)
+ {
+ m_default = t;
+ return this;
+ }
+
+ /**
+ * @brief Set the interval for this option.
+ *
+ * Unlike for program_options::value, this specifies a value
+ * to be applied on each occurrence of the option.
+ */
+ AccumulatorType*
+ setInterval(const T& t) {
+ m_interval = t;
+ return this;
+ }
+
+ virtual std::string
+ name() const
+ {
+ return std::string();
+ }
+
+ // There are no tokens for an AccumulatorType
+ virtual unsigned
+ min_tokens() const
+ {
+ return 0;
+ }
+
+ virtual unsigned
+ max_tokens() const
+ {
+ return 0;
+ }
+
+ // Accumulating from different sources is silly.
+ virtual bool
+ is_composing() const
+ {
+ return false;
+ }
+
+ // Requiring one or more appearances is unlikely.
+ virtual bool
+ is_required() const
+ {
+ return false;
+ }
+
+ /**
+ * @brief Parse options
+ *
+ * Every appearance of the option simply increments the value
+ * There should never be any tokens.
+ */
+ virtual void
+ parse(boost::any& value_store,
+ const std::vector<std::string>& new_tokens,
+ bool utf8) const
+ {
+ if (value_store.empty())
+ value_store = T();
+ boost::any_cast<T&>(value_store) += m_interval;
+ }
+
+ /**
+ * @brief If the option doesn't appear, this is the default value.
+ */
+ virtual bool
+ apply_default(boost::any& value_store) const
+ {
+ value_store = m_default;
+ return true;
+ }
+
+ /**
+ * @brief Notify the user function with the value of the value store.
+ */
+ virtual void
+ notify(const boost::any& value_store) const
+ {
+ const T* val = boost::any_cast<T>(&value_store);
+ if (m_store)
+ *m_store = *val;
+ }
+
+private:
+ T* m_store;
+ T m_interval;
+ T m_default;
+};
+
+template<typename T>
+AccumulatorType<T>* accumulator()
+{
+ return new AccumulatorType<T>(0);
+}
+
+template<typename T>
+AccumulatorType<T>* accumulator(T* store)
+{
+ return new AccumulatorType<T>(store);
+}
+
+#endif // NDNSEC_UTIL_HPP