libcmd/copy: Check the validity of the options if required

This is done at the start of the command call, which allows not building
expensive things if the arguments are incorrect anyway.

Fixes #687

Change-Id: Ic924fe2115cf760684c6fdf7987279e96ab00286
This commit is contained in:
Tom Hubrecht
2025-11-19 14:45:52 +00:00
parent 1fa8df82df
commit 04a4b15991
3 changed files with 13 additions and 4 deletions
+9 -3
View File
@@ -75,6 +75,15 @@ CopyCommand::CopyCommand()
});
}
void CopyCommand::run()
{
if (requireStore && srcUri.empty() && dstUri.empty()) {
throw UsageError("you must pass '--from' and/or '--to'");
}
StoreCommand::run();
}
ref<Store> CopyCommand::createStore(AsyncIoRoot & in)
{
return srcUri.empty() ? StoreCommand::createStore(in) : in.blockOn(openStore(srcUri));
@@ -82,9 +91,6 @@ ref<Store> CopyCommand::createStore(AsyncIoRoot & in)
ref<Store> CopyCommand::getDstStore()
{
if (srcUri.empty() && dstUri.empty())
throw UsageError("you must pass '--from' and/or '--to'");
return aio().blockOn(dstUri.empty() ? openStore() : openStore(dstUri));
}
+2
View File
@@ -56,9 +56,11 @@ private:
struct CopyCommand : virtual StoreCommand
{
std::string srcUri, dstUri;
bool requireStore = true;
CopyCommand();
void run() override;
ref<Store> createStore(AsyncIoRoot & in) override;
ref<Store> getDstStore();
+2 -1
View File
@@ -12,6 +12,7 @@ struct CmdMakeContentAddressed : virtual CopyCommand, virtual StorePathsCommand,
CmdMakeContentAddressed()
{
realiseMode = Realise::Outputs;
requireStore = false;
}
std::string description() override
@@ -28,7 +29,7 @@ struct CmdMakeContentAddressed : virtual CopyCommand, virtual StorePathsCommand,
void run(ref<Store> srcStore, StorePaths && storePaths) override
{
auto dstStore = aio().blockOn(dstUri.empty() ? openStore() : openStore(dstUri));
auto dstStore = getDstStore();
auto remappings = aio().blockOn(makeContentAddressed(*srcStore, *dstStore,
StorePathSet(storePaths.begin(), storePaths.end())));