|
1 | 1 | #include "nix/cmd/command.hh" |
2 | 2 | #include "nix/store/store-api.hh" |
| 3 | +#include "nix/expr/provenance.hh" |
3 | 4 | #include "nix/store/provenance.hh" |
4 | 5 | #include "nix/flake/provenance.hh" |
5 | 6 | #include "nix/fetchers/provenance.hh" |
6 | 7 | #include "nix/util/provenance.hh" |
| 8 | +#include "nix/util/json-utils.hh" |
7 | 9 |
|
8 | 10 | #include <memory> |
9 | 11 | #include <nlohmann/json.hpp> |
@@ -92,6 +94,60 @@ struct CmdProvenanceShow : StorePathsCommand |
92 | 94 | } else if (auto subpath = std::dynamic_pointer_cast<const SubpathProvenance>(provenance)) { |
93 | 95 | logger->cout("← from file " ANSI_BOLD "%s" ANSI_NORMAL, subpath->subpath.abs()); |
94 | 96 | provenance = subpath->next; |
| 97 | + } else if (auto drv = std::dynamic_pointer_cast<const DerivationProvenance>(provenance)) { |
| 98 | + logger->cout("← with derivation metadata"); |
| 99 | +#define TAB " " |
| 100 | + auto json = getObject(*(drv->meta)); |
| 101 | + if (auto identifiers = optionalValueAt(json, "identifiers")) { |
| 102 | + auto ident = getObject(*identifiers); |
| 103 | + if (auto cpeParts = optionalValueAt(ident, "cpeParts")) { |
| 104 | + auto parts = getObject(*cpeParts); |
| 105 | + |
| 106 | + auto vendor = parts["vendor"]; |
| 107 | + auto product = parts["product"]; |
| 108 | + auto version = parts["version"]; |
| 109 | + auto update = parts["update"]; |
| 110 | + |
| 111 | + logger->cout( |
| 112 | + TAB "" ANSI_BOLD "CPE:" ANSI_NORMAL " cpe:2.3:a:%s:%s:%s:%s:*:*:*:*:*:*", |
| 113 | + vendor.is_null() ? "*" : vendor.get<std::string>(), |
| 114 | + product.is_null() ? "*" : product.get<std::string>(), |
| 115 | + version.is_null() ? "*" : version.get<std::string>(), |
| 116 | + update.is_null() ? "*" : update.get<std::string>()); |
| 117 | + } |
| 118 | + } |
| 119 | + if (auto license = optionalValueAt(json, "license")) { |
| 120 | + if (license->is_array()) { |
| 121 | + logger->cout(TAB "" ANSI_BOLD "Licenses:" ANSI_NORMAL); |
| 122 | + auto licenses = getArray(*license); |
| 123 | + for (auto it = licenses.begin(); it != licenses.end(); it++) { |
| 124 | + auto license = getObject(*it); |
| 125 | + auto shortName = license["shortName"]; |
| 126 | + logger->cout(TAB "" TAB "- %s", shortName.get<std::string>()); |
| 127 | + } |
| 128 | + } else { |
| 129 | + auto obj = getObject(*license); |
| 130 | + auto shortName = obj["shortName"]; |
| 131 | + logger->cout(TAB "" ANSI_BOLD "License:" ANSI_NORMAL " %s", shortName.get<std::string>()); |
| 132 | + } |
| 133 | + } |
| 134 | + if (auto licenses = optionalValueAt(json, "licenses")) { |
| 135 | + if (licenses->is_array()) { |
| 136 | + logger->cout(TAB "" ANSI_BOLD "Licenses:" ANSI_NORMAL); |
| 137 | + auto licensesArray = getArray(*licenses); |
| 138 | + for (auto it = licensesArray.begin(); it != licensesArray.end(); it++) { |
| 139 | + auto license = getObject(*it); |
| 140 | + auto shortName = license["shortName"]; |
| 141 | + logger->cout(TAB "" TAB "- %s", shortName.get<std::string>()); |
| 142 | + } |
| 143 | + } else { |
| 144 | + auto license = getObject(*licenses); |
| 145 | + auto shortName = license["shortName"]; |
| 146 | + logger->cout(TAB "" ANSI_BOLD "License:" ANSI_NORMAL " %s", shortName.get<std::string>()); |
| 147 | + } |
| 148 | + } |
| 149 | +#undef TAB |
| 150 | + provenance = drv->next; |
95 | 151 | } else { |
96 | 152 | // Unknown or unhandled provenance type |
97 | 153 | auto json = provenance->to_json(); |
|
0 commit comments