Skip to content

Commit

Permalink
[client] Move bridge prompt to client_common
Browse files Browse the repository at this point in the history
  • Loading branch information
luis4a0 committed Aug 14, 2023
1 parent 5fe604a commit 0b5cd7d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 47 deletions.
20 changes: 20 additions & 0 deletions include/multipass/cli/prompters.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include <multipass/disabled_copy_move.h>
#include <multipass/terminal.h>

#include <regex>
#include <string>
#include <vector>

#ifndef MULTIPASS_CLI_PROMPTERS_H
#define MULTIPASS_CLI_PROMPTERS_H
Expand Down Expand Up @@ -88,6 +90,24 @@ class NewPassphrasePrompter : public PassphrasePrompter

std::string prompt(const std::string& text = "Please re-enter passphrase") const override;
};

class BridgePrompter : private DisabledCopyMove
{
public:
explicit BridgePrompter(Terminal* term) : term(term){};

~BridgePrompter() = default;

bool bridge_prompt(std::vector<std::string>& nets_need_bridging) const;

private:
BridgePrompter() = default;

Terminal* term;

const std::regex yes{"y|yes", std::regex::icase | std::regex::optimize};
const std::regex no{"n|no", std::regex::icase | std::regex::optimize};
};
} // namespace multipass

#endif // MULTIPASS_CLI_PROMPTERS_H
50 changes: 3 additions & 47 deletions src/client/cli/cmd/launch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <multipass/cli/argparser.h>
#include <multipass/cli/client_platform.h>
#include <multipass/cli/prompters.h>
#include <multipass/constants.h>
#include <multipass/exceptions/cmd_exceptions.h>
#include <multipass/exceptions/snap_environment_exception.h>
Expand Down Expand Up @@ -53,19 +54,6 @@ namespace fs = std::filesystem;

namespace
{
const std::regex yes{"y|yes", std::regex::icase | std::regex::optimize};
const std::regex no{"n|no", std::regex::icase | std::regex::optimize};

constexpr bool on_windows()
{ // TODO when we have remote client-daemon communication, we need to get the daemon's platform
return
#ifdef MULTIPASS_PLATFORM_WINDOWS
true;
#else
false;
#endif
}

auto checked_mode(const std::string& mode)
{
if (mode == "auto")
Expand Down Expand Up @@ -119,39 +107,6 @@ auto net_digest(const QString& options)

return net;
}

bool bridge_prompt(mp::Terminal* term, std::ostream& cout, std::vector<std::string> nets_need_bridging)
{
static constexpr auto plural = "Multipass needs to create {} to connect to {}.\nThis will temporarily disrupt "
"connectivity on those interfaces.\n\nDo you want to continue (yes/no)? ";
static constexpr auto singular = "Multipass needs to create a {} to connect to {}.\nThis will temporarily disrupt "
"connectivity on that interface.\n\nDo you want to continue (yes/no)? ";
static constexpr auto nodes = on_windows() ? "switches" : "bridges";
static constexpr auto node = on_windows() ? "switch" : "bridge";

if (term->is_live())
{
assert(nets_need_bridging.size()); // precondition
if (nets_need_bridging.size() != 1)
fmt::print(cout, plural, nodes, fmt::join(nets_need_bridging, ", "));
else
fmt::print(cout, singular, node, nets_need_bridging[0]);

while (true)
{
std::string answer;
std::getline(term->cin(), answer);
if (std::regex_match(answer, yes))
return true;
else if (std::regex_match(answer, no))
return false;
else
cout << "Please answer yes/no: ";
}
}

return false;
}
} // namespace

mp::ReturnCode cmd::Launch::run(mp::ArgParser* parser)
Expand Down Expand Up @@ -659,5 +614,6 @@ bool cmd::Launch::ask_bridge_permission(multipass::LaunchReply& reply)
for (auto i = 0; i < reply.nets_need_bridging_size(); ++i)
nets.push_back(reply.nets_need_bridging(i));

return bridge_prompt(term, cout, nets);
mp::BridgePrompter prompter(term);
return prompter.bridge_prompt(nets);
}
46 changes: 46 additions & 0 deletions src/client/common/prompters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <multipass/cli/prompters.h>
#include <multipass/exceptions/cli_exceptions.h>
#include <multipass/format.h>

#include <iostream>

Expand All @@ -34,6 +35,17 @@ auto get_input(std::istream& cin)

return value;
}

// TODO when we have remote client-daemon communication, we need to get the daemon's platform
constexpr bool on_windows()
{
return
#ifdef MULTIPASS_PLATFORM_WINDOWS
true;
#else
false;
#endif
}
} // namespace

std::string mp::PlainPrompter::prompt(const std::string& text) const
Expand Down Expand Up @@ -66,3 +78,37 @@ std::string mp::NewPassphrasePrompter::prompt(const std::string& text) const

return passphrase;
}

bool mp::BridgePrompter::bridge_prompt(std::vector<std::string>& nets_need_bridging) const
{
assert(nets_need_bridging.size()); // precondition

static constexpr auto plural = "Multipass needs to create {} to connect to {}.\nThis will temporarily disrupt "
"connectivity on those interfaces.\n\nDo you want to continue (yes/no)? ";
static constexpr auto singular = "Multipass needs to create a {} to connect to {}.\nThis will temporarily disrupt "
"connectivity on that interface.\n\nDo you want to continue (yes/no)? ";
static constexpr auto nodes = on_windows() ? "switches" : "bridges";
static constexpr auto node = on_windows() ? "switch" : "bridge";

if (term->is_live())
{
if (nets_need_bridging.size() != 1)
fmt::print(term->cout(), plural, nodes, fmt::join(nets_need_bridging, ", "));
else
fmt::print(term->cout(), singular, node, nets_need_bridging[0]);

while (true)
{
std::string answer;
std::getline(term->cin(), answer);
if (std::regex_match(answer, yes))
return true;
else if (std::regex_match(answer, no))
return false;
else
term->cout() << "Please answer yes/no: ";
}
}

return false;
}

0 comments on commit 0b5cd7d

Please sign in to comment.