From efa4bdbfcd1489527bcf6f20a49c9a3bca8bbf6b Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 20 Apr 2017 17:34:47 +0200 Subject: Improve nix show-config --json In particular, show descriptions. This could be used for manpage generation etc. --- src/libutil/config.cc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/libutil/config.cc') diff --git a/src/libutil/config.cc b/src/libutil/config.cc index 72b6cf806..62c6433c7 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -1,5 +1,6 @@ #include "config.hh" #include "args.hh" +#include "json.hh" namespace nix { @@ -103,6 +104,17 @@ void Config::resetOverriden() s.second.setting->overriden = false; } +void Config::toJSON(JSONObject & out) +{ + for (auto & s : _settings) + if (!s.second.isAlias) { + JSONObject out2(out.object(s.first)); + out2.attr("description", s.second.setting->description); + JSONPlaceholder out3(out2.placeholder("value")); + s.second.setting->toJSON(out3); + } +} + AbstractSetting::AbstractSetting( const std::string & name, const std::string & description, @@ -111,6 +123,17 @@ AbstractSetting::AbstractSetting( { } +void AbstractSetting::toJSON(JSONPlaceholder & out) +{ + out.write(to_string()); +} + +template +void BaseSetting::toJSON(JSONPlaceholder & out) +{ + out.write(value); +} + template<> void BaseSetting::set(const std::string & str) { value = str; @@ -161,6 +184,13 @@ template<> std::string BaseSetting::to_string() return concatStringsSep(" ", value); } +template<> void BaseSetting::toJSON(JSONPlaceholder & out) +{ + JSONList list(out.list()); + for (auto & s : value) + list.elem(s); +} + template<> void BaseSetting::set(const std::string & str) { value = tokenizeString(str); @@ -171,12 +201,20 @@ template<> std::string BaseSetting::to_string() return concatStringsSep(" ", value); } +template<> void BaseSetting::toJSON(JSONPlaceholder & out) +{ + JSONList list(out.list()); + for (auto & s : value) + list.elem(s); +} + template class BaseSetting; template class BaseSetting; template class BaseSetting; template class BaseSetting; template class BaseSetting; template class BaseSetting; +template class BaseSetting; void PathSetting::set(const std::string & str) { -- cgit v1.2.3