Skip to content

Commit ff79b07

Browse files
committed
traffic_ctl - Run a clean up on the output style parameters making it easy to use. 'all,req,resp' are now grouped into the '--format rpc' option.
1 parent 304ea26 commit ff79b07

File tree

5 files changed

+33
-55
lines changed

5 files changed

+33
-55
lines changed

doc/appendices/command-line/traffic_ctl_jsonrpc.en.rst

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ Options
8484
it's not required to always implement a pretty output
8585
``json`` It will show the response message formatted to `JSON`_. This is ideal if you want to redirect the stdout to a different source.
8686
It will only stream the json response, no other messages.
87-
``data:`` This is an addon to the default format style, data can be: ``{req|resp|all}`` which will make :program:`traffic_ctl`
88-
to print in json format the request or response or both.
87+
``rpc`` Show the JSONRPC request and response + the default output.
8988
=================== ========================================================================
9089

9190
In case of a record request(config) ``--records`` overrides this flag.
@@ -96,19 +95,10 @@ Options
9695

9796
.. code-block::
9897
99-
traffic_ctl config get variable --format data:req
100-
--> {request}
101-
102-
.. code-block::
103-
104-
$ traffic_ctl config get variable --format data:resp
105-
<-- {response}
106-
107-
.. code-block::
108-
109-
$ traffic_ctl config get variable --format data:all
98+
$ traffic_ctl config get variable --format rpc
11099
--> {request}
111100
<-- {response}
101+
variable 1234
112102
113103
.. code-block::
114104

doc/developer-guide/jsonrpc/jsonrpc-handler-development.en.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ please check the ``CustomJSONRPCResponse`` tester for more information.
366366
AddJsonRPCClientRequest
367367
~~~~~~~~~~~~~~~~~~~~~~~
368368

369-
This function will generate a json response as an output, internally it ses :program:`traffic_ctl file --format json` as client.
370-
The output can be used and compared with a gold file. This also provides schema validation for the entire JSONRPC protocol as well as
371-
the ``param`` field against a specific schema file. You can specify ``schema_file_name`` with a valid json schema file to validate
369+
This function will generate a json response as an output, internally it uses :program:`traffic_ctl rpc file --format json` as client.
370+
The output can be used and compared with a gold file. This also provides an optional schema validation for the entire JSONRPC protocol
371+
as well as the ``param`` field against a specific schema file. You can specify ``schema_file_name`` with a valid json schema file to validate
372372
the entire JSONRPC 2.0 request(except the content of the ``params`` field). You can also set ``params_field_schema_file_name`` with a
373373
valid json schema file to validate only the ``params`` field.
374374

src/traffic_ctl_jsonrpc/CtrlCommands.cc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,23 @@ namespace
2929
/// We use yamlcpp as codec implementation.
3030
using Codec = yamlcpp_json_emitter;
3131

32-
const std::unordered_map<std::string_view, BasePrinter::Options::Format> _Fmt_str_to_enum = {
33-
{"pretty", BasePrinter::Options::Format::PRETTY}, {"legacy", BasePrinter::Options::Format::LEGACY},
34-
{"json", BasePrinter::Options::Format::JSON}, {"req", BasePrinter::Options::Format::DATA_REQ},
35-
{"resp", BasePrinter::Options::Format::DATA_RESP}, {"all", BasePrinter::Options::Format::DATA_ALL}};
32+
const std::unordered_map<std::string_view, BasePrinter::Options::OutputFormat> _Fmt_str_to_enum = {
33+
{"pretty", BasePrinter::Options::OutputFormat::PRETTY},
34+
{"legacy", BasePrinter::Options::OutputFormat::LEGACY},
35+
{"json", BasePrinter::Options::OutputFormat::JSON},
36+
{"rpc", BasePrinter::Options::OutputFormat::RPC}};
3637

