[libcalamares] Allow a @@USER@@ replacement in commands

- Following example in preservefiles module, allow @@USER@@
   in commands (e.g. to do something specific in the home-dir
   of the new user).
This commit is contained in:
Adriaan de Groot 2018-05-23 09:25:57 -04:00
parent b7890d865f
commit b5d900c1c6
3 changed files with 30 additions and 12 deletions

View file

@ -98,9 +98,19 @@ CommandList::~CommandList()
{
}
static inline bool
findInCommands( const CommandList& l, const QString& needle )
{
for ( CommandList::const_iterator i = l.cbegin(); i != l.cend(); ++i )
if ( i->command().contains( needle ) )
return true;
return false;
}
Calamares::JobResult CommandList::run()
{
QLatin1Literal rootMagic( "@@ROOT@@" );
QLatin1Literal userMagic( "@@USER@@" );
System::RunLocation location = m_doChroot ? System::RunLocation::RunInTarget : System::RunLocation::RunInHost;
@ -108,14 +118,7 @@ Calamares::JobResult CommandList::run()
QString root = QStringLiteral( "/" );
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
bool needsRootSubstitution = false;
for ( CommandList::const_iterator i = cbegin(); i != cend(); ++i )
if ( i->command().contains( rootMagic ) )
{
needsRootSubstitution = true;
break;
}
bool needsRootSubstitution = findInCommands( *this, rootMagic );
if ( needsRootSubstitution && ( location == System::RunLocation::RunInHost ) )
{
if ( !gs || !gs->contains( "rootMountPoint" ) )
@ -127,10 +130,20 @@ Calamares::JobResult CommandList::run()
root = gs->value( "rootMountPoint" ).toString();
}
bool needsUserSubstitution = findInCommands( *this, userMagic );
if ( needsUserSubstitution && ( !gs || !gs->contains( "username" ) ) )
{
cError() << "No username defined.";
return Calamares::JobResult::error(
QCoreApplication::translate( "CommandList", "Could not run command." ),
QCoreApplication::translate( "CommandList", "The command needs to know the user's name, but no username is defined." ) );
}
QString user = gs->value( "username" ).toString(); // may be blank if unset
for ( CommandList::const_iterator i = cbegin(); i != cend(); ++i )
{
QString processed_cmd = i->command();
processed_cmd.replace( rootMagic, root );
processed_cmd.replace( rootMagic, root ).replace( userMagic, user );
bool suppress_result = false;
if ( processed_cmd.startsWith( '-' ) )
{