mirror of
https://github.com/NixOS/nix.git
synced 2025-12-24 01:41:08 +01:00
nix-cli: Add --json --pretty / --no-pretty
Default: istty(stdout) This refactors `nix develop` internals a bit to use the `json` type more. The assertion now operates in the in-memory json instead of re-parsing it. While this is technically a weaker guarantee, we should be able to rely on the library to get this right. It's its most essential purpose.
This commit is contained in:
parent
e9af7a0749
commit
fe00dfbd56
17 changed files with 151 additions and 29 deletions
|
|
@ -1,3 +1,5 @@
|
|||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "common-args.hh"
|
||||
#include "args/root.hh"
|
||||
#include "config-global.hh"
|
||||
|
|
@ -93,5 +95,18 @@ void MixCommonArgs::initialFlagsProcessed()
|
|||
pluginsInited();
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename>
|
||||
void MixPrintJSON::printJSON(const T /* nlohmann::json */ & json)
|
||||
{
|
||||
auto suspension = logger->suspend();
|
||||
if (outputPretty) {
|
||||
logger->writeToStdout(json.dump(2));
|
||||
} else {
|
||||
logger->writeToStdout(json.dump());
|
||||
}
|
||||
}
|
||||
|
||||
template void MixPrintJSON::printJSON(const nlohmann::json & json);
|
||||
|
||||
|
||||
} // namespace nix
|
||||
|
|
|
|||
|
|
@ -35,7 +35,66 @@ struct MixDryRun : virtual Args
|
|||
}
|
||||
};
|
||||
|
||||
struct MixJSON : virtual Args
|
||||
/**
|
||||
* Commands that can print JSON according to the
|
||||
* `--pretty`/`--no-pretty` flag.
|
||||
*
|
||||
* This is distinct from MixJSON, because for some commands,
|
||||
* JSON outputs is not optional.
|
||||
*/
|
||||
struct MixPrintJSON : virtual Args
|
||||
{
|
||||
bool outputPretty = isatty(STDOUT_FILENO);
|
||||
|
||||
MixPrintJSON()
|
||||
{
|
||||
addFlag({
|
||||
.longName = "pretty",
|
||||
.description =
|
||||
R"(
|
||||
Print multi-line, indented JSON output for readability.
|
||||
|
||||
Default: indent if output is to a terminal.
|
||||
|
||||
This option is only effective when `--json` is also specified.
|
||||
)",
|
||||
//.category = commonArgsCategory,
|
||||
.handler = {&outputPretty, true},
|
||||
});
|
||||
addFlag({
|
||||
.longName = "no-pretty",
|
||||
.description =
|
||||
R"(
|
||||
Print compact JSON output on a single line, even when the output is a terminal.
|
||||
Some commands may print multiple JSON objects on separate lines.
|
||||
|
||||
See `--pretty`.
|
||||
)",
|
||||
//.category = commonArgsCategory,
|
||||
.handler = {&outputPretty, false},
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Print an `nlohmann::json` to stdout
|
||||
*
|
||||
* - respecting `--pretty` / `--no-pretty`.
|
||||
* - suspending the progress bar
|
||||
*
|
||||
* This is a template to avoid accidental coercions from `string` to `json` in the caller,
|
||||
* to avoid mistakenly passing an already serialized JSON to this function.
|
||||
*
|
||||
* It is not recommended to print a JSON string - see the JSON guidelines
|
||||
* about extensibility, https://nix.dev/manual/nix/development/development/json-guideline.html -
|
||||
* but you _can_ print a sole JSON string by explicitly coercing it to
|
||||
* `nlohmann::json` first.
|
||||
*/
|
||||
template <typename T, typename = std::enable_if_t<std::is_same_v<T, nlohmann::json>>>
|
||||
void printJSON(const T & json);
|
||||
};
|
||||
|
||||
/** Optional JSON support via `--json` flag */
|
||||
struct MixJSON : virtual Args, virtual MixPrintJSON
|
||||
{
|
||||
bool json = false;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue