From 992829d38cb89732f6994642c1bdaa2448e610b7 Mon Sep 17 00:00:00 2001 From: songweibin Date: Wed, 9 Aug 2017 14:26:32 +0800 Subject: [PATCH] rbd: adding newline operation when only '--format' used Signed-off-by: songweibin --- src/common/Formatter.cc | 4 ++++ src/common/Formatter.h | 6 ++++++ src/mgr/PyFormatter.h | 2 +- src/rgw/rgw_formats.h | 1 + src/test/cli-integration/rbd/formatted-output.t | 4 ++-- src/tools/rbd/Utils.cc | 2 ++ src/tools/rbd/action/DiskUsage.cc | 2 +- 7 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 1a400aaa159bc..3b3589db805ca 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -127,6 +127,8 @@ void JSONFormatter::flush(std::ostream& os) { finish_pending_string(); os << m_ss.str(); + if (m_line_break_enabled) + os << "\n"; m_ss.clear(); m_ss.str(""); } @@ -326,6 +328,8 @@ void XMLFormatter::flush(std::ostream& os) * we should NOT output a newline. This primarily triggers on HTTP redirects */ if (m_pretty && !m_ss_str.empty()) os << "\n"; + else if (m_line_break_enabled) + os << "\n"; m_ss.clear(); m_ss.str(""); } diff --git a/src/common/Formatter.h b/src/common/Formatter.h index aa695ba30f546..df6c0a99a5eba 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -37,6 +37,7 @@ namespace ceph { Formatter(); virtual ~Formatter(); + virtual void enable_line_break() = 0; virtual void flush(std::ostream& os) = 0; void flush(bufferlist &bl); virtual void reset() = 0; @@ -93,6 +94,7 @@ namespace ceph { void set_status(int status, const char* status_name) override {}; void output_header() override {}; void output_footer() override {}; + void enable_line_break() override { m_line_break_enabled = true; } void flush(std::ostream& os) override; using Formatter::flush; // don't hide Formatter::flush(bufferlist &bl) void reset() override; @@ -128,6 +130,7 @@ namespace ceph { std::stringstream m_ss, m_pending_string; std::list m_stack; bool m_is_pending_string; + bool m_line_break_enabled = false; }; class XMLFormatter : public Formatter { @@ -139,6 +142,7 @@ namespace ceph { void output_header() override; void output_footer() override; + void enable_line_break() override { m_line_break_enabled = true; } void flush(std::ostream& os) override; using Formatter::flush; // don't hide Formatter::flush(bufferlist &bl) void reset() override; @@ -176,6 +180,7 @@ namespace ceph { const bool m_underscored; std::string m_pending_string_name; bool m_header_done; + bool m_line_break_enabled = false; }; class TableFormatter : public Formatter { @@ -185,6 +190,7 @@ namespace ceph { void set_status(int status, const char* status_name) override {}; void output_header() override {}; void output_footer() override {}; + void enable_line_break() override {}; void flush(std::ostream& os) override; using Formatter::flush; // don't hide Formatter::flush(bufferlist &bl) void reset() override; diff --git a/src/mgr/PyFormatter.h b/src/mgr/PyFormatter.h index 924f5822abe80..ec4dad81c2901 100644 --- a/src/mgr/PyFormatter.h +++ b/src/mgr/PyFormatter.h @@ -76,7 +76,7 @@ public: void set_status(int status, const char* status_name) override {} void output_header() override {}; void output_footer() override {}; - + void enable_line_break() override {}; void open_array_section(const char *name) override; void open_object_section(const char *name) override; diff --git a/src/rgw/rgw_formats.h b/src/rgw/rgw_formats.h index 6c7d006282baf..a50965873836f 100644 --- a/src/rgw/rgw_formats.h +++ b/src/rgw/rgw_formats.h @@ -29,6 +29,7 @@ public: void set_status(int status, const char* status_name) override {}; void output_header() override {}; void output_footer() override {}; + void enable_line_break() override {}; void flush(ostream& os) override; void reset() override; diff --git a/src/test/cli-integration/rbd/formatted-output.t b/src/test/cli-integration/rbd/formatted-output.t index aefdbf91e344a..b51326ac76d90 100644 --- a/src/test/cli-integration/rbd/formatted-output.t +++ b/src/test/cli-integration/rbd/formatted-output.t @@ -5,9 +5,9 @@ ls on empty pool never containing images $ rados -p rbd rm rbd_directory >/dev/null 2>&1 || true $ rbd ls $ rbd ls --format json - [] (no-eol) + [] $ rbd ls --format xml - (no-eol) + create ======= diff --git a/src/tools/rbd/Utils.cc b/src/tools/rbd/Utils.cc index ccb850e876aba..937dddb2126ea 100644 --- a/src/tools/rbd/Utils.cc +++ b/src/tools/rbd/Utils.cc @@ -831,6 +831,8 @@ int get_formatter(const po::variables_map &vm, std::cerr << "rbd: --pretty-format only works when --format " << "is json or xml" << std::endl; return -EINVAL; + } else if (*formatter != nullptr && !pretty) { + formatter->get()->enable_line_break(); } } else if (vm[at::PRETTY_FORMAT].as()) { std::cerr << "rbd: --pretty-format only works when --format " diff --git a/src/tools/rbd/action/DiskUsage.cc b/src/tools/rbd/action/DiskUsage.cc index 3a38a052fb3d2..9934e81dd21fe 100644 --- a/src/tools/rbd/action/DiskUsage.cc +++ b/src/tools/rbd/action/DiskUsage.cc @@ -283,7 +283,7 @@ int execute(const po::variables_map &vm) { from_snap_name.empty() ? nullptr : from_snap_name.c_str(), formatter.get()); if (r < 0) { - std::cerr << "du failed: " << cpp_strerror(r) << std::endl; + std::cerr << "rbd: du failed: " << cpp_strerror(r) << std::endl; return r; } return 0; -- 2.39.5