1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-24 11:19:35 +01:00

Add aliases for the old flake commands

Requires reworking a bit the existing alias mechanism to allow aliases
to be composite commands themselves
This commit is contained in:
regnat 2021-07-06 15:54:56 +02:00
parent 401b09b7fe
commit cc711c6956

View file

@ -106,26 +106,28 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
}); });
} }
std::map<std::string, std::vector<std::string>> aliases = { std::map<std::vector<std::string>, std::vector<std::string>> aliases = {
{"add-to-store", {"store", "add-path"}}, {{"add-to-store"}, {"store", "add-path"}},
{"cat-nar", {"nar", "cat"}}, {{"cat-nar"}, {"nar", "cat"}},
{"cat-store", {"store", "cat"}}, {{"cat-store"}, {"store", "cat"}},
{"copy-sigs", {"store", "copy-sigs"}}, {{"copy-sigs"}, {"store", "copy-sigs"}},
{"dev-shell", {"develop"}}, {{"dev-shell"}, {"develop"}},
{"diff-closures", {"store", "diff-closures"}}, {{"diff-closures"}, {"store", "diff-closures"}},
{"dump-path", {"store", "dump-path"}}, {{"dump-path"}, {"store", "dump-path"}},
{"hash-file", {"hash", "file"}}, {{"hash-file"}, {"hash", "file"}},
{"hash-path", {"hash", "path"}}, {{"hash-path"}, {"hash", "path"}},
{"ls-nar", {"nar", "ls"}}, {{"ls-nar"}, {"nar", "ls"}},
{"ls-store", {"store", "ls"}}, {{"ls-store"}, {"store", "ls"}},
{"make-content-addressable", {"store", "make-content-addressable"}}, {{"make-content-addressable"}, {"store", "make-content-addressable"}},
{"optimise-store", {"store", "optimise"}}, {{"optimise-store"}, {"store", "optimise"}},
{"ping-store", {"store", "ping"}}, {{"ping-store"}, {"store", "ping"}},
{"sign-paths", {"store", "sign"}}, {{"sign-paths"}, {"store", "sign"}},
{"to-base16", {"hash", "to-base16"}}, {{"to-base16"}, {"hash", "to-base16"}},
{"to-base32", {"hash", "to-base32"}}, {{"to-base32"}, {"hash", "to-base32"}},
{"to-base64", {"hash", "to-base64"}}, {{"to-base64"}, {"hash", "to-base64"}},
{"verify", {"store", "verify"}}, {{"verify"}, {"store", "verify"}},
{{"flake", "init"}, {"init"}},
{{"flake", "new"}, {"new"}},
}; };
bool aliasUsed = false; bool aliasUsed = false;
@ -134,14 +136,33 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
{ {
if (aliasUsed || command || pos == args.end()) return pos; if (aliasUsed || command || pos == args.end()) return pos;
auto arg = *pos; auto arg = *pos;
auto i = aliases.find(arg);
if (i == aliases.end()) return pos; // Loop through the aliases to see whether the current cli corresponds
warn("'%s' is a deprecated alias for '%s'", // to one of them.
arg, concatStringsSep(" ", i->second)); for (auto & [from, to] : aliases) {
pos = args.erase(pos); auto i = pos;
for (auto j = i->second.rbegin(); j != i->second.rend(); ++j) bool isCurrentAlias = true;
pos = args.insert(pos, *j); // Is the current alias a prefix of the args?
aliasUsed = true; for (auto & fromItem : from) {
if (i == args.end() || *i != fromItem) {
// The current alias doesnt match the args
isCurrentAlias = false;
break;
}
i++;
}
// If we went through to the end of the previous loop, then we match
// the currently considered alias.
// So rewrite the alias in the current args.
if (isCurrentAlias) {
warn("'%s' is a deprecated alias for '%s'",
concatStringsSep(" ", from), concatStringsSep(" ", to));
pos = args.erase(pos, i);
pos = args.insert(pos, to.begin(), to.end());
aliasUsed = true;
return pos;
}
}
return pos; return pos;
} }