Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions doc/appendices/command-line/traffic_ctl.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,15 @@ Options
=================== ========================================================================
Options Description
=================== ========================================================================
``legacy`` Will honour the old :program:`traffic_ctl` output messages. This is the default format type.
``pretty`` <if available> will print a different output, a prettier output. This depends on the implementation,
it's not required to always implement a pretty output
``json`` It will show the response message formatted to `JSON`_. This is ideal if you want to redirect the stdout to a different source.
It will only stream the json response, no other messages.
This option only applies to the RPC request or response.
``rpc`` Show the JSONRPC request and response + the default output.
This option only applies to the RPC request or response.
=================== ========================================================================

In case of a record request(config) ``--records`` overrides this flag.

Default: ``legacy``

Example:

.. code-block::
Expand Down Expand Up @@ -148,8 +145,6 @@ traffic_ctl config
Display all the known information about a configuration record. This includes the current and
default values, the data type, the record class and syntax checking expression.

Error output available if ``--format pretty`` is specified.

.. program:: traffic_ctl config
.. option:: diff [--records]

Expand All @@ -165,8 +160,6 @@ traffic_ctl config

Display the current value of a configuration record.

Error output available if ``--format pretty`` is specified.

.. program:: traffic_ctl config get
.. option:: --records

Expand Down Expand Up @@ -304,8 +297,6 @@ traffic_ctl metric

Display the current value of the specified statistics.

Error output available if ``--format pretty`` is specified.

.. program:: traffic_ctl metric
.. option:: match REGEX [REGEX...]

Expand All @@ -328,8 +319,6 @@ traffic_ctl metric

Display all the known information about a metric record.

Error output available if ``--format pretty`` is specified.

.. program:: traffic_ctl metric
.. option:: monitor [-i, -c] METRIC [METRIC...]

Expand Down
10 changes: 4 additions & 6 deletions src/traffic_ctl/CtrlCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ namespace
using Codec = yamlcpp_json_emitter;
} // namespace
const std::unordered_map<std::string_view, BasePrinter::Options::OutputFormat> _Fmt_str_to_enum = {
{"pretty", BasePrinter::Options::OutputFormat::PRETTY},
{"legacy", BasePrinter::Options::OutputFormat::LEGACY},
{"json", BasePrinter::Options::OutputFormat::JSON },
{"rpc", BasePrinter::Options::OutputFormat::RPC }
{"json", BasePrinter::Options::OutputFormat::JSON},
{"rpc", BasePrinter::Options::OutputFormat::RPC }
};

BasePrinter::Options::OutputFormat
Expand All @@ -50,7 +48,7 @@ parse_format(ts::Arguments *args)
return BasePrinter::Options::OutputFormat::RECORDS;
}

BasePrinter::Options::OutputFormat val{BasePrinter::Options::OutputFormat::LEGACY};
BasePrinter::Options::OutputFormat val{BasePrinter::Options::OutputFormat::NOT_SET};

