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,36 +544,36 @@ nlohmann::json Args::toJSON()
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)
if (hasPrefix(type, prefix))
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),
.description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')",
.labels = {"hash-algo"},
.handler = {[ha](std::string 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),
.description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512'). Optional as can also be gotten from SRI hash itself.",
.labels = {"hash-algo"},
.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;
static Flag mkHashTypeFlag(std::string && longName, HashAlgorithm * ha);
static Flag mkHashTypeOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha);
static Flag mkHashAlgoFlag(std::string && longName, HashAlgorithm * ha);
static Flag mkHashAlgoOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha);
};
/**

View File

@ -48,7 +48,7 @@ struct CmdHashBase : Command
.handler = {&hashFormat, HashFormat::Base16},
});
addFlag(Flag::mkHashTypeFlag("type", &ha));
addFlag(Flag::mkHashAlgoFlag("type", &ha));
#if 0
addFlag({
@ -112,7 +112,7 @@ struct CmdToBase : Command
CmdToBase(HashFormat hashFormat) : hashFormat(hashFormat)
{
addFlag(Flag::mkHashTypeOptFlag("type", &ht));
addFlag(Flag::mkHashAlgoOptFlag("type", &ht));
expectArgs("strings", &args);
}
@ -139,7 +139,7 @@ struct CmdHashConvert : Command
{
std::optional<HashFormat> from;
HashFormat to;
std::optional<HashAlgorithm> type;
std::optional<HashAlgorithm> algo;
std::vector<std::string> hashStrings;
CmdHashConvert(): to(HashFormat::SRI) {
@ -161,14 +161,7 @@ struct CmdHashConvert : Command
to = parseHashFormat(str);
}},
});
addFlag({
.longName = "algo",
.description = "Specify the algorithm if it can't be auto-detected.",
.labels = {"hash algorithm"},
.handler = {[this](std::string str) {
type = parseHashAlgo(str);
}},
});
addFlag(Args::Flag::mkHashAlgoOptFlag("algo", &algo));
expectArgs({
.label = "hashes",
.handler = {&hashStrings},
@ -184,7 +177,7 @@ struct CmdHashConvert : Command
void run() override {
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) {
auto from_as_string = printHashFormat(*from);
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({
.longName = "executable",