[libcalamares] Refactor target-environment calls
- Add a more general targetEnvCommand() that returns both error code and process output. - Change existing targetEnvCall() and targetEnvOutput() to use general form while discarding some data.
This commit is contained in:
parent
f5aec1ad8a
commit
9d31380980
@ -92,42 +92,14 @@ System::mount( const QString& devicePath,
|
||||
return QProcess::execute( program, args );
|
||||
}
|
||||
|
||||
int
|
||||
System::targetEnvCall( const QStringList& args,
|
||||
const QString& workingPath,
|
||||
const QString& stdInput,
|
||||
int timeoutSec )
|
||||
ProcessResult
|
||||
System::targetEnvCommand(
|
||||
const QStringList& args,
|
||||
const QString& workingPath,
|
||||
const QString& stdInput,
|
||||
int timeoutSec )
|
||||
{
|
||||
QString discard;
|
||||
return targetEnvOutput( args,
|
||||
discard,
|
||||
workingPath,
|
||||
stdInput,
|
||||
timeoutSec );
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
System::targetEnvCall( const QString& command,
|
||||
const QString& workingPath,
|
||||
const QString& stdInput,
|
||||
int timeoutSec )
|
||||
{
|
||||
return targetEnvCall( QStringList{ command },
|
||||
workingPath,
|
||||
stdInput,
|
||||
timeoutSec );
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
System::targetEnvOutput( const QStringList& args,
|
||||
QString& output,
|
||||
const QString& workingPath,
|
||||
const QString& stdInput,
|
||||
int timeoutSec )
|
||||
{
|
||||
output.clear();
|
||||
QString output;
|
||||
|
||||
if ( !Calamares::JobQueue::instance() )
|
||||
return -3;
|
||||
@ -212,22 +184,7 @@ System::targetEnvOutput( const QStringList& args,
|
||||
cLog() << "Target cmd:" << args;
|
||||
cLog().noquote() << "Target output:\n" << output;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
System::targetEnvOutput( const QString& command,
|
||||
QString& output,
|
||||
const QString& workingPath,
|
||||
const QString& stdInput,
|
||||
int timeoutSec )
|
||||
{
|
||||
return targetEnvOutput( QStringList{ command },
|
||||
output,
|
||||
workingPath,
|
||||
stdInput,
|
||||
timeoutSec );
|
||||
return ProcessResult(r, output);
|
||||
}
|
||||
|
||||
|
||||
|
@ -21,10 +21,18 @@
|
||||
#include "DllMacro.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QPair>
|
||||
#include <QString>
|
||||
|
||||
namespace CalamaresUtils
|
||||
{
|
||||
class ProcessResult : public QPair< int, QString >
|
||||
{
|
||||
public:
|
||||
/** @brief Implicit one-argument constructor has no output, only a return code */
|
||||
ProcessResult( int r ) : QPair< int, QString >( r, QString() ) {}
|
||||
ProcessResult( int r, QString s ) : QPair< int, QString >( r, s ) {}
|
||||
} ;
|
||||
|
||||
/**
|
||||
* @brief The System class is a singleton with utility functions that perform
|
||||
@ -61,42 +69,75 @@ public:
|
||||
const QString& filesystemName = QString(),
|
||||
const QString& options = QString() );
|
||||
|
||||
|
||||
/**
|
||||
* Runs the specified command in the chroot of the target system.
|
||||
* @param args the call with arguments, as a string list.
|
||||
* @param args the command with arguments, as a string list.
|
||||
* @param workingPath the current working directory for the QProcess
|
||||
* call (optional).
|
||||
* @param stdInput the input string to send to the running process as
|
||||
* standard input (optional).
|
||||
* @param timeoutSec the timeout after which the process will be
|
||||
* killed (optional, default is 0 i.e. no timeout).
|
||||
* @returns the program's exit code, or:
|
||||
*
|
||||
* @returns the program's exit code and its output (if any). Special
|
||||
* exit codes (which will never have any output) are:
|
||||
* -1 = QProcess crash
|
||||
* -2 = QProcess cannot start
|
||||
* -3 = bad arguments
|
||||
* -4 = QProcess timeout
|
||||
*/
|
||||
DLLEXPORT int targetEnvCall( const QStringList& args,
|
||||
DLLEXPORT ProcessResult targetEnvCommand(
|
||||
const QStringList &args,
|
||||
const QString& workingPath = QString(),
|
||||
const QString& stdInput = QString(),
|
||||
int timeoutSec = 0 );
|
||||
|
||||
/** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code */
|
||||
inline int targetEnvCall( const QStringList& args,
|
||||
const QString& workingPath = QString(),
|
||||
const QString& stdInput = QString(),
|
||||
int timeoutSec = 0 );
|
||||
int timeoutSec = 0 )
|
||||
{
|
||||
return targetEnvCommand( args, workingPath, stdInput, timeoutSec ).first;
|
||||
}
|
||||
|
||||
DLLEXPORT int targetEnvCall( const QString& command,
|
||||
/** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code */
|
||||
inline int targetEnvCall( const QString& command,
|
||||
const QString& workingPath = QString(),
|
||||
const QString& stdInput = QString(),
|
||||
int timeoutSec = 0 );
|
||||
int timeoutSec = 0 )
|
||||
{
|
||||
return targetEnvCall( QStringList{ command }, workingPath, stdInput, timeoutSec );
|
||||
}
|
||||
|
||||
DLLEXPORT int targetEnvOutput( const QStringList& args,
|
||||
/** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code
|
||||
*
|
||||
* Places the called program's output in the @p output string.
|
||||
*/
|
||||
int targetEnvOutput( const QStringList& args,
|
||||
QString& output,
|
||||
const QString& workingPath = QString(),
|
||||
const QString& stdInput = QString(),
|
||||
int timeoutSec = 0 );
|
||||
int timeoutSec = 0 )
|
||||
{
|
||||
auto r = targetEnvCommand( args, workingPath, stdInput, timeoutSec );
|
||||
output = r.second;
|
||||
return r.first;
|
||||
}
|
||||
|
||||
DLLEXPORT int targetEnvOutput( const QString& command,
|
||||
/** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code
|
||||
*
|
||||
* Places the called program's output in the @p output string.
|
||||
*/
|
||||
inline int targetEnvOutput( const QString& command,
|
||||
QString& output,
|
||||
const QString& workingPath = QString(),
|
||||
const QString& stdInput = QString(),
|
||||
int timeoutSec = 0 );
|
||||
int timeoutSec = 0 )
|
||||
{
|
||||
return targetEnvOutput( QStringList{ command }, output, workingPath, stdInput, timeoutSec );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief getTotalMemoryB returns the total main memory, in bytes.
|
||||
|
Loading…
Reference in New Issue
Block a user