/* * SPDX-License-Identifier: Apache-2.0 * Copyright (C) 2020 Raspberry Pi Ltd */ #include "cli.h" #include "imagewriter.h" #include #include #include #include #include "drivelistmodel.h" #include "dependencies/drivelist/src/drivelist.hpp" /* Message handler to discard qDebug() output if using cli (unless --debug is set) */ static void devnullMsgHandler(QtMsgType, const QMessageLogContext &, const QString &) { } Cli::Cli(int &argc, char *argv[]) : QObject(nullptr) { #ifdef Q_OS_WIN /* Allocate console on Windows (only needed if compiled as GUI program) */ if (::AttachConsole(ATTACH_PARENT_PROCESS) || ::AllocConsole()) { freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); std::ios::sync_with_stdio(); } #endif _app = new QCoreApplication(argc, argv); _app->setOrganizationName("Raspberry Pi"); _app->setOrganizationDomain("raspberrypi.org"); _app->setApplicationName("Imager"); _imageWriter = new ImageWriter; connect(_imageWriter, &ImageWriter::success, this, &Cli::onSuccess); connect(_imageWriter, &ImageWriter::error, this, &Cli::onError); connect(_imageWriter, &ImageWriter::preparationStatusUpdate, this, &Cli::onPreparationStatusUpdate); connect(_imageWriter, &ImageWriter::downloadProgress, this, &Cli::onDownloadProgress); connect(_imageWriter, &ImageWriter::verifyProgress, this, &Cli::onVerifyProgress); } Cli::~Cli() { delete _imageWriter; delete _app; } int Cli::run() { QCommandLineParser parser; parser.addOptions({ {"cli", ""}, {"disable-verify", "Disable verification"}, {"enable-writing-system-drives", "Only use this if you know what you are doing"}, {"sha256", "Expected hash", "sha256", ""}, {"cache-file", "Custom cache file (requires setting sha256 as well)", "cache-file", ""}, {"first-run-script", "Add firstrun.sh to image", "first-run-script", ""}, {"cloudinit-userdata", "Add cloud-init user-data file to image", "cloudinit-userdata", ""}, {"cloudinit-networkconfig", "Add cloud-init network-config file to image", "cloudinit-networkconfig", ""}, {"disable-eject", "Disable automatic ejection of storage media after verification"}, {"debug", "Output debug messages to console"}, {"quiet", "Only write to console on error"}, }); parser.addPositionalArgument("src", "Image file/URL"); parser.addPositionalArgument("dst", "Destination device"); parser.process(*_app); const QStringList args = parser.positionalArguments(); if (args.count() != 2) { std::cerr << "Usage: --cli [--disable-verify] [--disable-eject] [--sha256 [--cache-file ]] [--first-run-script