diff options
author | Paul Colomiets <paul@colomiets.name> | 2014-06-23T21·30+0300 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2014-06-27T09·23+0200 |
commit | 858b8f9760a81540b0a95068d96dc5c1628673c3 (patch) | |
tree | b21b484e44a9193c2575eabd691505b47e1734ff | |
parent | 8504e7d60488cb12dd2597734ebd1d3cadf5d153 (diff) |
Add `--json` argument to `nix-instantiate`
-rw-r--r-- | doc/manual/nix-instantiate.xml | 9 | ||||
-rw-r--r-- | src/nix-instantiate/nix-instantiate.cc | 23 |
2 files changed, 26 insertions, 6 deletions
diff --git a/doc/manual/nix-instantiate.xml b/doc/manual/nix-instantiate.xml index b388560a11f2..936f154dde56 100644 --- a/doc/manual/nix-instantiate.xml +++ b/doc/manual/nix-instantiate.xml @@ -124,6 +124,15 @@ input.</para> </varlistentry> + <varlistentry><term><option>--json</option></term> + + <listitem><para>When used with <option>--parse</option> and + <option>--eval</option>, print the resulting expression as an + JSON representation of the abstract syntax tree rather than as an + ATerm.</para></listitem> + + </varlistentry> + <varlistentry><term><option>--strict</option></term> <listitem><para>When used with <option>--eval</option>, diff --git a/src/nix-instantiate/nix-instantiate.cc b/src/nix-instantiate/nix-instantiate.cc index cdd74523ca72..7acc9d501c31 100644 --- a/src/nix-instantiate/nix-instantiate.cc +++ b/src/nix-instantiate/nix-instantiate.cc @@ -5,6 +5,7 @@ #include "get-drvs.hh" #include "attr-path.hh" #include "value-to-xml.hh" +#include "value-to-json.hh" #include "util.hh" #include "store-api.hh" #include "common-opts.hh" @@ -13,6 +14,12 @@ #include <map> #include <iostream> +enum OutputKind { + OUTPUT_PLAIN, + OUTPUT_XML, + OUTPUT_JSON, +}; + using namespace nix; @@ -37,7 +44,7 @@ static bool indirectRoot = false; void processExpr(EvalState & state, const Strings & attrPaths, bool parseOnly, bool strict, Bindings & autoArgs, - bool evalOnly, bool xmlOutput, bool location, Expr * e) + bool evalOnly, OutputKind output, bool location, Expr * e) { if (parseOnly) { std::cout << format("%1%\n") % *e; @@ -58,8 +65,10 @@ void processExpr(EvalState & state, const Strings & attrPaths, vRes = v; else state.autoCallFunction(autoArgs, v, vRes); - if (xmlOutput) + if (output == OUTPUT_XML) printValueAsXML(state, strict, location, vRes, std::cout, context); + else if (output == OUTPUT_JSON) + printValueAsJSON(state, strict, vRes, std::cout, context); else { if (strict) state.strictForceValue(vRes); std::cout << vRes << std::endl; @@ -108,7 +117,7 @@ void run(Strings args) bool findFile = false; bool evalOnly = false; bool parseOnly = false; - bool xmlOutput = false; + OutputKind outputKind = OUTPUT_PLAIN; bool xmlOutputSourceLocation = true; bool strict = false; Strings attrPaths; @@ -145,7 +154,9 @@ void run(Strings args) else if (arg == "--indirect") indirectRoot = true; else if (arg == "--xml") - xmlOutput = true; + outputKind = OUTPUT_XML; + else if (arg == "--json") + outputKind = OUTPUT_JSON; else if (arg == "--no-location") xmlOutputSourceLocation = false; else if (arg == "--strict") @@ -179,7 +190,7 @@ void run(Strings args) if (readStdin) { Expr * e = parseStdin(state); processExpr(state, attrPaths, parseOnly, strict, autoArgs, - evalOnly, xmlOutput, xmlOutputSourceLocation, e); + evalOnly, outputKind, xmlOutputSourceLocation, e); } else if (files.empty() && !fromArgs) files.push_back("./default.nix"); @@ -188,7 +199,7 @@ void run(Strings args) ? state.parseExprFromString(*i, absPath(".")) : state.parseExprFromFile(resolveExprPath(lookupFileArg(state, *i))); processExpr(state, attrPaths, parseOnly, strict, autoArgs, - evalOnly, xmlOutput, xmlOutputSourceLocation, e); + evalOnly, outputKind, xmlOutputSourceLocation, e); } state.printStats(); |