aboutsummaryrefslogtreecommitdiff
path: root/src/nix-env/nix-env.cc
diff options
context:
space:
mode:
authorFarid Zakaria <fmzakari@google.com>2022-01-06 14:40:02 -0800
committerFarid Zakaria <fmzakari@google.com>2022-01-06 14:40:02 -0800
commite36fdbbfd9812a9d0a3aadf5f7898c9092d72963 (patch)
treede91882189693d839ec157455fd2f740d8591705 /src/nix-env/nix-env.cc
parentf71d84672b6880634434f58c809c1341499e45e5 (diff)
Check for `--meta` before emitting it for `--json`
Check that the meta flag is present when emitting JSON query information for `nix-env`. fixes #5882
Diffstat (limited to 'src/nix-env/nix-env.cc')
-rw-r--r--src/nix-env/nix-env.cc28
1 files changed, 15 insertions, 13 deletions
diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc
index b2d789324..f4647f43d 100644
--- a/src/nix-env/nix-env.cc
+++ b/src/nix-env/nix-env.cc
@@ -907,7 +907,7 @@ static VersionDiff compareVersionAgainstSet(
}
-static void queryJSON(Globals & globals, vector<DrvInfo> & elems, bool printOutPath)
+static void queryJSON(Globals & globals, vector<DrvInfo> & elems, bool printOutPath, bool printMeta)
{
JSONObject topObj(cout, true);
for (auto & i : elems) {
@@ -927,17 +927,19 @@ static void queryJSON(Globals & globals, vector<DrvInfo> & elems, bool printOutP
}
}
- JSONObject metaObj = pkgObj.object("meta");
- StringSet metaNames = i.queryMetaNames();
- for (auto & j : metaNames) {
- auto placeholder = metaObj.placeholder(j);
- Value * v = i.queryMeta(j);
- if (!v) {
- printError("derivation '%s' has invalid meta attribute '%s'", i.queryName(), j);
- placeholder.write(nullptr);
- } else {
- PathSet context;
- printValueAsJSON(*globals.state, true, *v, noPos, placeholder, context);
+ if (printMeta) {
+ JSONObject metaObj = pkgObj.object("meta");
+ StringSet metaNames = i.queryMetaNames();
+ for (auto & j : metaNames) {
+ auto placeholder = metaObj.placeholder(j);
+ Value * v = i.queryMeta(j);
+ if (!v) {
+ printError("derivation '%s' has invalid meta attribute '%s'", i.queryName(), j);
+ placeholder.write(nullptr);
+ } else {
+ PathSet context;
+ printValueAsJSON(*globals.state, true, *v, noPos, placeholder, context);
+ }
}
}
}
@@ -1043,7 +1045,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs)
/* Print the desired columns, or XML output. */
if (jsonOutput) {
- queryJSON(globals, elems, printOutPath);
+ queryJSON(globals, elems, printOutPath, printMeta);
return;
}