Add `--json` argument to `nix-instantiate`

This commit is contained in:
Paul Colomiets 2014-06-24 00:30:22 +03:00 committed by Eelco Dolstra
parent 8504e7d604
commit 858b8f9760
2 changed files with 26 additions and 6 deletions

View File

@ -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>,

View File

@ -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();