Skip to content

Commit

Permalink
Merge #2077
Browse files Browse the repository at this point in the history
2077: [cli] Version Information Formatter r=Saviq a=surahman

In response to issue #2020
Outputting the updates available notice message to `stderr` until I read up on _**QT Command Line Parser**_ and set up flag options.

Co-authored-by: Saad Ur Rahman <saad.ur.rahman@gmail.com>
  • Loading branch information
bors[bot] and surahman authored Aug 2, 2021
2 parents b500386 + 28f79fc commit 2126b95
Show file tree
Hide file tree
Showing 19 changed files with 296 additions and 52 deletions.
3 changes: 2 additions & 1 deletion include/multipass/cli/client_common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 Canonical, Ltd.
* Copyright (C) 2019-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -42,6 +42,7 @@ multipass::ReturnCode standard_failure_handler_for(const std::string& command, s
const grpc::Status& status,
const std::string& error_details = std::string());
bool update_available(const UpdateInfo& update_info);
std::string update_notice(const multipass::UpdateInfo& update_info);
}

namespace client
Expand Down
3 changes: 2 additions & 1 deletion include/multipass/cli/csv_formatter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,6 +29,7 @@ class CSVFormatter final : public Formatter
std::string format(const ListReply& list) const override;
std::string format(const NetworksReply& list) const override;
std::string format(const FindReply& list) const override;
std::string format(const VersionReply& list, const std::string& client_version) const override;
};
}
#endif // MULTIPASS_CSV_FORMATTER
3 changes: 2 additions & 1 deletion include/multipass/cli/formatter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -36,6 +36,7 @@ class Formatter
virtual std::string format(const ListReply& reply) const = 0;
virtual std::string format(const NetworksReply& reply) const = 0;
virtual std::string format(const FindReply& reply) const = 0;
virtual std::string format(const VersionReply& reply, const std::string& client_version) const = 0;

protected:
Formatter() = default;
Expand Down
3 changes: 2 additions & 1 deletion include/multipass/cli/json_formatter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,6 +29,7 @@ class JsonFormatter final : public Formatter
std::string format(const ListReply& list) const override;
std::string format(const NetworksReply& list) const override;
std::string format(const FindReply& list) const override;
std::string format(const VersionReply& list, const std::string& client_version) const override;
};
}
#endif // MULTIPASS_JSON_FORMATTER
3 changes: 2 additions & 1 deletion include/multipass/cli/table_formatter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,6 +29,7 @@ class TableFormatter final : public Formatter
std::string format(const ListReply& list) const override;
std::string format(const NetworksReply& list) const override;
std::string format(const FindReply& list) const override;
std::string format(const VersionReply& list, const std::string& client_version) const override;
};
}
#endif // MULTIPASS_TABLE_FORMATTER
3 changes: 2 additions & 1 deletion include/multipass/cli/yaml_formatter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,6 +29,7 @@ class YamlFormatter final : public Formatter
std::string format(const ListReply& list) const override;
std::string format(const NetworksReply& list) const override;
std::string format(const FindReply& list) const override;
std::string format(const VersionReply& list, const std::string& client_version) const override;
};
}
#endif // MULTIPASS_YAML_FORMATTER
26 changes: 0 additions & 26 deletions src/client/cli/cmd/common_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@
namespace mp = multipass;
namespace cmd = multipass::cmd;

namespace
{
std::string message_box(const std::string& message)
{
std::string::size_type divider_length = 50;
{
std::istringstream m(message);
std::string s;
while (getline(m, s, '\n'))
{
divider_length = std::max(divider_length, s.length());
}
}

const auto divider = std::string(divider_length, '#');

return '\n' + divider + '\n' + message + '\n' + divider + '\n';
}
} // namespace

