[libcalamares] Extend CommandLine

- rename fields so they are meaningful (this is a leftover
  from it inheriting std::pair)
- add environment list member
- add constructor that consumes a QVariantMap
This commit is contained in:
Adriaan de Groot 2024-02-20 16:51:23 +01:00
parent 15c514326c
commit 0d9d2ac59a
2 changed files with 45 additions and 12 deletions

View File

@ -94,12 +94,29 @@ get_gs_expander( System::RunLocation location )
return expander; return expander;
} }
CommandLine::CommandLine( const QVariantMap& m )
{
const QString command = Calamares::getString( m, "command" );
const qint64 timeout = Calamares::getInteger( m, "timeout", -1 );
if ( !command.isEmpty() )
{
m_command = command;
m_timeout = timeout >= 0 ? std::chrono::seconds( timeout ) : CommandLine::TimeoutNotSet();
m_environment = Calamares::getStringList( m, "environment" );
}
else
{
cWarning() << "Bad CommandLine element" << m;
// this CommandLine is invalid
}
}
CommandLine CommandLine
CommandLine::expand( KMacroExpanderBase& expander ) const CommandLine::expand( KMacroExpanderBase& expander ) const
{ {
QString c = first; QString c = m_command;
expander.expandMacrosShellQuote( c ); expander.expandMacrosShellQuote( c );
return { c, second }; return { c, m_environment, m_timeout };
} }
Calamares::CommandLine Calamares::CommandLine

View File

@ -28,30 +28,43 @@ namespace Calamares
* Each command can have an associated timeout in seconds. The timeout * Each command can have an associated timeout in seconds. The timeout
* defaults to 10 seconds. Provide some convenience naming and construction. * defaults to 10 seconds. Provide some convenience naming and construction.
*/ */
struct CommandLine class CommandLine
{ {
public:
static inline constexpr std::chrono::seconds TimeoutNotSet() { return std::chrono::seconds( -1 ); } static inline constexpr std::chrono::seconds TimeoutNotSet() { return std::chrono::seconds( -1 ); }
/// An invalid command line /// An invalid command line
CommandLine() = default; CommandLine() = default;
CommandLine( const QString& s ) CommandLine( const QString& s )
: first( s ) : m_command( s )
, second( TimeoutNotSet() )
{ {
} }
CommandLine( const QString& s, std::chrono::seconds t ) CommandLine( const QString& s, std::chrono::seconds t )
: first( s ) : m_command( s )
, second( t ) , m_timeout( t )
{ {
} }
QString command() const { return first; } CommandLine( const QString& s, const QStringList& env, std::chrono::seconds t )
: m_command( s )
, m_environment( env )
, m_timeout( t )
{
}
std::chrono::seconds timeout() const { return second; } /** @brief Constructs a CommandLine from a map with keys
*
* Relevant keys are *command*, *environment* and *timeout*.
*/
CommandLine( const QVariantMap& m );
bool isValid() const { return !first.isEmpty(); } QString command() const { return m_command; }
[[nodiscard]] QStringList environment() const { return m_environment; }
std::chrono::seconds timeout() const { return m_timeout; }
bool isValid() const { return !m_command.isEmpty(); }
/** @brief Returns a copy of this one command, with variables expanded /** @brief Returns a copy of this one command, with variables expanded
* *
@ -60,6 +73,7 @@ struct CommandLine
* instance, which handles the ROOT and USER variables. * instance, which handles the ROOT and USER variables.
*/ */
DLLEXPORT CommandLine expand( KMacroExpanderBase& expander ) const; DLLEXPORT CommandLine expand( KMacroExpanderBase& expander ) const;
/** @brief As above, with a default macro-expander. /** @brief As above, with a default macro-expander.
* *
* The default macro-expander assumes RunInHost (e.g. ROOT will * The default macro-expander assumes RunInHost (e.g. ROOT will
@ -68,8 +82,9 @@ struct CommandLine
DLLEXPORT CommandLine expand() const; DLLEXPORT CommandLine expand() const;
private: private:
QString first; QString m_command;
std::chrono::seconds second = std::chrono::seconds( -1 ); QStringList m_environment;
std::chrono::seconds m_timeout = TimeoutNotSet();
}; };
/** @brief Abbreviation, used internally. */ /** @brief Abbreviation, used internally. */
@ -109,6 +124,7 @@ public:
* @see CommandLine::expand() for details. * @see CommandLine::expand() for details.
*/ */
CommandList expand( KMacroExpanderBase& expander ) const; CommandList expand( KMacroExpanderBase& expander ) const;
/** @brief As above, with a default macro-expander. /** @brief As above, with a default macro-expander.
* *
* Each command-line in the list is expanded with that default macro-expander. * Each command-line in the list is expanded with that default macro-expander.