aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Van den Broeck <nick.van.den.broeck666@gmail.com>2019-02-27 19:54:18 +0100
committerNick Van den Broeck <nick.van.den.broeck666@gmail.com>2019-03-07 11:56:43 +0100
commit9ff1a9ea65bdeb520becb843b8300a23fb88a5a9 (patch)
treef10d20eb177b1a069ac275465a2af372f68fc34c
parentcfb6ab80cea7f0ed3f525e8120f2e569f963fa0e (diff)
Implemented json flag for `nix flake info`
-rw-r--r--src/libexpr/primops/flake.cc2
-rw-r--r--src/libexpr/primops/flake.hh2
-rw-r--r--src/nix/command.cc9
-rw-r--r--src/nix/command.hh7
-rw-r--r--src/nix/flake.cc14
5 files changed, 29 insertions, 5 deletions
diff --git a/src/libexpr/primops/flake.cc b/src/libexpr/primops/flake.cc
index 9f137a0b9..ac0421549 100644
--- a/src/libexpr/primops/flake.cc
+++ b/src/libexpr/primops/flake.cc
@@ -141,7 +141,7 @@ static FlakeSourceInfo fetchFlake(EvalState & state, const FlakeRef & flakeRef)
else abort();
}
-static Flake getFlake(EvalState & state, const FlakeRef & flakeRef)
+Flake getFlake(EvalState & state, const FlakeRef & flakeRef)
{
auto sourceInfo = fetchFlake(state, flakeRef);
debug("got flake source '%s' with revision %s",
diff --git a/src/libexpr/primops/flake.hh b/src/libexpr/primops/flake.hh
index af28bc5b0..194b969a2 100644
--- a/src/libexpr/primops/flake.hh
+++ b/src/libexpr/primops/flake.hh
@@ -34,5 +34,5 @@ struct Flake
// content hash
};
-static Flake getFlake(EvalState & state, const FlakeRef & flakeRef);
+Flake getFlake(EvalState & state, const FlakeRef & flakeRef);
}
diff --git a/src/nix/command.cc b/src/nix/command.cc
index 5967ab36c..e1e32aaae 100644
--- a/src/nix/command.cc
+++ b/src/nix/command.cc
@@ -27,6 +27,15 @@ void StoreCommand::run()
run(getStore());
}
+JsonFormattable::JsonFormattable()
+{
+ mkFlag()
+ .longName("json-formattable")
+ .shortName('j')
+ .description("output will be printed as json")
+ .handler([&]() { jsonFormatting = true; });
+}
+
StorePathsCommand::StorePathsCommand(bool recursive)
: recursive(recursive)
{
diff --git a/src/nix/command.hh b/src/nix/command.hh
index b3248222e..5c2f8c304 100644
--- a/src/nix/command.hh
+++ b/src/nix/command.hh
@@ -26,6 +26,13 @@ private:
std::shared_ptr<Store> _store;
};
+struct JsonFormattable : virtual Command
+{
+ bool jsonFormatting = false;;
+
+ JsonFormattable();
+};
+
struct Buildable
{
Path drvPath; // may be empty
diff --git a/src/nix/flake.cc b/src/nix/flake.cc
index 099425688..22e5b297c 100644
--- a/src/nix/flake.cc
+++ b/src/nix/flake.cc
@@ -4,6 +4,7 @@
#include "shared.hh"
#include "progress-bar.hh"
#include "eval.hh"
+#include <nlohmann/json.hpp>
using namespace nix;
@@ -33,7 +34,7 @@ struct CmdFlakeList : StoreCommand, MixEvalArgs
}
};
-struct CmdFlakeInfo : FlakeCommand
+struct CmdFlakeInfo : FlakeCommand, JsonFormattable
{
std::string name() override
{
@@ -49,8 +50,15 @@ struct CmdFlakeInfo : FlakeCommand
{
auto evalState = std::make_shared<EvalState>(searchPath, store);
nix::Flake flake = nix::getFlake(*evalState, FlakeRef(flakeUri));
- std::cout << "Location: " << flake.path << "\n";
- std::cout << "Description: " << flake.description << "\n";
+ if (jsonFormatting) {
+ nlohmann::json j;
+ j["location"] = flake.path;
+ j["description"] = flake.description;
+ std::cout << j.dump(4) << std::endl;
+ } else {
+ std::cout << "Location: " << flake.path << "\n";
+ std::cout << "Description: " << flake.description << "\n";
+ }
}
};