Skip to content

Commit 7156737

Browse files
authored
Merge pull request #13008 from Mic92/aliases
Move alias support from NixArgs to MultiCommand + test
2 parents 837f349 + b1b75e1 commit 7156737

File tree

7 files changed

+78
-73
lines changed

7 files changed

+78
-73
lines changed

doc/manual/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ nix3_manpages = [
281281
'nix3-store',
282282
'nix3-store-optimise',
283283
'nix3-store-path-from-hash-part',
284-
'nix3-store-ping',
285284
'nix3-store-prefetch-file',
286285
'nix3-store-repair',
287286
'nix3-store-sign',

src/libutil/args.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,25 @@ nlohmann::json MultiCommand::toJSON()
647647
return res;
648648
}
649649

650+
Strings::iterator MultiCommand::rewriteArgs(Strings & args, Strings::iterator pos)
651+
{
652+
if (command)
653+
return command->second->rewriteArgs(args, pos);
654+
655+
if (aliasUsed || pos == args.end()) return pos;
656+
auto arg = *pos;
657+
auto i = aliases.find(arg);
658+
if (i == aliases.end()) return pos;
659+
auto & info = i->second;
660+
if (info.status == AliasStatus::Deprecated) {
661+
warn("'%s' is a deprecated alias for '%s'",
662+
arg, concatStringsSep(" ", info.replacement));
663+
}
664+
pos = args.erase(pos);
665+
for (auto j = info.replacement.rbegin(); j != info.replacement.rend(); ++j)
666+
pos = args.insert(pos, *j);
667+
aliasUsed = true;
668+
return pos;
669+
}
670+
650671
}

src/libutil/include/nix/util/args.hh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,30 @@ public:
393393

394394
nlohmann::json toJSON() override;
395395

396+
enum struct AliasStatus {
397+
/** Aliases that don't go away */
398+
AcceptedShorthand,
399+
/** Aliases that will go away */
400+
Deprecated,
401+
};
402+
403+
/** An alias, except for the original syntax, which is in the map key. */
404+
struct AliasInfo {
405+
AliasStatus status;
406+
std::vector<std::string> replacement;
407+
};
408+
409+
/**
410+
* A list of aliases (remapping a deprecated/shorthand subcommand
411+
* to something else).
412+
*/
413+
std::map<std::string, AliasInfo> aliases;
414+
415+
Strings::iterator rewriteArgs(Strings & args, Strings::iterator pos) override;
416+
396417
protected:
397418
std::string commandName = "";
419+
bool aliasUsed = false;
398420
};
399421

400422
Strings argvToStrings(int argc, char * * argv);

src/nix/main.cc

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,6 @@ void chrootHelper(int argc, char * * argv);
5050

5151
namespace nix {
5252

53-
enum struct AliasStatus {
54-
/** Aliases that don't go away */
55-
AcceptedShorthand,
56-
/** Aliases that will go away */
57-
Deprecated,
58-
};
59-
60-
/** An alias, except for the original syntax, which is in the map key. */
61-
struct AliasInfo {
62-
AliasStatus status;
63-
std::vector<std::string> replacement;
64-
};
65-
6653
/* Check if we have a non-loopback/link-local network interface. */
6754
static bool haveInternet()
6855
{
@@ -153,54 +140,34 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs, virtual RootArgs
153140
.handler = {[&]() { refresh = true; }},
154141
.experimentalFeature = Xp::NixCommand,
155142
});
156-
}
157143

158-
std::map<std::string, AliasInfo> aliases = {
159-
{"add-to-store", { AliasStatus::Deprecated, {"store", "add-path"}}},
160-
{"cat-nar", { AliasStatus::Deprecated, {"nar", "cat"}}},
161-
{"cat-store", { AliasStatus::Deprecated, {"store", "cat"}}},
162-
{"copy-sigs", { AliasStatus::Deprecated, {"store", "copy-sigs"}}},
163-
{"dev-shell", { AliasStatus::Deprecated, {"develop"}}},
164-
{"diff-closures", { AliasStatus::Deprecated, {"store", "diff-closures"}}},
165-
{"dump-path", { AliasStatus::Deprecated, {"store", "dump-path"}}},
166-
{"hash-file", { AliasStatus::Deprecated, {"hash", "file"}}},
167-
{"hash-path", { AliasStatus::Deprecated, {"hash", "path"}}},
168-
{"ls-nar", { AliasStatus::Deprecated, {"nar", "ls"}}},
169-
{"ls-store", { AliasStatus::Deprecated, {"store", "ls"}}},
170-
{"make-content-addressable", { AliasStatus::Deprecated, {"store", "make-content-addressed"}}},
171-
{"optimise-store", { AliasStatus::Deprecated, {"store", "optimise"}}},
172-
{"ping-store", { AliasStatus::Deprecated, {"store", "info"}}},
173-
{"sign-paths", { AliasStatus::Deprecated, {"store", "sign"}}},
174-
{"shell", { AliasStatus::AcceptedShorthand, {"env", "shell"}}},
175-
{"show-derivation", { AliasStatus::Deprecated, {"derivation", "show"}}},
176-
{"show-config", { AliasStatus::Deprecated, {"config", "show"}}},
177-
{"to-base16", { AliasStatus::Deprecated, {"hash", "to-base16"}}},
178-
{"to-base32", { AliasStatus::Deprecated, {"hash", "to-base32"}}},
179-
{"to-base64", { AliasStatus::Deprecated, {"hash", "to-base64"}}},
180-
{"verify", { AliasStatus::Deprecated, {"store", "verify"}}},
181-
{"doctor", { AliasStatus::Deprecated, {"config", "check"}}},
144+
aliases = {
145+
{"add-to-store", { AliasStatus::Deprecated, {"store", "add-path"}}},
146+
{"cat-nar", { AliasStatus::Deprecated, {"nar", "cat"}}},
147+
{"cat-store", { AliasStatus::Deprecated, {"store", "cat"}}},
148+
{"copy-sigs", { AliasStatus::Deprecated, {"store", "copy-sigs"}}},
149+
{"dev-shell", { AliasStatus::Deprecated, {"develop"}}},
150+
{"diff-closures", { AliasStatus::Deprecated, {"store", "diff-closures"}}},
151+
{"dump-path", { AliasStatus::Deprecated, {"store", "dump-path"}}},
152+
{"hash-file", { AliasStatus::Deprecated, {"hash", "file"}}},
153+
{"hash-path", { AliasStatus::Deprecated, {"hash", "path"}}},
154+
{"ls-nar", { AliasStatus::Deprecated, {"nar", "ls"}}},
155+
{"ls-store", { AliasStatus::Deprecated, {"store", "ls"}}},
156+
{"make-content-addressable", { AliasStatus::Deprecated, {"store", "make-content-addressed"}}},
157+
{"optimise-store", { AliasStatus::Deprecated, {"store", "optimise"}}},
158+
{"ping-store", { AliasStatus::Deprecated, {"store", "info"}}},
159+
{"sign-paths", { AliasStatus::Deprecated, {"store", "sign"}}},
160+
{"shell", { AliasStatus::AcceptedShorthand, {"env", "shell"}}},
161+
{"show-derivation", { AliasStatus::Deprecated, {"derivation", "show"}}},
162+
{"show-config", { AliasStatus::Deprecated, {"config", "show"}}},
163+
{"to-base16", { AliasStatus::Deprecated, {"hash", "to-base16"}}},
164+
{"to-base32", { AliasStatus::Deprecated, {"hash", "to-base32"}}},
165+
{"to-base64", { AliasStatus::Deprecated, {"hash", "to-base64"}}},
166+
{"verify", { AliasStatus::Deprecated, {"store", "verify"}}},
167+
{"doctor", { AliasStatus::Deprecated, {"config", "check"}}},
168+
};
182169
};
183170