37-
BasePrinter::Options::Format
38+
BasePrinter::Options::OutputFormat
3839
parse_format(ts::Arguments &args)
3940
{
4041
if (args.get("records")) {
41-
return BasePrinter::Options::Format::RECORDS;
42+
return BasePrinter::Options::OutputFormat::RECORDS;
4243
}
4344

44-
BasePrinter::Options::Format val{BasePrinter::Options::Format::LEGACY};
45+
BasePrinter::Options::OutputFormat val{BasePrinter::Options::OutputFormat::LEGACY};
4546

4647
if (auto data = args.get("format"); data) {
4748
ts::TextView fmt{data.value()};
48-
if ("data" == fmt.prefix(':')) {
49-
fmt.take_prefix_at(':');
50-
}
5149
if (auto search = _Fmt_str_to_enum.find(fmt); search != std::end(_Fmt_str_to_enum)) {
5250
val = search->second;
5351
}
@@ -76,14 +74,14 @@ CtrlCommand::execute()
7674
std::string
7775
CtrlCommand::invoke_rpc(std::string const &request)
7876
{
79-
if (_printer->print_req_msg()) {
77+
if (_printer->print_rpc_message()) {
8078
std::string text;
8179
ts::bwprint(text, "--> {}", request);
8280
_printer->write_debug(std::string_view{text});
8381
}
8482
if (auto resp = _rpcClient.invoke(request); !resp.empty()) {
8583
// all good.
86-
if (_printer->print_resp_msg()) {
84+
if (_printer->print_rpc_message()) {
8785
std::string text;
8886
ts::bwprint(text, "<-- {}", resp);
8987
_printer->write_debug(std::string_view{text});
@@ -381,9 +379,9 @@ DirectRPCCommand::DirectRPCCommand(ts::Arguments args) : CtrlCommand(args)
381379
_invoked_func = [&]() { read_from_input(); };
382380
} else if (_arguments.get("invoke")) {
383381
_invoked_func = [&]() { invoke_method(); };
384-
if (printOpts._format == BasePrinter::Options::Format::LEGACY) {
382+
if (printOpts._format == BasePrinter::Options::OutputFormat::LEGACY) {
385383
// overwrite this and let it drop json instead.
386-
printOpts._format = BasePrinter::Options::Format::DATA_ALL;
384+
printOpts._format = BasePrinter::Options::OutputFormat::RPC;
387385
}
388386
}
389387

src/traffic_ctl_jsonrpc/CtrlPrinters.h

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,16 @@ class BasePrinter
3838
public:
3939
/// This enum maps the --format flag coming from traffic_ctl. (also --records is included here, see comments down below.)
4040
struct Options {
41-
enum class Format {
41+
enum class OutputFormat {
4242
LEGACY = 0, // Legacy format, mimics the old traffic_ctl output
4343
PRETTY, // Enhanced printing messages. (in case you would like to generate them)
4444
JSON, // Json formatting
4545
RECORDS, // only valid for configs, but it's handy to have it here.
46-
DATA_REQ, // Print json request + default format
47-
DATA_RESP, // Print json response + default format
48-
DATA_ALL // Print json request and response + default format
46+
RPC // Print JSONRPC request and response + default output.
4947
};
5048
Options() = default;
51-
Options(Format fmt) : _format(fmt) {}
52-
Format _format{Format::LEGACY}; //!< selected(passed) format.
49+
Options(OutputFormat fmt) : _format(fmt) {}
50+
OutputFormat _format{OutputFormat::LEGACY}; //!< selected(passed) format.
5351
};
5452

5553
/// Printer constructor. Needs the format as it will be used by derived classes.
@@ -81,10 +79,9 @@ class BasePrinter
8179
virtual void write_output(std::string_view output) const;
8280
virtual void write_debug(std::string_view output) const;
8381

84-
/// Format getters.
85-
Options::Format get_format() const;
86-
bool print_req_msg() const;
87-
bool print_resp_msg() const;
82+
/// OutputFormat getters.
83+
Options::OutputFormat get_format() const;
84+
bool print_rpc_message() const;
8885
bool is_json_format() const;
8986
bool is_legacy_format() const;
9087
bool is_records_format() const;
@@ -95,44 +92,38 @@ class BasePrinter
9592
Options _printOpt;
9693
};
9794

98-
inline BasePrinter::Options::Format
95+
inline BasePrinter::Options::OutputFormat
9996
BasePrinter::get_format() const
10097
{
10198
return _printOpt._format;
10299
}
103100

104101
inline bool
105-
BasePrinter::print_req_msg() const
102+
BasePrinter::print_rpc_message() const
106103
{
107-
return get_format() == Options::Format::DATA_ALL || get_format() == Options::Format::DATA_REQ;
108-
}
109-
110-
inline bool
111-
BasePrinter::print_resp_msg() const
112-
{
113-
return get_format() == Options::Format::DATA_ALL || get_format() == Options::Format::DATA_RESP;
104+
return get_format() == Options::OutputFormat::RPC;
114105
}
115106

116107
inline bool
117108
BasePrinter::is_json_format() const
118109
{
119-
return get_format() == Options::Format::JSON;
110+
return get_format() == Options::OutputFormat::JSON;
120111
}
121112

122113
inline bool
123114
BasePrinter::is_legacy_format() const
124115
{
125-
return get_format() == Options::Format::LEGACY;
116+
return get_format() == Options::OutputFormat::LEGACY;
126117
}
127118
inline bool
128119
BasePrinter::is_records_format() const
129120
{
130-
return get_format() == Options::Format::RECORDS;
121+
return get_format() == Options::OutputFormat::RECORDS;
131122
}
132123
inline bool
133124
BasePrinter::is_pretty_format() const
134125
{
135-
return get_format() == Options::Format::PRETTY;
126+
return get_format() == Options::OutputFormat::PRETTY;
136127
}
137128
//------------------------------------------------------------------------------------------------------------------------------------
138129
class GenericPrinter : public BasePrinter

src/traffic_ctl_jsonrpc/traffic_ctl.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ main(int argc, const char **argv)
5555
.add_option("--version", "-V", "Print version string")
5656
.add_option("--help", "-h", "Print usage information")
5757
.add_option("--run-root", "", "using TS_RUNROOT as sandbox", "TS_RUNROOT", 1)
58-
.add_option("--format", "-f", "Use a specific output format {legacy|pretty|json|data:{req|resp|all}}", "", 1, "legacy",
59-
"format");
58+
.add_option("--format", "-f", "Use a specific output format {legacy|pretty|json|rpc}", "", 1, "legacy", "format");
6059

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

0 commit comments

Comments
 (0)