From a732ce11bcf60c6c9ac978200d0bab0e0081cd45 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 11 Jun 2018 08:33:47 -0400 Subject: [PATCH] [libcalamares] Warn more about badly-formed config --- src/libcalamares/Settings.cpp | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/libcalamares/Settings.cpp b/src/libcalamares/Settings.cpp index 087822623..bc645ab39 100644 --- a/src/libcalamares/Settings.cpp +++ b/src/libcalamares/Settings.cpp @@ -30,6 +30,32 @@ #include +/** Helper function to grab a QString out of the config, and to warn if not present. */ +static QString +requireString( const YAML::Node& config, const char* key ) +{ + if ( config[ key ] ) + return QString::fromStdString( config[ key ].as< std::string >() ); + else + { + cWarning() << "Required settings.conf key" << key << "is missing."; + return QString(); + } +} + +/** Helper function to grab a bool out of the config, and to warn if not present. */ +static bool +requireBool( const YAML::Node& config, const char* key, bool d ) +{ + if ( config[ key ] ) + return config[ key ].as< bool >(); + else + { + cWarning() << "Required settings.conf key" << key << "is missing."; + return d; + } +} + namespace Calamares { @@ -41,7 +67,6 @@ Settings::instance() return s_instance; } - Settings::Settings( const QString& settingsFilePath, bool debugMode, QObject* parent ) @@ -148,11 +173,9 @@ Settings::Settings( const QString& settingsFilePath, } } - m_brandingComponentName = QString::fromStdString( config[ "branding" ] - .as< std::string >() ); - m_promptInstall = config[ "prompt-install" ].as< bool >(); - - m_doChroot = config[ "dont-chroot" ] ? !config[ "dont-chroot" ].as< bool >() : true; + m_brandingComponentName = requireString( config, "branding" ); + m_promptInstall = requireBool( config, "prompt-install", false ); + m_doChroot = requireBool( config, "dont-chroot", true ); } catch ( YAML::Exception& e ) {