From 6448ea84ab537600d3f350867063bc305b3bb910 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 17 Jan 2022 13:58:26 +0100 Subject: Factor out --from / --to logic --- src/libcmd/command.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++------- src/libcmd/command.hh | 17 ++++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) (limited to 'src/libcmd') diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index 429cd32cc..5e6d4a857 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -73,13 +73,16 @@ ref EvalCommand::getEvalStore() ref EvalCommand::getEvalState() { - if (!evalState) evalState = -#if HAVE_BOEHMGC - std::allocate_shared(traceable_allocator(), -#else - std::make_shared( -#endif - searchPath, getEvalStore(), getStore()); + if (!evalState) + evalState = + #if HAVE_BOEHMGC + std::allocate_shared(traceable_allocator(), + searchPath, getEvalStore(), getStore()) + #else + std::make_shared( + searchPath, getEvalStore(), getStore()) + #endif + ; return ref(evalState); } @@ -156,6 +159,43 @@ void StorePathsCommand::run(ref store, BuiltPaths && paths) run(store, std::move(sorted)); } +CopyCommand::CopyCommand() + : BuiltPathsCommand(true) +{ + addFlag({ + .longName = "from", + .description = "URL of the source Nix store.", + .labels = {"store-uri"}, + .handler = {&srcUri}, + }); + + addFlag({ + .longName = "to", + .description = "URL of the destination Nix store.", + .labels = {"store-uri"}, + .handler = {&dstUri}, + }); +} + +ref CopyCommand::createStore() +{ + return srcUri.empty() ? StoreCommand::createStore() : openStore(srcUri); +} + +void CopyCommand::run(ref store) +{ + if (srcUri.empty() && dstUri.empty()) + throw UsageError("you must pass '--from' and/or '--to'"); + + BuiltPathsCommand::run(store); +} + +void CopyCommand::run(ref srcStore, BuiltPaths && paths) +{ + ref dstStore = dstUri.empty() ? openStore() : openStore(dstUri); + run(srcStore, dstStore, std::move(paths)); +} + void StorePathCommand::run(ref store, std::vector && storePaths) { if (storePaths.size() != 1) diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh index 07f398468..0c3e29e25 100644 --- a/src/libcmd/command.hh +++ b/src/libcmd/command.hh @@ -176,6 +176,23 @@ public: bool useDefaultInstallables() override { return !all; } }; +/* A command that copies something between `--from` and `--to` + stores. */ +struct CopyCommand : virtual BuiltPathsCommand +{ + std::string srcUri, dstUri; + + CopyCommand(); + + ref createStore() override; + + void run(ref store) override; + + void run(ref srcStore, BuiltPaths && paths) override; + + virtual void run(ref srcStore, ref dstStore, BuiltPaths && paths) = 0; +}; + struct StorePathsCommand : public BuiltPathsCommand { StorePathsCommand(bool recursive = false); -- cgit v1.2.3