[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
31/118: Add `--json` argument to `nix-instantiate`
From: |
Ludovic Courtès |
Subject: |
31/118: Add `--json` argument to `nix-instantiate` |
Date: |
Tue, 19 May 2015 14:45:25 +0000 |
civodul pushed a commit to branch nix
in repository guix.
commit 858b8f9760a81540b0a95068d96dc5c1628673c3
Author: Paul Colomiets <address@hidden>
Date: Tue Jun 24 00:30:22 2014 +0300
Add `--json` argument to `nix-instantiate`
---
doc/manual/nix-instantiate.xml | 9 +++++++++
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 b388560..936f154 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 cdd7452..7acc9d5 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();
- 21/118: Add autoloads, make code more concise & idiomatic, (continued)
- 21/118: Add autoloads, make code more concise & idiomatic, Ludovic Courtès, 2015/05/19
- 25/118: Fix bogus warnings about dumping large paths, Ludovic Courtès, 2015/05/19
- 20/118: == operator: Ignore string context, Ludovic Courtès, 2015/05/19
- 17/118: nix-env -qa --json: Generate valid JSON even if there are invalid meta attrs, Ludovic Courtès, 2015/05/19
- 23/118: findFile: Realise the context of the path attributes, Ludovic Courtès, 2015/05/19
- 11/118: Rephrase @ operator description, Ludovic Courtès, 2015/05/19
- 29/118: Merge branch 'shlevy-import-native', Ludovic Courtès, 2015/05/19
- 12/118: dev-shell is a bash script, not sh, Ludovic Courtès, 2015/05/19
- 22/118: Share code between scopedImport and import, Ludovic Courtès, 2015/05/19
- 26/118: Don't use member initialisers, Ludovic Courtès, 2015/05/19
- 31/118: Add `--json` argument to `nix-instantiate`,
Ludovic Courtès <=
- 18/118: Print a warning when loading a large path into memory, Ludovic Courtès, 2015/05/19
- 27/118: Add importNative primop, Ludovic Courtès, 2015/05/19
- 24/118: Drop ImportError and FindError, Ludovic Courtès, 2015/05/19
- 28/118: Only add the importNative primop if the allow-arbitrary-code-during-evaluation option is true (default false), Ludovic Courtès, 2015/05/19
- 30/118: allow-arbitrary-code-during-evaluation -> allow-unsafe-native-code-during-evaluation, Ludovic Courtès, 2015/05/19
- 34/118: Add builtin function ‘fromJSON’, Ludovic Courtès, 2015/05/19
- 36/118: Fix compilation error on some versions of GCC, Ludovic Courtès, 2015/05/19
- 32/118: Style fix, Ludovic Courtès, 2015/05/19
- 46/118: Fix use of sysread, Ludovic Courtès, 2015/05/19
- 33/118: Manual: html -> xhtml, Ludovic Courtès, 2015/05/19