Skip to content

Commit

Permalink
Merge pull request #3176 from canonical/truncate-snapshot-comment
Browse files Browse the repository at this point in the history
[snapshots] truncate snapshot comment on newline in overview

r=ricab a=sharder996
  • Loading branch information
ricab committed Oct 30, 2023
2 parents 3e344b7 + 1b6cd80 commit e61166c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/client/cli/formatter/csv_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ std::string generate_snapshot_overview_report(const mp::InfoReply& reply)
for (const auto& item : mp::format::sort_snapshots(reply.snapshot_overview().overview()))
{
const auto& snapshot = item.fundamentals();
fmt::format_to(std::back_inserter(buf), "{},{},{},{}\n", item.instance_name(), snapshot.snapshot_name(),
fmt::format_to(std::back_inserter(buf), "{},{},{},\"{}\"\n", item.instance_name(), snapshot.snapshot_name(),
snapshot.parent(), snapshot.comment());
}

Expand Down
10 changes: 8 additions & 2 deletions src/client/cli/formatter/table_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace mp = multipass;

namespace
{
const std::regex newline("(\r\n|\n)");

std::string format_images(const google::protobuf::RepeatedPtrField<mp::FindReply_ImageInfo>& images_info,
std::string type)
{
Expand Down Expand Up @@ -95,7 +97,6 @@ std::string generate_snapshot_details(const mp::DetailedInfoItem& item)
}

// TODO@snapshots split and align string if it extends onto several lines
std::regex newline("(\r\n|\n)");
fmt::format_to(std::back_inserter(buf), "{:<16}{}\n", "Comment:",
fundamentals.comment().empty()
? "--"
Expand Down Expand Up @@ -238,7 +239,6 @@ std::string generate_snapshot_overview_report(const mp::InfoReply& reply)
const auto parent_column_width = mp::format::column_width(
overview.begin(), overview.end(), [](const auto& item) -> int { return item.fundamentals().parent().length(); },
parent_col_header.length());
const auto max_comment_column_width = 50;

const auto row_format = "{:<{}}{:<{}}{:<{}}{:<}\n";

Expand All @@ -247,7 +247,13 @@ std::string generate_snapshot_overview_report(const mp::InfoReply& reply)

for (const auto& item : mp::format::sort_snapshots(overview))
{
size_t max_comment_column_width = 50;
std::smatch match;
auto snapshot = item.fundamentals();

if (std::regex_search(snapshot.comment().begin(), snapshot.comment().end(), match, newline))
max_comment_column_width = std::min((size_t)(match.position(1)) + 1, max_comment_column_width);

fmt::format_to(std::back_inserter(buf), row_format, item.instance_name(), name_column_width,
snapshot.snapshot_name(), snapshot_column_width,
snapshot.parent().empty() ? "--" : snapshot.parent(), parent_column_width,
Expand Down
19 changes: 10 additions & 9 deletions tests/test_output_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ auto construct_multiple_snapshot_overview_info_reply()
fundamentals = snapshot_entry->mutable_fundamentals();
snapshot_entry->set_instance_name("prosperous-spadefish");
fundamentals->set_snapshot_name("snapshot2");
fundamentals->set_comment("Before restoring snap1");
fundamentals->set_comment("Before restoring snap1\nContains a newline that\r\nshould be truncated");
timestamp.set_seconds(1671840000);
fundamentals->mutable_creation_timestamp()->CopyFrom(timestamp);

Expand Down Expand Up @@ -985,7 +985,7 @@ const std::vector<FormatterParamType> orderable_list_info_formatter_outputs{
"hale-roller pristine -- A first snapshot\n"
"hale-roller rocking pristine A very long comment that should be truncated by t…\n"
"hale-roller rolling pristine Loaded with stuff\n"
"prosperous-spadefish snapshot2 -- Before restoring snap1\n"
"prosperous-spadefish snapshot2 -- Before restoring snap1\n"
"prosperous-spadefish snapshot10 snapshot2 --\n",
"table_snapshot_overview_multiple"},

Expand Down Expand Up @@ -1030,12 +1030,13 @@ const std::vector<FormatterParamType> orderable_list_info_formatter_outputs{
"ab5191cc172564e7cc0eafd397312a32598823e645279c820f0935393aead509,18.04 LTS,,,,,,,\"\";,,3\n",
"csv_info_multiple_instances"},
{&csv_formatter, &single_snapshot_overview_info_reply,
"Instance,Snapshot,Parent,Comment\nfoo,snapshot1,,This is a sample comment\n", "csv_snapshot_overview_single"},
"Instance,Snapshot,Parent,Comment\nfoo,snapshot1,,\"This is a sample comment\"\n", "csv_snapshot_overview_single"},
{&csv_formatter, &multiple_snapshot_overview_info_reply,
"Instance,Snapshot,Parent,Comment\nhale-roller,pristine,,A first "
"snapshot\nhale-roller,rocking,pristine,A very long comment that should be truncated by the table "
"formatter\nhale-roller,rolling,pristine,Loaded with stuff\nprosperous-spadefish,snapshot2,,Before restoring "
"snap1\nprosperous-spadefish,snapshot10,snapshot2,\n",
"Instance,Snapshot,Parent,Comment\nhale-roller,pristine,,\"A first "
"snapshot\"\nhale-roller,rocking,pristine,\"A very long comment that should be truncated by the table "
"formatter\"\nhale-roller,rolling,pristine,\"Loaded with stuff\"\nprosperous-spadefish,snapshot2,,\"Before "
"restoring snap1\nContains a newline that\r\nshould be "
"truncated\"\nprosperous-spadefish,snapshot10,snapshot2,\"\"\n",
"csv_snapshot_overview_multiple"},

{&yaml_formatter, &empty_list_reply, "\n", "yaml_list_empty"},
Expand Down Expand Up @@ -1194,7 +1195,7 @@ const std::vector<FormatterParamType> orderable_list_info_formatter_outputs{
"prosperous-spadefish:\n"
" - snapshot2:\n"
" - parent: ~\n"
" comment: Before restoring snap1\n"
" comment: \"Before restoring snap1\\nContains a newline that\\r\\nshould be truncated\"\n"
" - snapshot10:\n"
" - parent: snapshot2\n"
" comment: ~\n",
Expand Down Expand Up @@ -1418,7 +1419,7 @@ const std::vector<FormatterParamType> non_orderable_list_info_formatter_outputs{
" \"parent\": \"snapshot2\"\n"
" },\n"
" \"snapshot2\": {\n"
" \"comment\": \"Before restoring snap1\",\n"
" \"comment\": \"Before restoring snap1\\nContains a newline that\\r\\nshould be truncated\",\n"
" \"parent\": \"\"\n"
" }\n"
" }\n"
Expand Down

0 comments on commit e61166c

Please sign in to comment.