184-
bool aliasUsed = false;
185-
186-
Strings::iterator rewriteArgs(Strings & args, Strings::iterator pos) override
187-
{
188-
if (aliasUsed || command || pos == args.end()) return pos;
189-
auto arg = *pos;
190-
auto i = aliases.find(arg);
191-
if (i == aliases.end()) return pos;
192-
auto & info = i->second;
193-
if (info.status == AliasStatus::Deprecated) {
194-
warn("'%s' is a deprecated alias for '%s'",
195-
arg, concatStringsSep(" ", info.replacement));
196-
}
197-
pos = args.erase(pos);
198-
for (auto j = info.replacement.rbegin(); j != info.replacement.rend(); ++j)
199-
pos = args.insert(pos, *j);
200-
aliasUsed = true;
201-
return pos;
202-
}
203-
204171
std::string description() override
205172
{
206173
return "a tool for reproducible and declarative configuration management";

src/nix/store-info.cc

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
using namespace nix;
99

10-
struct CmdPingStore : StoreCommand, MixJSON
10+
struct CmdInfoStore : StoreCommand, MixJSON
1111
{
1212
std::string description() override
1313
{
@@ -46,15 +46,4 @@ struct CmdPingStore : StoreCommand, MixJSON
4646
}
4747
};
4848

49-
struct CmdInfoStore : CmdPingStore
50-
{
51-
void run(nix::ref<nix::Store> store) override
52-
{
53-
warn("'nix store ping' is a deprecated alias for 'nix store info'");
54-
CmdPingStore::run(store);
55-
}
56-
};
57-
58-
59-
static auto rCmdPingStore = registerCommand2<CmdPingStore>({"store", "info"});
60-
static auto rCmdInfoStore = registerCommand2<CmdInfoStore>({"store", "ping"});
49+
static auto rCmdInfoStore = registerCommand2<CmdInfoStore>({"store", "info"});

src/nix/store.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ using namespace nix;
55
struct CmdStore : NixMultiCommand
66
{
77
CmdStore() : NixMultiCommand("store", RegisterCommand::getCommandsFor({"store"}))
8-
{ }
8+
{
9+
aliases = {
10+
{"ping", { AliasStatus::Deprecated, {"info"}}},
11+
};
12+
}
913

1014
std::string description() override
1115
{

tests/functional/store-info.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
source common.sh
44

55
STORE_INFO=$(nix store info 2>&1)
6+
LEGACY_STORE_INFO=$(nix store ping 2>&1) # alias to nix store info
67
STORE_INFO_JSON=$(nix store info --json)
78

89
echo "$STORE_INFO" | grep "Store URL: ${NIX_REMOTE}"
10+
echo "$LEGACY_STORE_INFO" | grep "Store URL: ${NIX_REMOTE}"
911

1012
if [[ -v NIX_DAEMON_PACKAGE ]] && isDaemonNewer "2.7.0pre20220126"; then
1113
DAEMON_VERSION=$("$NIX_DAEMON_PACKAGE"/bin/nix daemon --version | cut -d' ' -f3)
1214
echo "$STORE_INFO" | grep "Version: $DAEMON_VERSION"
1315
[[ "$(echo "$STORE_INFO_JSON" | jq -r ".version")" == "$DAEMON_VERSION" ]]
1416
fi
1517

18+
1619
expect 127 NIX_REMOTE=unix:"$PWD"/store nix store info || \
1720
fail "nix store info on a non-existent store should fail"
1821

0 commit comments

Comments
 (0)