2023-02-05 19:16:17 +02:00
|
|
|
#include "command-installable-value.hh"
|
2017-04-25 12:23:47 +03:00
|
|
|
#include "common-args.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "eval.hh"
|
2020-12-02 22:25:32 +02:00
|
|
|
#include "eval-inline.hh"
|
2017-04-25 12:23:47 +03:00
|
|
|
#include "value-to-json.hh"
|
2018-01-17 13:04:44 +02:00
|
|
|
#include "progress-bar.hh"
|
2017-04-25 12:23:47 +03:00
|
|
|
|
2022-11-16 17:49:49 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2017-04-25 12:23:47 +03:00
|
|
|
using namespace nix;
|
|
|
|
|
2023-02-05 19:16:17 +02:00
|
|
|
struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
|
2017-04-25 12:23:47 +03:00
|
|
|
{
|
2017-05-03 15:08:18 +03:00
|
|
|
bool raw = false;
|
2020-06-17 19:11:53 +03:00
|
|
|
std::optional<std::string> apply;
|
2020-12-02 22:25:32 +02:00
|
|
|
std::optional<Path> writeTo;
|
2017-05-03 15:08:18 +03:00
|
|
|
|
2023-02-05 19:16:17 +02:00
|
|
|
CmdEval() : InstallableValueCommand()
|
2017-05-03 15:08:18 +03:00
|
|
|
{
|
2021-01-27 13:06:03 +02:00
|
|
|
addFlag({
|
|
|
|
.longName = "raw",
|
|
|
|
.description = "Print strings without quotes or escaping.",
|
|
|
|
.handler = {&raw, true},
|
|
|
|
});
|
2020-06-17 19:11:53 +03:00
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "apply",
|
2021-01-13 15:18:04 +02:00
|
|
|
.description = "Apply the function *expr* to each argument.",
|
2020-06-17 19:11:53 +03:00
|
|
|
.labels = {"expr"},
|
|
|
|
.handler = {&apply},
|
|
|
|
});
|
2020-12-02 22:25:32 +02:00
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "write-to",
|
2021-01-13 15:18:04 +02:00
|
|
|
.description = "Write a string or attrset of strings to *path*.",
|
2020-12-02 22:25:32 +02:00
|
|
|
.labels = {"path"},
|
|
|
|
.handler = {&writeTo},
|
|
|
|
});
|
2017-05-03 15:08:18 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 12:23:47 +03:00
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "evaluate a Nix expression";
|
|
|
|
}
|
|
|
|
|
2020-12-09 19:34:52 +02:00
|
|
|
std::string doc() override
|
2017-09-07 21:37:46 +03:00
|
|
|
{
|
2020-12-09 19:34:52 +02:00
|
|
|
return
|
|
|
|
#include "eval.md"
|
|
|
|
;
|
2017-09-07 21:37:46 +03:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:18:23 +03:00
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
2023-02-05 19:16:17 +02:00
|
|
|
void run(ref<Store> store, ref<InstallableValue> installable) override
|
2017-04-25 12:23:47 +03:00
|
|
|
{
|
2017-05-03 15:08:18 +03:00
|
|
|
if (raw && json)
|
|
|
|
throw UsageError("--raw and --json are mutually exclusive");
|
|
|
|
|
2017-04-25 12:23:47 +03:00
|
|
|
auto state = getEvalState();
|
|
|
|
|
2020-12-02 22:25:32 +02:00
|
|
|
auto [v, pos] = installable->toValue(*state);
|
2018-01-17 13:03:06 +02:00
|
|
|
PathSet context;
|
2018-01-17 13:04:44 +02:00
|
|
|
|
2020-06-17 19:11:53 +03:00
|
|
|
if (apply) {
|
|
|
|
auto vApply = state->allocValue();
|
|
|
|
state->eval(state->parseExprFromString(*apply, absPath(".")), *vApply);
|
|
|
|
auto vRes = state->allocValue();
|
|
|
|
state->callFunction(*vApply, *v, *vRes, noPos);
|
|
|
|
v = vRes;
|
|
|
|
}
|
|
|
|
|
2020-12-02 22:25:32 +02:00
|
|
|
if (writeTo) {
|
|
|
|
stopProgressBar();
|
|
|
|
|
|
|
|
if (pathExists(*writeTo))
|
|
|
|
throw Error("path '%s' already exists", *writeTo);
|
|
|
|
|
2022-03-04 20:31:59 +02:00
|
|
|
std::function<void(Value & v, const PosIdx pos, const Path & path)> recurse;
|
2020-12-02 22:25:32 +02:00
|
|
|
|
2022-03-04 20:31:59 +02:00
|
|
|
recurse = [&](Value & v, const PosIdx pos, const Path & path)
|
2020-12-02 22:25:32 +02:00
|
|
|
{
|
2021-11-27 19:40:24 +02:00
|
|
|
state->forceValue(v, pos);
|
2020-12-17 15:45:45 +02:00
|
|
|
if (v.type() == nString)
|
2020-12-02 22:25:32 +02:00
|
|
|
// FIXME: disallow strings with contexts?
|
|
|
|
writeFile(path, v.string.s);
|
2020-12-17 15:45:45 +02:00
|
|
|
else if (v.type() == nAttrs) {
|
2020-12-02 22:25:32 +02:00
|
|
|
if (mkdir(path.c_str(), 0777) == -1)
|
|
|
|
throw SysError("creating directory '%s'", path);
|
2022-03-05 15:40:24 +02:00
|
|
|
for (auto & attr : *v.attrs) {
|
|
|
|
std::string_view name = state->symbols[attr.name];
|
2020-12-02 22:25:32 +02:00
|
|
|
try {
|
2022-03-05 15:40:24 +02:00
|
|
|
if (name == "." || name == "..")
|
|
|
|
throw Error("invalid file name '%s'", name);
|
|
|
|
recurse(*attr.value, attr.pos, concatStrings(path, "/", name));
|
2020-12-02 22:25:32 +02:00
|
|
|
} catch (Error & e) {
|
2022-03-04 20:31:59 +02:00
|
|
|
e.addTrace(
|
|
|
|
state->positions[attr.pos],
|
2022-03-05 15:40:24 +02:00
|
|
|
hintfmt("while evaluating the attribute '%s'", name));
|
2020-12-02 22:25:32 +02:00
|
|
|
throw;
|
|
|
|
}
|
2022-03-05 15:40:24 +02:00
|
|
|
}
|
2020-12-02 22:25:32 +02:00
|
|
|
}
|
|
|
|
else
|
2022-03-04 20:31:59 +02:00
|
|
|
throw TypeError("value at '%s' is not a string or an attribute set", state->positions[pos]);
|
2020-12-02 22:25:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
recurse(*v, pos, *writeTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (raw) {
|
2020-04-16 14:46:37 +03:00
|
|
|
stopProgressBar();
|
2023-03-02 16:02:24 +02:00
|
|
|
writeFull(STDOUT_FILENO, *state->coerceToString(noPos, *v, context, "while generating the eval command output"));
|
2020-12-02 22:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (json) {
|
2023-03-02 16:02:24 +02:00
|
|
|
logger->cout("%s", printValueAsJSON(*state, true, *v, pos, context, false));
|
2020-12-02 22:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2018-01-17 13:03:06 +02:00
|
|
|
state->forceValueDeep(*v);
|
2022-03-05 15:40:24 +02:00
|
|
|
logger->cout("%s", printValue(*state, *v));
|
2017-04-25 12:23:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static auto rCmdEval = registerCommand<CmdEval>("eval");
|