Skip to content

Commit

Permalink
Merge #1099
Browse files Browse the repository at this point in the history
1099: Provide available settings (Fixes #1023) r=townsend2010 a=ricab

Addresses #1023 

This lists available configuration keys in `get` and `set`. I ideally they should offer guidance on what each key means and what values are allowed, but that will require more thought.

Co-authored-by: Ricardo Abreu <[email protected]>
  • Loading branch information
bors[bot] and ricab authored Oct 2, 2019
2 parents e266e08 + ec8fcd2 commit 723cf06
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/multipass/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QVariant>

#include <map>
#include <set>

namespace multipass
{
Expand All @@ -33,6 +34,7 @@ class Settings : public Singleton<Settings>
public:
Settings(const Singleton<Settings>::PrivatePass&);

std::set<QString> keys() const;
virtual QString get(const QString& key) const; // throws on unknown key
virtual void set(const QString& key, const QString& val); // throws on unknown key or bad settings

Expand Down
8 changes: 8 additions & 0 deletions src/client/cli/cmd/common_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <multipass/cli/argparser.h>
#include <multipass/cli/format_utils.h>
#include <multipass/exceptions/settings_exceptions.h>
#include <multipass/settings.h>

#include <fmt/ostream.h>
#include <sstream>
Expand Down Expand Up @@ -154,3 +155,10 @@ auto cmd::return_code_from(const mp::SettingsException& e) -> mp::ReturnCode
{
return dynamic_cast<const InvalidSettingsException*>(&e) ? ReturnCode::CommandLineError : ReturnCode::CommandFail;
}

QString multipass::cmd::describe_settings_keys()
{
const auto keys = Settings::instance().keys();
return std::accumulate(cbegin(keys), cend(keys), QStringLiteral("Keys:"),
[](const auto& a, const auto& b) { return a + "\n " + b; });
}
1 change: 1 addition & 0 deletions src/client/cli/cmd/common_cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ std::string instance_action_message_for(const InstanceNames& instance_names, con
ReturnCode run_cmd(const QStringList& args, const ArgParser* parser, std::ostream& cout, std::ostream& cerr);
ReturnCode run_cmd_and_retry(const QStringList& args, const ArgParser* parser, std::ostream& cout, std::ostream& cerr);
ReturnCode return_code_from(const SettingsException& e);
QString describe_settings_keys();

// helpers for update handling
bool update_available(const multipass::UpdateInfo& update_info);
Expand Down
9 changes: 5 additions & 4 deletions src/client/cli/cmd/get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ std::string cmd::Get::name() const

QString cmd::Get::short_help() const
{
return QStringLiteral("Get a configuration option");
return QStringLiteral("Get a configuration setting");
}

QString cmd::Get::description() const
{
return QStringLiteral("Get the configuration option corresponding to the given key");
auto desc = QStringLiteral("Get the configuration setting corresponding to the given key.");
return desc + "\n\n" + describe_settings_keys();
}

mp::ParseCode cmd::Get::parse_args(mp::ArgParser* parser)
{
parser->addPositionalArgument("key", "Path to the option whose configured value should be obtained.", "<key>");
parser->addPositionalArgument("key", "Path to the setting whose configured value should be obtained.", "<key>");

auto status = parser->commandParse(this);
if (status == ParseCode::Ok)
Expand All @@ -77,7 +78,7 @@ mp::ParseCode cmd::Get::parse_args(mp::ArgParser* parser)
}
else
{
cerr << "Need exactly one option key.\n";
cerr << "Need exactly one setting key.\n";
status = ParseCode::CommandLineError;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/client/cli/cmd/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,20 @@ std::string cmd::Set::name() const

QString cmd::Set::short_help() const
{
return QStringLiteral("Set a configuration option");
return QStringLiteral("Set a configuration setting");
}

QString cmd::Set::description() const
{
return QStringLiteral("Set, to the given value, the configuration option corresponding to the given key");
auto desc = QStringLiteral("Set, to the given value, the configuration setting corresponding to the given key.");
return desc + "\n\n" + describe_settings_keys();
}

mp::ParseCode cmd::Set::parse_args(mp::ArgParser* parser)
{
parser->addPositionalArgument(
"keyval",
"A key-value pair. The key specifies a path to the option to configure. The value is its intended value.",
"A key-value pair. The key specifies a path to the setting to configure. The value is its intended value.",
"<key>=<value>");

auto status = parser->commandParse(this);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cassert>
#include <cerrno>
#include <fstream>
#include <iterator>
#include <stdexcept>

namespace mp = multipass;
Expand Down Expand Up @@ -132,6 +133,15 @@ mp::Settings::Settings(const Singleton<Settings>::PrivatePass& pass)
{
}

std::set<QString> multipass::Settings::keys() const
{
std::set<QString> ret{};
std::transform(cbegin(defaults), cend(defaults), std::inserter(ret, begin(ret)),
[](const auto& elem) { return elem.first; }); // I wish get<0> worked here... maybe in C++20

return ret;
}

// TODO try installing yaml backend
QString mp::Settings::get(const QString& key) const
{
Expand Down

0 comments on commit 723cf06

Please sign in to comment.