guix-commits
[Top][All Lists]
Advanced

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]