about summary refs log tree commit diff
path: root/src/nix/ls.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-11-14T13·23+0100
committerEelco Dolstra <edolstra@gmail.com>2017-11-14T13·23+0100
commitbac8055652964d9ad5202011befb6b199463ddef (patch)
treeb37165578b2e6b5bf0c0739af636acbfa63c0be9 /src/nix/ls.cc
parentc0d93a01ee996427a1ec6b7602e655c8405624d9 (diff)
nix ls-{store,nar}: Add --json flag
Diffstat (limited to 'src/nix/ls.cc')
-rw-r--r--src/nix/ls.cc21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/nix/ls.cc b/src/nix/ls.cc
index 5a5fa8f62d92..8566b8b0682c 100644
--- a/src/nix/ls.cc
+++ b/src/nix/ls.cc
@@ -2,10 +2,12 @@
 #include "store-api.hh"
 #include "fs-accessor.hh"
 #include "nar-accessor.hh"
+#include "common-args.hh"
+#include "json.hh"
 
 using namespace nix;
 
-struct MixLs : virtual Args
+struct MixLs : virtual Args, MixJSON
 {
     std::string path;
 
@@ -20,7 +22,7 @@ struct MixLs : virtual Args
         mkFlag('d', "directory", "show directories rather than their contents", &showDirectory);
     }
 
-    void list(ref<FSAccessor> accessor)
+    void listText(ref<FSAccessor> accessor)
     {
         std::function<void(const FSAccessor::Stat &, const Path &, const std::string &, bool)> doPath;
 
@@ -61,10 +63,6 @@ struct MixLs : virtual Args
                 showFile(curPath, relPath);
         };
 
-        if (path == "/") {
-            path = "";
-        }
-
         auto st = accessor->stat(path);
         if (st.type == FSAccessor::Type::tMissing)
             throw Error(format("path '%1%' does not exist") % path);
@@ -72,6 +70,17 @@ struct MixLs : virtual Args
             st.type == FSAccessor::Type::tDirectory ? "." : baseNameOf(path),
             showDirectory);
     }
+
+    void list(ref<FSAccessor> accessor)
+    {
+        if (path == "/") path = "";
+
+        if (json) {
+            JSONPlaceholder jsonRoot(std::cout, true);
+            listNar(jsonRoot, accessor, path);
+        } else
+            listText(accessor);
+    }
 };
 
 struct CmdLsStore : StoreCommand, MixLs