[libcalamares] Implement object-style command line

- handle command: and timeout: entries
 - test for setting the values
This commit is contained in:
Adriaan de Groot 2018-01-29 21:08:42 +01:00
parent 72bac332be
commit c641f5dec6
3 changed files with 46 additions and 1 deletions

View File

@ -21,6 +21,7 @@
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include "JobQueue.h" #include "JobQueue.h"
#include "utils/CalamaresUtils.h"
#include "utils/CalamaresUtilsSystem.h" #include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h" #include "utils/Logger.h"
@ -29,6 +30,17 @@
namespace CalamaresUtils namespace CalamaresUtils
{ {
static CommandLine get_variant_object( const QVariantMap& m )
{
QString command = CalamaresUtils::getString( m, "command" );
int timeout = CalamaresUtils::getInteger( m, "timeout", -1 );
if ( !command.isEmpty() )
return CommandLine( command, timeout );
cDebug() << "WARNING Bad CommandLine element" << m;
return CommandLine();
}
static CommandList_t get_variant_stringlist( const QVariantList& l, int timeout ) static CommandList_t get_variant_stringlist( const QVariantList& l, int timeout )
{ {
CommandList_t retl; CommandList_t retl;
@ -37,6 +49,13 @@ static CommandList_t get_variant_stringlist( const QVariantList& l, int timeout
{ {
if ( v.type() == QVariant::String ) if ( v.type() == QVariant::String )
retl.append( CommandLine( v.toString(), timeout ) ); retl.append( CommandLine( v.toString(), timeout ) );
else if ( v.type() == QVariant::Map )
{
auto c( get_variant_object( v.toMap() ) );
if ( c.isValid() )
retl.append( c );
// Otherwise warning is already given
}
else else
cDebug() << "WARNING Bad CommandList element" << c << v.type() << v; cDebug() << "WARNING Bad CommandList element" << c << v.type() << v;
++c; ++c;
@ -63,6 +82,13 @@ CommandList::CommandList::CommandList( const QVariant& v, bool doChroot, int tim
} }
else if ( v.type() == QVariant::String ) else if ( v.type() == QVariant::String )
append( v.toString() ); append( v.toString() );
else if ( v.type() == QVariant::Map )
{
auto c( get_variant_object( v.toMap() ) );
if ( c.isValid() )
append( c );
// Otherwise warning is already given
}
else else
cDebug() << "WARNING: CommandList does not understand variant" << v.type(); cDebug() << "WARNING: CommandList does not understand variant" << v.type();
} }

View File

@ -33,6 +33,12 @@ namespace CalamaresUtils
*/ */
struct CommandLine : public QPair< QString, int > struct CommandLine : public QPair< QString, int >
{ {
/// An invalid command line
CommandLine()
: QPair< QString, int >( QString(), -1 )
{
}
CommandLine( const QString& s ) CommandLine( const QString& s )
: QPair< QString, int >( s, 10 ) : QPair< QString, int >( s, 10 )
{ {
@ -52,6 +58,11 @@ struct CommandLine : public QPair< QString, int >
{ {
return second; return second;
} }
bool isValid() const
{
return !first.isEmpty();
}
} ; } ;
/** @brief Abbreviation, used internally. */ /** @brief Abbreviation, used internally. */
@ -82,6 +93,7 @@ public:
using CommandList_t::cbegin; using CommandList_t::cbegin;
using CommandList_t::cend; using CommandList_t::cend;
using CommandList_t::const_iterator; using CommandList_t::const_iterator;
using CommandList_t::at;
protected: protected:
using CommandList_t::append; using CommandList_t::append;

View File

@ -102,6 +102,8 @@ script: "ls /tmp"
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 1 ); QCOMPARE( cl.count(), 1 );
QCOMPARE( cl.at(0).timeout(), 10 );
QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) );
// Not a string // Not a string
doc = YAML::Load( R"(--- doc = YAML::Load( R"(---
@ -125,6 +127,8 @@ script:
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 1 ); QCOMPARE( cl.count(), 1 );
QCOMPARE( cl.at(0).timeout(), 20 );
QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) );
} }
void ShellProcessTests::testProcessListFromObject() void ShellProcessTests::testProcessListFromObject()
@ -132,11 +136,14 @@ void ShellProcessTests::testProcessListFromObject()
YAML::Node doc = YAML::Load( R"(--- YAML::Node doc = YAML::Load( R"(---
script: script:
- command: "ls /tmp" - command: "ls /tmp"
timeout: 20 timeout: 12
- "-/bin/false" - "-/bin/false"
)" ); )" );
CommandList cl( CommandList cl(
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 2 ); QCOMPARE( cl.count(), 2 );
QCOMPARE( cl.at(0).timeout(), 12 );
QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) );
QCOMPARE( cl.at(1).timeout(), 10 ); // default
} }