1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 10:11:47 +02:00

Merge pull request #4304 from NixOS/separate-manpages

Separate manpages for 'nix' subcommands
This commit is contained in:
Eelco Dolstra 2020-12-03 13:38:29 +01:00 committed by GitHub
commit 4f25644a13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 166 additions and 33 deletions

View file

@ -3,6 +3,7 @@
#include "shared.hh"
#include "store-api.hh"
#include "eval.hh"
#include "eval-inline.hh"
#include "json.hh"
#include "value-to-json.hh"
#include "progress-bar.hh"
@ -13,6 +14,7 @@ struct CmdEval : MixJSON, InstallableCommand
{
bool raw = false;
std::optional<std::string> apply;
std::optional<Path> writeTo;
CmdEval()
{
@ -24,6 +26,13 @@ struct CmdEval : MixJSON, InstallableCommand
.labels = {"expr"},
.handler = {&apply},
});
addFlag({
.longName = "write-to",
.description = "write a string or attrset of strings to 'path'",
.labels = {"path"},
.handler = {&writeTo},
});
}
std::string description() override
@ -66,7 +75,7 @@ struct CmdEval : MixJSON, InstallableCommand
auto state = getEvalState();
auto v = installable->toValue(*state).first;
auto [v, pos] = installable->toValue(*state);
PathSet context;
if (apply) {
@ -77,13 +86,51 @@ struct CmdEval : MixJSON, InstallableCommand
v = vRes;
}
if (raw) {
if (writeTo) {
stopProgressBar();
if (pathExists(*writeTo))
throw Error("path '%s' already exists", *writeTo);
std::function<void(Value & v, const Pos & pos, const Path & path)> recurse;
recurse = [&](Value & v, const Pos & pos, const Path & path)
{
state->forceValue(v);
if (v.type == tString)
// FIXME: disallow strings with contexts?
writeFile(path, v.string.s);
else if (v.type == tAttrs) {
if (mkdir(path.c_str(), 0777) == -1)
throw SysError("creating directory '%s'", path);
for (auto & attr : *v.attrs)
try {
if (attr.name == "." || attr.name == "..")
throw Error("invalid file name '%s'", attr.name);
recurse(*attr.value, *attr.pos, path + "/" + std::string(attr.name));
} catch (Error & e) {
e.addTrace(*attr.pos, hintfmt("while evaluating the attribute '%s'", attr.name));
throw;
}
}
else
throw TypeError("value at '%s' is not a string or an attribute set", pos);
};
recurse(*v, pos, *writeTo);
}
else if (raw) {
stopProgressBar();
std::cout << state->coerceToString(noPos, *v, context);
} else if (json) {
}
else if (json) {
JSONPlaceholder jsonOut(std::cout);
printValueAsJSON(*state, true, *v, jsonOut, context);
} else {
}
else {
state->forceValueDeep(*v);
logger->cout("%s", *v);
}

View file

@ -149,6 +149,50 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
}
};
static void showHelp(std::vector<std::string> subcommand)
{
showManPage(subcommand.empty() ? "nix" : fmt("nix3-%s", concatStringsSep("-", subcommand)));
}
struct CmdHelp : Command
{
std::vector<std::string> subcommand;
CmdHelp()
{
expectArgs({
.label = "subcommand",
.handler = {&subcommand},
});
}
std::string description() override
{
return "show help about 'nix' or a particular subcommand";
}
Examples examples() override
{
return {
Example{
"To show help about 'nix' in general:",
"nix help"
},
Example{
"To show help about a particular subcommand:",
"nix help run"
},
};
}
void run() override
{
showHelp(subcommand);
}
};
static auto rCmdHelp = registerCommand<CmdHelp>("help");
void mainWrapped(int argc, char * * argv)
{
/* The chroot helper needs to be run before any threads have been