Further HashType renaming + using mkHashAlgoOptFlag for new conversion

https://github.com/NixOS/nix/issues/8876
This commit is contained in:
Peter Kolloch 2023-11-28 14:41:44 +01:00
parent 5334c9c792
commit 837b889c41
4 changed files with 27 additions and 34 deletions

View file

@ -544,14 +544,14 @@ nlohmann::json Args::toJSON()
return res; return res;
} }
static void hashTypeCompleter(AddCompletions & completions, size_t index, std::string_view prefix) static void hashAlgoCompleter(AddCompletions & completions, size_t index, std::string_view prefix)
{ {
for (auto & type : hashAlgorithms) for (auto & type : hashAlgorithms)
if (hasPrefix(type, prefix)) if (hasPrefix(type, prefix))
completions.add(type); completions.add(type);
} }
Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashAlgorithm * ha) Args::Flag Args::Flag::mkHashAlgoFlag(std::string && longName, HashAlgorithm * ha)
{ {
return Flag{ return Flag{
.longName = std::move(longName), .longName = std::move(longName),
@ -560,11 +560,11 @@ Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashAlgorithm * h
.handler = {[ha](std::string s) { .handler = {[ha](std::string s) {
*ha = parseHashAlgo(s); *ha = parseHashAlgo(s);
}}, }},
.completer = hashTypeCompleter, .completer = hashAlgoCompleter,
}; };
} }
Args::Flag Args::Flag::mkHashTypeOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha) Args::Flag Args::Flag::mkHashAlgoOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha)
{ {
return Flag{ return Flag{
.longName = std::move(longName), .longName = std::move(longName),
@ -573,7 +573,7 @@ Args::Flag Args::Flag::mkHashTypeOptFlag(std::string && longName, std::optional<
.handler = {[oha](std::string s) { .handler = {[oha](std::string s) {
*oha = std::optional<HashAlgorithm>{parseHashAlgo(s)}; *oha = std::optional<HashAlgorithm>{parseHashAlgo(s)};
}}, }},
.completer = hashTypeCompleter, .completer = hashAlgoCompleter,
}; };
} }

View file

@ -175,8 +175,8 @@ protected:
std::optional<ExperimentalFeature> experimentalFeature; std::optional<ExperimentalFeature> experimentalFeature;
static Flag mkHashTypeFlag(std::string && longName, HashAlgorithm * ha); static Flag mkHashAlgoFlag(std::string && longName, HashAlgorithm * ha);
static Flag mkHashTypeOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha); static Flag mkHashAlgoOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha);
}; };
/** /**

View file

@ -48,7 +48,7 @@ struct CmdHashBase : Command
.handler = {&hashFormat, HashFormat::Base16}, .handler = {&hashFormat, HashFormat::Base16},
}); });
addFlag(Flag::mkHashTypeFlag("type", &ha)); addFlag(Flag::mkHashAlgoFlag("type", &ha));
#if 0 #if 0
addFlag({ addFlag({
@ -112,7 +112,7 @@ struct CmdToBase : Command
CmdToBase(HashFormat hashFormat) : hashFormat(hashFormat) CmdToBase(HashFormat hashFormat) : hashFormat(hashFormat)
{ {
addFlag(Flag::mkHashTypeOptFlag("type", &ht)); addFlag(Flag::mkHashAlgoOptFlag("type", &ht));
expectArgs("strings", &args); expectArgs("strings", &args);
} }
@ -139,7 +139,7 @@ struct CmdHashConvert : Command
{ {
std::optional<HashFormat> from; std::optional<HashFormat> from;
HashFormat to; HashFormat to;
std::optional<HashAlgorithm> type; std::optional<HashAlgorithm> algo;
std::vector<std::string> hashStrings; std::vector<std::string> hashStrings;
CmdHashConvert(): to(HashFormat::SRI) { CmdHashConvert(): to(HashFormat::SRI) {
@ -161,14 +161,7 @@ struct CmdHashConvert : Command
to = parseHashFormat(str); to = parseHashFormat(str);
}}, }},
}); });
addFlag({ addFlag(Args::Flag::mkHashAlgoOptFlag("algo", &algo));
.longName = "algo",
.description = "Specify the algorithm if it can't be auto-detected.",
.labels = {"hash algorithm"},
.handler = {[this](std::string str) {
type = parseHashAlgo(str);
}},
});
expectArgs({ expectArgs({
.label = "hashes", .label = "hashes",
.handler = {&hashStrings}, .handler = {&hashStrings},
@ -184,7 +177,7 @@ struct CmdHashConvert : Command
void run() override { void run() override {
for (const auto& s: hashStrings) { for (const auto& s: hashStrings) {
Hash h = Hash::parseAny(s, type); Hash h = Hash::parseAny(s, algo);
if (from && h.to_string(*from, from == HashFormat::SRI) != s) { if (from && h.to_string(*from, from == HashFormat::SRI) != s) {
auto from_as_string = printHashFormat(*from); auto from_as_string = printHashFormat(*from);
throw BadHash("input hash '%s' does not have the expected format '--from %s'", s, from_as_string); throw BadHash("input hash '%s' does not have the expected format '--from %s'", s, from_as_string);

View file

@ -279,7 +279,7 @@ struct CmdStorePrefetchFile : StoreCommand, MixJSON
}} }}
}); });
addFlag(Flag::mkHashTypeFlag("hash-type", &hashAlgo)); addFlag(Flag::mkHashAlgoFlag("hash-type", &hashAlgo));
addFlag({ addFlag({
.longName = "executable", .longName = "executable",