[contextualprocess] Add wildcard

- Re-build the structures for doing value-checks, is now more tree-like.
 - Document pointer ownership.
 - Introduce wildcard matches ("*")
 - Don't drop empty command-lists, since they can be used to avoid
   wildcard matches. (E.g. "in this case, do nothing, but don't
   fall through to wildcard").
This commit is contained in:
Adriaan de Groot 2018-02-19 06:18:08 -05:00
parent 7ecb39574e
commit 699b42a756
2 changed files with 62 additions and 19 deletions

View file

@ -30,31 +30,71 @@
#include "utils/CommandList.h" #include "utils/CommandList.h"
#include "utils/Logger.h" #include "utils/Logger.h"
/**
* Passing a CommandList to ValueCheck gives ownership of the CommandList to
* the ValueCheck, which will delete the CommandList when needed.
*/
struct ValueCheck : public QPair<QString, CalamaresUtils::CommandList*>
{
ValueCheck( const QString& value, CalamaresUtils::CommandList* commands )
: QPair<QString, CalamaresUtils::CommandList*>(value, commands)
{
}
~ValueCheck()
{
delete second;
}
QString value() const { return first; }
CalamaresUtils::CommandList* commands() const { return second; }
} ;
struct ContextualProcessBinding struct ContextualProcessBinding
{ {
ContextualProcessBinding( const QString& _n, const QString& _v, CalamaresUtils::CommandList* _c ) ContextualProcessBinding( const QString& varname )
: variable( _n ) : variable( varname )
, value( _v )
, commands( _c )
{ {
} }
~ContextualProcessBinding(); ~ContextualProcessBinding();
int count() const /**
* @brief add commands to be executed when @p value is matched.
*
* Ownership of the CommandList passes to the ValueCheck held
* by this binding.
*/
void append( const QString& value, CalamaresUtils::CommandList* commands )
{ {
return commands ? commands->count() : 0; checks.append( ValueCheck( value, commands ) );
if ( value == '*' )
wildcard = commands;
}
Calamares::JobResult run( const QString& value ) const
{
for ( const auto& c : checks )
{
if ( value == c.value() )
return c.commands()->run();
}
if ( wildcard )
return wildcard->run();
return Calamares::JobResult::ok();
} }
QString variable; QString variable;
QString value; QList<ValueCheck> checks;
CalamaresUtils::CommandList* commands; CalamaresUtils::CommandList* wildcard{ nullptr };
} ; } ;
ContextualProcessBinding::~ContextualProcessBinding() ContextualProcessBinding::~ContextualProcessBinding()
{ {
delete commands; wildcard = nullptr;
} }
ContextualProcessJob::ContextualProcessJob( QObject* parent ) ContextualProcessJob::ContextualProcessJob( QObject* parent )
@ -83,9 +123,9 @@ ContextualProcessJob::exec()
for ( const ContextualProcessBinding* binding : m_commands ) for ( const ContextualProcessBinding* binding : m_commands )
{ {
if ( gs->contains( binding->variable ) && ( gs->value( binding->variable ).toString() == binding->value ) ) if ( gs->contains( binding->variable ) )
{ {
Calamares::JobResult r = binding->commands->run(); Calamares::JobResult r = binding->run( gs->value( binding->variable ).toString() );
if ( !r ) if ( !r )
return r; return r;
} }
@ -114,6 +154,8 @@ ContextualProcessJob::setConfigurationMap( const QVariantMap& configurationMap )
continue; continue;
} }
auto binding = new ContextualProcessBinding( variableName );
m_commands.append( binding );
QVariantMap values = iter.value().toMap(); QVariantMap values = iter.value().toMap();
for ( QVariantMap::const_iterator valueiter = values.cbegin(); valueiter != values.cend(); ++valueiter ) for ( QVariantMap::const_iterator valueiter = values.cbegin(); valueiter != values.cend(); ++valueiter )
{ {
@ -126,13 +168,7 @@ ContextualProcessJob::setConfigurationMap( const QVariantMap& configurationMap )
CalamaresUtils::CommandList* commands = new CalamaresUtils::CommandList( valueiter.value(), !dontChroot, timeout ); CalamaresUtils::CommandList* commands = new CalamaresUtils::CommandList( valueiter.value(), !dontChroot, timeout );
if ( commands->count() > 0 ) binding->append( valueString, commands );
{
m_commands.append( new ContextualProcessBinding( variableName, valueString, commands ) );
cDebug() << variableName << '=' << valueString << "will execute" << commands->count() << "commands";
}
else
delete commands;
} }
} }
} }

View file

@ -15,12 +15,18 @@
# #
# You can check for an empty value with "". # You can check for an empty value with "".
# #
# As a special case, the value-check "*" matches any value, but **only**
# if no other value-check matches. Use it as an *else* form for value-
# checks. Take care to put the asterisk in quotes.
#
# Global configuration variables are not checked in a deterministic # Global configuration variables are not checked in a deterministic
# order, so do not rely on commands from one variable-check to # order, so do not rely on commands from one variable-check to
# always happen before (or after) checks on another # always happen before (or after) checks on another
# variable. Similarly, the value-equality checks are not # variable. Similarly, the value-equality checks are not
# done in a deterministic order, but all of the value-checks # done in a deterministic order, but all of the value-checks
# for a given variable happen together. # for a given variable happen together. As a special case, the
# value-check for "*" (the *else* case) happens after all of the
# other value-checks, and only matches if none of the others do.
# #
# The values after a value sub-keys are the same kinds of values # The values after a value sub-keys are the same kinds of values
# as can be given to the *script* key in the shellprocess module. # as can be given to the *script* key in the shellprocess module.
@ -34,3 +40,4 @@ firmwareType:
timeout: 120 # This is slow timeout: 120 # This is slow
bios: "-pkg remove bios-firmware" bios: "-pkg remove bios-firmware"
"": "/bin/false no-firmware-type-set" "": "/bin/false no-firmware-type-set"
"*": "/bin/false some-other-firmware-value"