aboutsummaryrefslogtreecommitdiff
path: root/src/libcmd
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-01-17 13:58:26 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-01-18 14:08:49 +0100
commit6448ea84ab537600d3f350867063bc305b3bb910 (patch)
tree88b6bd50459b3d0f617d32855c0a757716f39b43 /src/libcmd
parentf6f0bcf11ff79213f5ffa58d0e490901b2d3ce58 (diff)
Factor out --from / --to logic
Diffstat (limited to 'src/libcmd')
-rw-r--r--src/libcmd/command.cc54
-rw-r--r--src/libcmd/command.hh17
2 files changed, 64 insertions, 7 deletions
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<Store> EvalCommand::getEvalStore()
ref<EvalState> EvalCommand::getEvalState()
{
- if (!evalState) evalState =
-#if HAVE_BOEHMGC
- std::allocate_shared<EvalState>(traceable_allocator<EvalState>(),
-#else
- std::make_shared<EvalState>(
-#endif
- searchPath, getEvalStore(), getStore());
+ if (!evalState)
+ evalState =
+ #if HAVE_BOEHMGC
+ std::allocate_shared<EvalState>(traceable_allocator<EvalState>(),
+ searchPath, getEvalStore(), getStore())
+ #else
+ std::make_shared<EvalState>(
+ searchPath, getEvalStore(), getStore())
+ #endif
+ ;
return ref<EvalState>(evalState);
}
@@ -156,6 +159,43 @@ void StorePathsCommand::run(ref<Store> 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<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)
{
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<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
{
StorePathsCommand(bool recursive = false);