if (auto data = args->get("format"); data) {
ts::TextView fmt{data.value()};
Expand Down Expand Up @@ -457,7 +455,7 @@ DirectRPCCommand::DirectRPCCommand(ts::Arguments *args) : CtrlCommand(args)
_invoked_func = [&]() { read_from_input(); };
} else if (get_parsed_arguments()->get(INVOKE_STR)) {
_invoked_func = [&]() { invoke_method(); };
if (printOpts._format == BasePrinter::Options::OutputFormat::LEGACY) {
if (printOpts._format == BasePrinter::Options::OutputFormat::NOT_SET) {
// overwrite this and let it drop json instead.
printOpts._format = BasePrinter::Options::OutputFormat::RPC;
}
Expand Down
63 changes: 4 additions & 59 deletions src/traffic_ctl/CtrlPrinters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,7 @@ BasePrinter::write_output_json(YAML::Node const &node) const
void
RecordPrinter::write_output(YAML::Node const &result)
{
auto response = result.as<shared::rpc::RecordLookUpResponse>();
if (is_legacy_format()) {
write_output_legacy(response);
} else {
write_output_pretty(response);
}
}
void
RecordPrinter::write_output_legacy(shared::rpc::RecordLookUpResponse const &response)
{
auto const &response = result.as<shared::rpc::RecordLookUpResponse>();
std::string text;
for (auto &&recordInfo : response.recordList) {
if (!recordInfo.registered) {
Expand All @@ -123,11 +114,6 @@ RecordPrinter::write_output_legacy(shared::rpc::RecordLookUpResponse const &resp
// we print errors if found.
print_record_error_list(response.errorList);
}
void
RecordPrinter::write_output_pretty(shared::rpc::RecordLookUpResponse const &response)
{
write_output_legacy(response);
}
//------------------------------------------------------------------------------------------------------------------------------------
void
MetricRecordPrinter::write_output(YAML::Node const &result)
Expand Down Expand Up @@ -169,19 +155,11 @@ ConfigReloadPrinter::write_output(YAML::Node const &result)
void
ConfigShowFileRegistryPrinter::write_output(YAML::Node const &result)
{
if (is_pretty_format()) {
this->write_output_pretty(result);
} else {
if (auto registry = result["config_registry"]) {
if (auto &&registry = result["config_registry"]) {
if (is_json_format()) {
write_output_json(registry);
return;
}
}
}

void
ConfigShowFileRegistryPrinter::write_output_pretty(YAML::Node const &result)
{
if (auto &&registry = result["config_registry"]) {
for (auto &&element : registry) {
std::cout << "┌ " << element["file_path"] << '\n';
std::cout << "└┬ Config name: " << element["config_record_name"] << '\n';
Expand All @@ -195,7 +173,6 @@ ConfigShowFileRegistryPrinter::write_output_pretty(YAML::Node const &result)
void
ConfigSetPrinter::write_output(YAML::Node const &result)
{
// we match the legacy format, the only one supported for now.
static const std::unordered_map<std::string, std::string> Update_Type_To_String_Message = {
{"0", "Set {}" }, // UNDEFINED
{"1", "Set {}, please wait 10 seconds for traffic server to sync configuration, restart is not required"}, // DYNAMIC
Expand Down Expand Up @@ -223,16 +200,6 @@ void
RecordDescribePrinter::write_output(YAML::Node const &result)
{
auto const &response = result.as<shared::rpc::RecordLookUpResponse>();
if (is_legacy_format()) {
write_output_legacy(response);
} else {
write_output_pretty(response);
}
}

void
RecordDescribePrinter::write_output_legacy(shared::rpc::RecordLookUpResponse const &response)
{
std::string text;
for (auto &&recordInfo : response.recordList) {
if (!recordInfo.registered) {
Expand Down Expand Up @@ -272,13 +239,6 @@ RecordDescribePrinter::write_output_legacy(shared::rpc::RecordLookUpResponse con
// also print errors.
print_record_error_list(response.errorList);
}

void
RecordDescribePrinter::write_output_pretty(shared::rpc::RecordLookUpResponse const &response)
{
// we default for legacy.
write_output_legacy(response);
}
//------------------------------------------------------------------------------------------------------------------------------------
void
GetHostStatusPrinter::write_output(YAML::Node const &result)
Expand Down Expand Up @@ -307,14 +267,6 @@ SetHostStatusPrinter::write_output(YAML::Node const &result)

void
CacheDiskStoragePrinter::write_output(YAML::Node const &result)
{
// do nothing.
if (!is_legacy_format()) {
write_output_pretty(result);
}
}
void
CacheDiskStoragePrinter::write_output_pretty(YAML::Node const &result)
{
auto my_print = [](auto const &disk) {
std::cout << "Device: " << disk.path << '\n';
Expand All @@ -334,13 +286,6 @@ CacheDiskStoragePrinter::write_output_pretty(YAML::Node const &result)
//------------------------------------------------------------------------------------------------------------------------------------
void
CacheDiskStorageOfflinePrinter::write_output(YAML::Node const &result)
{
if (!is_legacy_format()) {
write_output_pretty(result);
}
}
void
CacheDiskStorageOfflinePrinter::write_output_pretty(YAML::Node const &result)
{
for (auto &&item : result) {
if (auto n = item["has_online_storage_left"]) {
Expand Down
32 changes: 5 additions & 27 deletions src/traffic_ctl/CtrlPrinters.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ class BasePrinter
/// This enum maps the --format flag coming from traffic_ctl. (also --records is included here, see comments down below.)
struct Options {
enum class OutputFormat {
LEGACY = 0, // Legacy format, mimics the old traffic_ctl output
PRETTY, // Enhanced printing messages. (in case you would like to generate them)
JSON, // Json formatting
RECORDS, // only valid for configs, but it's handy to have it here.
RPC // Print JSONRPC request and response + default output.
NOT_SET = 0, // nothing set.
JSON, // Json formatting
RECORDS, // only valid for configs, but it's handy to have it here.
RPC // Print JSONRPC request and response + default output.
};
Options() = default;
Options(OutputFormat fmt) : _format(fmt) {}
OutputFormat _format{OutputFormat::LEGACY}; //!< selected(passed) format.
OutputFormat _format{OutputFormat::NOT_SET}; //!< selected(passed) format.
};

/// Printer constructor. Needs the format as it will be used by derived classes.
Expand Down Expand Up @@ -83,9 +82,7 @@ class BasePrinter
Options::OutputFormat get_format() const;
bool print_rpc_message() const;
bool is_json_format() const;
bool is_legacy_format() const;
bool is_records_format() const;
bool is_pretty_format() const;

protected:
void write_output_json(YAML::Node const &node) const;
Expand All @@ -110,21 +107,11 @@ BasePrinter::is_json_format() const
return get_format() == Options::OutputFormat::JSON;
}

inline bool
BasePrinter::is_legacy_format() const
{
return get_format() == Options::OutputFormat::LEGACY;
}
inline bool
BasePrinter::is_records_format() const
{
return get_format() == Options::OutputFormat::RECORDS;
}
inline bool
BasePrinter::is_pretty_format() const
{
return get_format() == Options::OutputFormat::PRETTY;
}
//------------------------------------------------------------------------------------------------------------------------------------
class GenericPrinter : public BasePrinter
{
Expand All @@ -141,8 +128,6 @@ class GenericPrinter : public BasePrinter
class RecordPrinter : public BasePrinter
{
void write_output(YAML::Node const &result) override;
void write_output_legacy(shared::rpc::RecordLookUpResponse const &result);
void write_output_pretty(shared::rpc::RecordLookUpResponse const &result);

public:
RecordPrinter(Options opt) : BasePrinter(opt) { _printAsRecords = is_records_format(); }
Expand All @@ -162,7 +147,6 @@ class MetricRecordPrinter : public BasePrinter
class DiffConfigPrinter : public RecordPrinter
{
void write_output(YAML::Node const &result) override;
void write_output_pretty(YAML::Node const &result);

public:
DiffConfigPrinter(BasePrinter::Options opt) : RecordPrinter(opt) {}
Expand All @@ -171,7 +155,6 @@ class DiffConfigPrinter : public RecordPrinter
class ConfigReloadPrinter : public BasePrinter
{
void write_output(YAML::Node const &result) override;
void write_output_pretty(YAML::Node const &result);

public:
ConfigReloadPrinter(BasePrinter::Options opt) : BasePrinter(opt) {}
Expand All @@ -180,7 +163,6 @@ class ConfigReloadPrinter : public BasePrinter
class ConfigShowFileRegistryPrinter : public BasePrinter
{
void write_output(YAML::Node const &result) override;
void write_output_pretty(YAML::Node const &result);

public:
using BasePrinter::BasePrinter;
Expand All @@ -196,8 +178,6 @@ class ConfigSetPrinter : public BasePrinter
//------------------------------------------------------------------------------------------------------------------------------------
class RecordDescribePrinter : public BasePrinter
{
void write_output_legacy(shared::rpc::RecordLookUpResponse const &result);
void write_output_pretty(shared::rpc::RecordLookUpResponse const &result);
void write_output(YAML::Node const &result) override;

public:
Expand All @@ -222,7 +202,6 @@ class SetHostStatusPrinter : public BasePrinter
//------------------------------------------------------------------------------------------------------------------------------------
class CacheDiskStoragePrinter : public BasePrinter
{
void write_output_pretty(YAML::Node const &result);
void write_output(YAML::Node const &result) override;

public:
Expand All @@ -232,7 +211,6 @@ class CacheDiskStoragePrinter : public BasePrinter
class CacheDiskStorageOfflinePrinter : public BasePrinter
{
void write_output(YAML::Node const &result) override;
void write_output_pretty(YAML::Node const &result);

public:
CacheDiskStorageOfflinePrinter(BasePrinter::Options opt) : BasePrinter(opt) {}
Expand Down
2 changes: 1 addition & 1 deletion src/traffic_ctl/traffic_ctl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ main(int argc, const char **argv)
.add_option("--version", "-V", "Print version string")
.add_option("--help", "-h", "Print usage information")
.add_option("--run-root", "", "using TS_RUNROOT as sandbox", "TS_RUNROOT", 1)
.add_option("--format", "-f", "Use a specific output format {legacy|pretty|json|rpc}", "", 1, "legacy", "format");
.add_option("--format", "-f", "Use a specific output format {json|rpc}", "", 1, "", "format");

auto &config_command = parser.add_command("config", "Manipulate configuration records").require_commands();
auto &metric_command = parser.add_command("metric", "Manipulate performance metrics").require_commands();
Expand Down