mp::ParseCode cmd::check_for_name_and_all_option_conflict(const mp::ArgParser* parser, std::ostream& cerr,
bool allow_empty)
{
Expand Down Expand Up @@ -125,12 +105,6 @@ std::string cmd::instance_action_message_for(const mp::InstanceNames& instance_n
return message;
}

std::string cmd::update_notice(const mp::UpdateInfo& update_info)
{
return ::message_box(fmt::format("{}\n{}\n\nGo here for more information: {}", update_info.title(),
update_info.description(), update_info.url()));
}

mp::ReturnCode cmd::run_cmd(const QStringList& args, const mp::ArgParser* parser, std::ostream& cout,
std::ostream& cerr)
{
Expand Down
4 changes: 0 additions & 4 deletions src/client/cli/cmd/common_cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ ReturnCode run_cmd_and_retry(const QStringList& args, const ArgParser* parser, s
ReturnCode return_code_from(const SettingsException& e);
QString describe_settings_keys();

// helpers for update handling
bool update_available(const multipass::UpdateInfo& update_info);
std::string update_notice(const multipass::UpdateInfo& update_info);

// parser helpers
void add_timeout(multipass::ArgParser*);
int parse_timeout(const multipass::ArgParser* parser);
Expand Down
43 changes: 35 additions & 8 deletions src/client/cli/cmd/version.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Canonical, Ltd.
* Copyright (C) 2017-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -18,6 +18,7 @@
#include "version.h"
#include "common_cli.h"
#include <multipass/cli/argparser.h>
#include <multipass/cli/formatter.h>
#include <multipass/version.h>

namespace mp = multipass;
Expand All @@ -32,16 +33,20 @@ mp::ReturnCode cmd::Version::run(mp::ArgParser* parser)
return parser->returnCodeFrom(ret);
}

cout << "multipass " << multipass::version_string << "\n";
auto on_success = [this](mp::VersionReply& reply)
{
cout << chosen_formatter->format(reply, multipass::version_string);

auto on_success = [this](mp::VersionReply& reply) {
cout << "multipassd " << reply.version() << "\n";
if (term->is_live() && update_available(reply.update_info()))
cout << update_notice(reply.update_info());
return ReturnCode::Ok;
};

auto on_failure = [](grpc::Status& status) { return ReturnCode::Ok; };
auto on_failure = [this](grpc::Status& status)
{
VersionReply reply;
cout << chosen_formatter->format(reply, multipass::version_string);

return ReturnCode::Ok;
};

mp::VersionRequest request;
request.set_verbosity_level(parser->verbosityLevel());
Expand All @@ -66,5 +71,27 @@ QString cmd::Version::description() const

mp::ParseCode cmd::Version::parse_args(mp::ArgParser* parser)
{
return parser->commandParse(this);
QCommandLineOption formatOption("format",
"Output version information in the requested format.\n"
"Valid formats are: table (default), json, csv and yaml",
"format", "table");

parser->addOption(formatOption);

auto status = parser->commandParse(this);

if (status != ParseCode::Ok)
{
return status;
}

if (parser->positionalArguments().count() > 0)
{
cerr << "This command takes no arguments\n";
return ParseCode::CommandLineError;
}

status = handle_format_option(parser, &chosen_formatter, cerr);

return status;
}
6 changes: 5 additions & 1 deletion src/client/cli/cmd/version.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Canonical, Ltd.
* Copyright (C) 2017-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -24,6 +24,8 @@

namespace multipass
{
class Formatter;

namespace cmd
{
class Version final : public Command
Expand All @@ -38,6 +40,8 @@ class Version final : public Command

private:
ParseCode parse_args(ArgParser *parser) override;

Formatter* chosen_formatter;
};
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/client/cli/formatter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2018-2020 Canonical Ltd.
# Copyright © 2018-2021 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand All @@ -20,6 +20,7 @@ add_library(formatter STATIC
yaml_formatter.cpp)

target_link_libraries(formatter
client_common
fmt
rpc
yaml
Expand Down
14 changes: 13 additions & 1 deletion src/client/cli/formatter/csv_formatter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -100,3 +100,15 @@ std::string mp::CSVFormatter::format(const FindReply& reply) const

return fmt::to_string(buf);
}

std::string mp::CSVFormatter::format(const VersionReply& reply, const std::string& client_version) const
{
fmt::memory_buffer buf;

fmt::format_to(buf, "Multipass,Multipassd,Title,Description,URL\n");

fmt::format_to(buf, "{},{},{},{},{}\n", client_version, reply.version(), reply.update_info().title(),
reply.update_info().description(), reply.update_info().url());

return fmt::to_string(buf);
}
2 changes: 1 addition & 1 deletion src/client/cli/formatter/format_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
26 changes: 25 additions & 1 deletion src/client/cli/formatter/json_formatter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -17,6 +17,7 @@

#include <multipass/cli/json_formatter.h>

#include <multipass/cli/client_common.h>
#include <multipass/cli/format_utils.h>
#include <multipass/utils.h>

Expand Down Expand Up @@ -188,3 +189,26 @@ std::string mp::JsonFormatter::format(const FindReply& reply) const

return QString(QJsonDocument(find_json).toJson()).toStdString();
}

std::string mp::JsonFormatter::format(const VersionReply& reply, const std::string& client_version) const
{
QJsonObject version_json;

version_json.insert("multipass", QString::fromStdString(client_version));

if (!reply.version().empty())
{
version_json.insert("multipassd", QString::fromStdString(reply.version()));

if (mp::cmd::update_available(reply.update_info()))
{
QJsonObject update;
update.insert("title", QString::fromStdString(reply.update_info().title()));
update.insert("description", QString::fromStdString(reply.update_info().description()));
update.insert("url", QString::fromStdString(reply.update_info().url()));

version_json.insert("update", update);
}
}
return QString(QJsonDocument(version_json).toJson()).toStdString();
}
20 changes: 19 additions & 1 deletion src/client/cli/formatter/table_formatter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 Canonical, Ltd.
* Copyright (C) 2018-2021 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -17,6 +17,7 @@

#include <multipass/cli/table_formatter.h>

#include <multipass/cli/client_common.h>
#include <multipass/cli/format_utils.h>

#include <multipass/format.h>
Expand Down Expand Up @@ -224,3 +225,20 @@ std::string mp::TableFormatter::format(const FindReply& reply) const

return fmt::to_string(buf);
}

std::string mp::TableFormatter::format(const VersionReply& reply, const std::string& client_version) const
{
fmt::memory_buffer buf;
fmt::format_to(buf, "{:<12}{}\n", "multipass", client_version);

if (!reply.version().empty())
{
fmt::format_to(buf, "{:<12}{}\n", "multipassd", reply.version());

if (mp::cmd::update_available(reply.update_info()))
{
fmt::format_to(buf, "{}", mp::cmd::update_notice(reply.update_info()));
}
}
return fmt::to_string(buf);
}
Loading

0 comments on commit 2126b95

Please sign in to comment.