mirror of
https://github.com/cmclark00/retro-imager.git
synced 2025-05-18 07:55:21 +01:00
cli: Add disable-eject, rework options specification
This commit is contained in:
parent
d825c22fb9
commit
215099e106
1 changed files with 29 additions and 37 deletions
66
src/cli.cpp
66
src/cli.cpp
|
@ -49,27 +49,19 @@ Cli::~Cli()
|
||||||
int Cli::main()
|
int Cli::main()
|
||||||
{
|
{
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
QCommandLineOption cli("cli");
|
parser.addOptions({
|
||||||
parser.addOption(cli);
|
{"cli", ""},
|
||||||
QCommandLineOption disableVerify("disable-verify", "Disable verification");
|
{"disable-verify", "Disable verification"},
|
||||||
parser.addOption(disableVerify);
|
{"enable-writing-system-drives", "Only use this if you know what you are doing"},
|
||||||
QCommandLineOption writeSystemDrive("enable-writing-system-drives", "Only use this if you know what you are doing");
|
{"sha256", "Expected hash", "sha256", ""},
|
||||||
parser.addOption(writeSystemDrive);
|
{"cache-file", "Custom cache file (requires setting sha256 as well)", "cache-file", ""},
|
||||||
QCommandLineOption sha256Option("sha256", "Expected hash", "sha256", "");
|
{"first-run-script", "Add firstrun.sh to image", "first-run-script", ""},
|
||||||
parser.addOption(sha256Option);
|
{"cloudinit-userdata", "Add cloud-init user-data file to image", "cloudinit-userdata", ""},
|
||||||
QCommandLineOption cacheFileOption("cache-file", "Custom cache file (requires setting sha256 as well)", "cache-file", "");
|
{"cloudinit-networkconfig", "Add cloud-init network-config file to image", "cloudinit-networkconfig", ""},
|
||||||
parser.addOption(cacheFileOption);
|
{"disable-eject", "Disable automatic ejection of storage media after verification"},
|
||||||
QCommandLineOption firstRunScriptOption("first-run-script", "Add firstrun.sh to image", "first-run-script", "");
|
{"debug", "Output debug messages to console"},
|
||||||
parser.addOption(firstRunScriptOption);
|
{"quiet", "Only write to console on error"},
|
||||||
QCommandLineOption userdataOption("cloudinit-userdata", "Add cloud-init user-data file to image", "cloudinit-userdata", "");
|
});
|
||||||
parser.addOption(userdataOption);
|
|
||||||
QCommandLineOption networkconfigOption("cloudinit-networkconfig", "Add cloud-init network-config file to image", "cloudinit-networkconfig", "");
|
|
||||||
parser.addOption(networkconfigOption);
|
|
||||||
|
|
||||||
QCommandLineOption debugOption("debug", "Output debug messages to console");
|
|
||||||
parser.addOption(debugOption);
|
|
||||||
QCommandLineOption quietOption("quiet", "Only write to console on error");
|
|
||||||
parser.addOption(quietOption);
|
|
||||||
|
|
||||||
parser.addPositionalArgument("src", "Image file/URL");
|
parser.addPositionalArgument("src", "Image file/URL");
|
||||||
parser.addPositionalArgument("dst", "Destination device");
|
parser.addPositionalArgument("dst", "Destination device");
|
||||||
|
@ -78,25 +70,25 @@ int Cli::main()
|
||||||
const QStringList args = parser.positionalArguments();
|
const QStringList args = parser.positionalArguments();
|
||||||
if (args.count() != 2)
|
if (args.count() != 2)
|
||||||
{
|
{
|
||||||
std::cerr << "Usage: --cli [--disable-verify] [--sha256 <expected hash> [--cache-file <cache file>]] [--first-run-script <script>] [--debug] [--quiet] <image file to write> <destination drive device>" << std::endl;
|
std::cerr << "Usage: --cli [--disable-verify] [--disable-eject] [--sha256 <expected hash> [--cache-file <cache file>]] [--first-run-script <script>] [--debug] [--quiet] <image file to write> <destination drive device>" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parser.isSet(debugOption))
|
if (!parser.isSet("debug"))
|
||||||
{
|
{
|
||||||
qInstallMessageHandler(devnullMsgHandler);
|
qInstallMessageHandler(devnullMsgHandler);
|
||||||
}
|
}
|
||||||
_quiet = parser.isSet(quietOption);
|
_quiet = parser.isSet("quiet");
|
||||||
QByteArray initFormat = (parser.value(userdataOption).isEmpty()
|
QByteArray initFormat = (parser.value("cloudinit-userdata").isEmpty()
|
||||||
&& parser.value(networkconfigOption).isEmpty() ) ? "systemd" : "cloudinit";
|
&& parser.value("cloudinit-networkconfig").isEmpty() ) ? "systemd" : "cloudinit";
|
||||||
|
|
||||||
if (args[0].startsWith("http:", Qt::CaseInsensitive) || args[0].startsWith("https:", Qt::CaseInsensitive))
|
if (args[0].startsWith("http:", Qt::CaseInsensitive) || args[0].startsWith("https:", Qt::CaseInsensitive))
|
||||||
{
|
{
|
||||||
_imageWriter->setSrc(args[0], 0, 0, parser.value(sha256Option).toLatin1(), false, "", "", initFormat);
|
_imageWriter->setSrc(args[0], 0, 0, parser.value("sha256").toLatin1(), false, "", "", initFormat);
|
||||||
|
|
||||||
if (!parser.value(cacheFileOption).isEmpty())
|
if (!parser.value("cache-file").isEmpty())
|
||||||
{
|
{
|
||||||
_imageWriter->setCustomCacheFile(parser.value(cacheFileOption), parser.value(sha256Option).toLatin1() );
|
_imageWriter->setCustomCacheFile(parser.value("cache-file"), parser.value("sha256").toLatin1() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -105,7 +97,7 @@ int Cli::main()
|
||||||
|
|
||||||
if (fi.isFile())
|
if (fi.isFile())
|
||||||
{
|
{
|
||||||
_imageWriter->setSrc(QUrl::fromLocalFile(args[0]), fi.size(), 0, parser.value(sha256Option).toLatin1(), false, "", "", initFormat);
|
_imageWriter->setSrc(QUrl::fromLocalFile(args[0]), fi.size(), 0, parser.value("sha256").toLatin1(), false, "", "", initFormat);
|
||||||
}
|
}
|
||||||
else if (!fi.exists())
|
else if (!fi.exists())
|
||||||
{
|
{
|
||||||
|
@ -119,7 +111,7 @@ int Cli::main()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parser.isSet(writeSystemDrive))
|
if (parser.isSet("enable-writing-system-drives"))
|
||||||
{
|
{
|
||||||
std::cerr << "WARNING: writing to system drives is enabled." << std::endl;
|
std::cerr << "WARNING: writing to system drives is enabled." << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -156,10 +148,10 @@ int Cli::main()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parser.value(userdataOption).isEmpty())
|
if (!parser.value("cloudinit-userdata").isEmpty())
|
||||||
{
|
{
|
||||||
QByteArray userData, networkConfig;
|
QByteArray userData, networkConfig;
|
||||||
QFile f(parser.value(userdataOption));
|
QFile f(parser.value("cloudinit-userdata"));
|
||||||
|
|
||||||
if (!f.exists())
|
if (!f.exists())
|
||||||
{
|
{
|
||||||
|
@ -177,7 +169,7 @@ int Cli::main()
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
f.setFileName(parser.value(networkconfigOption));
|
f.setFileName(parser.value("cloudinit-networkconfig"));
|
||||||
if (!f.exists())
|
if (!f.exists())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: network-config file does not exists" << std::endl;
|
std::cerr << "Error: network-config file does not exists" << std::endl;
|
||||||
|
@ -196,10 +188,10 @@ int Cli::main()
|
||||||
|
|
||||||
_imageWriter->setImageCustomization("", "", "", userData, networkConfig);
|
_imageWriter->setImageCustomization("", "", "", userData, networkConfig);
|
||||||
}
|
}
|
||||||
else if (!parser.value(firstRunScriptOption).isEmpty())
|
else if (!parser.value("first-run-script").isEmpty())
|
||||||
{
|
{
|
||||||
QByteArray firstRunScript;
|
QByteArray firstRunScript;
|
||||||
QFile f(parser.value(firstRunScriptOption));
|
QFile f(parser.value("first-run-script"));
|
||||||
if (!f.exists())
|
if (!f.exists())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: firstrun script does not exists" << std::endl;
|
std::cerr << "Error: firstrun script does not exists" << std::endl;
|
||||||
|
@ -220,7 +212,7 @@ int Cli::main()
|
||||||
}
|
}
|
||||||
|
|
||||||
_imageWriter->setDst(args[1]);
|
_imageWriter->setDst(args[1]);
|
||||||
_imageWriter->setVerifyEnabled(!parser.isSet(disableVerify));
|
_imageWriter->setVerifyEnabled(!parser.isSet("disable-verify"));
|
||||||
|
|
||||||
/* Run startWrite() in event loop (otherwise calling _app->exit() on error does not work) */
|
/* Run startWrite() in event loop (otherwise calling _app->exit() on error does not work) */
|
||||||
QTimer::singleShot(1, _imageWriter, &ImageWriter::startWrite);
|
QTimer::singleShot(1, _imageWriter, &ImageWriter::startWrite);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue