Factor out --from / --to logic

This commit is contained in:
Eelco Dolstra 2022-01-17 13:58:26 +01:00
parent f6f0bcf11f
commit 6448ea84ab
3 changed files with 66 additions and 44 deletions

View file

@ -73,13 +73,16 @@ ref<Store> EvalCommand::getEvalStore()
ref<EvalState> EvalCommand::getEvalState() ref<EvalState> EvalCommand::getEvalState()
{ {
if (!evalState) evalState = if (!evalState)
#if HAVE_BOEHMGC evalState =
#if HAVE_BOEHMGC
std::allocate_shared<EvalState>(traceable_allocator<EvalState>(), std::allocate_shared<EvalState>(traceable_allocator<EvalState>(),
#else searchPath, getEvalStore(), getStore())
#else
std::make_shared<EvalState>( std::make_shared<EvalState>(
#endif searchPath, getEvalStore(), getStore())
searchPath, getEvalStore(), getStore()); #endif
;
return ref<EvalState>(evalState); return ref<EvalState>(evalState);
} }
@ -156,6 +159,43 @@ void StorePathsCommand::run(ref<Store> store, BuiltPaths && paths)
run(store, std::move(sorted)); 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<Store> CopyCommand::createStore()
{
return srcUri.empty() ? StoreCommand::createStore() : openStore(srcUri);
}
void CopyCommand::run(ref<Store> store)
{
if (srcUri.empty() && dstUri.empty())
throw UsageError("you must pass '--from' and/or '--to'");
BuiltPathsCommand::run(store);
}
void CopyCommand::run(ref<Store> srcStore, BuiltPaths && paths)
{
ref<Store> dstStore = dstUri.empty() ? openStore() : openStore(dstUri);
run(srcStore, dstStore, std::move(paths));
}
void StorePathCommand::run(ref<Store> store, std::vector<StorePath> && storePaths) void StorePathCommand::run(ref<Store> store, std::vector<StorePath> && storePaths)
{ {
if (storePaths.size() != 1) if (storePaths.size() != 1)

View file

@ -176,6 +176,23 @@ public:
bool useDefaultInstallables() override { return !all; } 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<Store> createStore() override;
void run(ref<Store> store) override;
void run(ref<Store> srcStore, BuiltPaths && paths) override;
virtual void run(ref<Store> srcStore, ref<Store> dstStore, BuiltPaths && paths) = 0;
};
struct StorePathsCommand : public BuiltPathsCommand struct StorePathsCommand : public BuiltPathsCommand
{ {
StorePathsCommand(bool recursive = false); StorePathsCommand(bool recursive = false);

View file

@ -1,17 +1,11 @@
#include "command.hh" #include "command.hh"
#include "shared.hh" #include "shared.hh"
#include "store-api.hh" #include "store-api.hh"
#include "sync.hh"
#include "thread-pool.hh"
#include <atomic>
using namespace nix; using namespace nix;
struct CmdCopy : BuiltPathsCommand struct CmdCopy : CopyCommand
{ {
std::string srcUri, dstUri;
CheckSigsFlag checkSigs = CheckSigs; CheckSigsFlag checkSigs = CheckSigs;
SubstituteFlag substitute = NoSubstitute; SubstituteFlag substitute = NoSubstitute;
@ -21,20 +15,6 @@ struct CmdCopy : BuiltPathsCommand
CmdCopy() CmdCopy()
: BuiltPathsCommand(true) : 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},
});
addFlag({ addFlag({
.longName = "no-check-sigs", .longName = "no-check-sigs",
.description = "Do not require that paths are signed by trusted keys.", .description = "Do not require that paths are signed by trusted keys.",
@ -65,23 +45,8 @@ struct CmdCopy : BuiltPathsCommand
Category category() override { return catSecondary; } Category category() override { return catSecondary; }
ref<Store> createStore() override void run(ref<Store> srcStore, ref<Store> dstStore, BuiltPaths && paths) override
{ {
return srcUri.empty() ? StoreCommand::createStore() : openStore(srcUri);
}
void run(ref<Store> store) override
{
if (srcUri.empty() && dstUri.empty())
throw UsageError("you must pass '--from' and/or '--to'");
BuiltPathsCommand::run(store);
}
void run(ref<Store> srcStore, BuiltPaths && paths) override
{
ref<Store> dstStore = dstUri.empty() ? openStore() : openStore(dstUri);
RealisedPath::Set stuffToCopy; RealisedPath::Set stuffToCopy;
for (auto & builtPath : paths) { for (auto & builtPath : paths) {