trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zw...@apache.org
Subject [trafficserver] branch master updated: add custom --help and -h flag to traffic_ctl
Date Mon, 12 Mar 2018 18:13:48 GMT
This is an automated email from the ASF dual-hosted git repository.

zwoop pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new aa887f6  add custom --help and -h flag to traffic_ctl
aa887f6 is described below

commit aa887f6d0a6dc86a42f5b2892872b325e8ee0efe
Author: Bill Xun <bill.j.xun@gmail.com>
AuthorDate: Thu Mar 8 16:16:53 2018 -0600

    add custom --help and -h flag to traffic_ctl
---
 cmd/traffic_ctl/traffic_ctl.cc | 152 ++++++++++++++++++-----------------------
 1 file changed, 68 insertions(+), 84 deletions(-)

diff --git a/cmd/traffic_ctl/traffic_ctl.cc b/cmd/traffic_ctl/traffic_ctl.cc
index 32f59d9..ebcb929 100644
--- a/cmd/traffic_ctl/traffic_ctl.cc
+++ b/cmd/traffic_ctl/traffic_ctl.cc
@@ -23,34 +23,20 @@
 
 #include "traffic_ctl.h"
 
-#include "ts/I_Layout.h"
 #include "I_RecProcess.h"
 #include "RecordsConfig.h"
+#include "ts/I_Layout.h"
 #include "ts/runroot.h"
 
 AppVersionInfo CtrlVersionInfo;
 
-const char *
-CtrlMgmtRecord::name() const
-{
-  return this->ele->rec_name;
-}
+const char *CtrlMgmtRecord::name() const { return this->ele->rec_name; }
 
-TSRecordT
-CtrlMgmtRecord::type() const
-{
-  return this->ele->rec_type;
-}
+TSRecordT CtrlMgmtRecord::type() const { return this->ele->rec_type; }
 
-int
-CtrlMgmtRecord::rclass() const
-{
-  return this->ele->rec_class;
-}
+int CtrlMgmtRecord::rclass() const { return this->ele->rec_class; }
 
-int64_t
-CtrlMgmtRecord::as_int() const
-{
+int64_t CtrlMgmtRecord::as_int() const {
   switch (this->ele->rec_type) {
   case TS_REC_INT:
     return this->ele->valueT.int_val;
@@ -61,43 +47,35 @@ CtrlMgmtRecord::as_int() const
   }
 }
 
-TSMgmtError
-CtrlMgmtRecord::fetch(const char *name)
-{
+TSMgmtError CtrlMgmtRecord::fetch(const char *name) {
   return TSRecordGet(name, this->ele);
 }
 
-TSMgmtError
-CtrlMgmtRecordList::match(const char *name)
-{
+TSMgmtError CtrlMgmtRecordList::match(const char *name) {
   return TSRecordGetMatchMlt(name, this->list);
 }
 
-CtrlMgmtRecordValue::CtrlMgmtRecordValue(const CtrlMgmtRecord &rec)
-{
+CtrlMgmtRecordValue::CtrlMgmtRecordValue(const CtrlMgmtRecord &rec) {
   this->init(rec.ele->rec_type, rec.ele->valueT);
 }
 
-CtrlMgmtRecordValue::CtrlMgmtRecordValue(const TSRecordEle *ele)
-{
+CtrlMgmtRecordValue::CtrlMgmtRecordValue(const TSRecordEle *ele) {
   this->init(ele->rec_type, ele->valueT);
 }
 
-CtrlMgmtRecordValue::CtrlMgmtRecordValue(TSRecordT _t, TSRecordValueT _v)
-{
+CtrlMgmtRecordValue::CtrlMgmtRecordValue(TSRecordT _t, TSRecordValueT _v) {
   this->init(_t, _v);
 }
 
-void
-CtrlMgmtRecordValue::init(TSRecordT _t, TSRecordValueT _v)
-{
+void CtrlMgmtRecordValue::init(TSRecordT _t, TSRecordValueT _v) {
   this->rec_type = _t;
   switch (this->rec_type) {
   case TS_REC_INT:
     snprintf(this->fmt.nbuf, sizeof(this->fmt.nbuf), "%" PRId64, _v.int_val);
     break;
   case TS_REC_COUNTER:
-    snprintf(this->fmt.nbuf, sizeof(this->fmt.nbuf), "%" PRId64, _v.counter_val);
+    snprintf(this->fmt.nbuf, sizeof(this->fmt.nbuf), "%" PRId64,
+             _v.counter_val);
     break;
   case TS_REC_FLOAT:
     snprintf(this->fmt.nbuf, sizeof(this->fmt.nbuf), "%f", _v.float_val);
@@ -110,14 +88,12 @@ CtrlMgmtRecordValue::init(TSRecordT _t, TSRecordValueT _v)
     }
     break;
   default:
-    rec_type      = TS_REC_STRING;
+    rec_type = TS_REC_STRING;
     this->fmt.str = "(invalid)";
   }
 }
 
-const char *
-CtrlMgmtRecordValue::c_str() const
-{
+const char *CtrlMgmtRecordValue::c_str() const {
   switch (this->rec_type) {
   case TS_REC_STRING:
     return this->fmt.str;
@@ -126,9 +102,7 @@ CtrlMgmtRecordValue::c_str() const
   }
 }
 
-void
-CtrlMgmtError(TSMgmtError err, const char *fmt, ...)
-{
+void CtrlMgmtError(TSMgmtError err, const char *fmt, ...) {
   ats_scoped_str msg(TSGetErrorMessage(err));
 
   if (fmt) {
@@ -145,13 +119,14 @@ CtrlMgmtError(TSMgmtError err, const char *fmt, ...)
   }
 }
 
-int
-CtrlSubcommandUsage(const char *name, const subcommand *cmds, unsigned ncmds, const ArgumentDescription
*desc, unsigned ndesc)
-{
+int CtrlSubcommandUsage(const char *name, const subcommand *cmds,
+                        unsigned ncmds, const ArgumentDescription *desc,
+                        unsigned ndesc) {
   const char *opt = ndesc ? "[OPTIONS]" : "";
   const char *sep = (ndesc && name) ? " " : "";
 
-  fprintf(stderr, "Usage: traffic_ctl %s%s%s CMD [ARGS ...]\n\nSubcommands:\n", name ? name
: "", sep, opt);
+  fprintf(stderr, "Usage: traffic_ctl %s%s%s CMD [ARGS ...]\n\nSubcommands:\n",
+          name ? name : "", sep, opt);
 
   for (unsigned i = 0; i < ncmds; ++i) {
     fprintf(stderr, "    %-16s%s\n", cmds[i].name, cmds[i].help);
@@ -164,9 +139,8 @@ CtrlSubcommandUsage(const char *name, const subcommand *cmds, unsigned
ncmds, co
   return CTRL_EX_USAGE;
 }
 
-int
-CtrlCommandUsage(const char *msg, const ArgumentDescription *desc, unsigned ndesc)
-{
+int CtrlCommandUsage(const char *msg, const ArgumentDescription *desc,
+                     unsigned ndesc) {
   fprintf(stderr, "Usage: traffic_ctl %s\n", msg);
 
   if (ndesc) {
@@ -176,23 +150,19 @@ CtrlCommandUsage(const char *msg, const ArgumentDescription *desc, unsigned
ndes
   return CTRL_EX_USAGE;
 }
 
-bool
-CtrlProcessArguments(int /* argc */, const char **argv, const ArgumentDescription *desc,
unsigned ndesc)
-{
+bool CtrlProcessArguments(int /* argc */, const char **argv,
+                          const ArgumentDescription *desc, unsigned ndesc) {
   n_file_arguments = 0;
   return process_args_ex(&CtrlVersionInfo, desc, ndesc, argv);
 }
 
-int
-CtrlUnimplementedCommand(unsigned /* argc */, const char **argv)
-{
+int CtrlUnimplementedCommand(unsigned /* argc */, const char **argv) {
   CtrlDebug("the '%s' command is not implemented", *argv);
   return CTRL_EX_UNIMPLEMENTED;
 }
 
-int
-CtrlGenericSubcommand(const char *name, const subcommand *cmds, unsigned ncmds, unsigned
argc, const char **argv)
-{
+int CtrlGenericSubcommand(const char *name, const subcommand *cmds,
+                          unsigned ncmds, unsigned argc, const char **argv) {
   CtrlCommandLine cmdline;
 
   // Process command line arguments and dump into variables
@@ -211,47 +181,56 @@ CtrlGenericSubcommand(const char *name, const subcommand *cmds, unsigned
ncmds,
   return CtrlSubcommandUsage(name, cmds, ncmds, nullptr, 0);
 }
 
-int
-main(int argc, const char **argv)
-{
-  CtrlCommandLine cmdline;
-  int debug = false;
-
-  CtrlVersionInfo.setup(PACKAGE_NAME, "traffic_ctl", PACKAGE_VERSION, __DATE__, __TIME__,
BUILD_MACHINE, BUILD_PERSON, "");
-  program_name = CtrlVersionInfo.AppStr;
-
-  ArgumentDescription argument_descriptions[] = {
-    {"debug", '-', "Enable debugging output", "F", &debug, nullptr, nullptr},
-    HELP_ARGUMENT_DESCRIPTION(),
-    VERSION_ARGUMENT_DESCRIPTION(),
-    RUNROOT_ARGUMENT_DESCRIPTION(),
-  };
-
-  const subcommand commands[] = {
+static const subcommand commands[] = {
     {subcommand_alarm, "alarm", "Manipulate alarms"},
     {subcommand_config, "config", "Manipulate configuration records"},
     {subcommand_metric, "metric", "Manipulate performance metrics"},
     {subcommand_server, "server", "Stop, restart and examine the server"},
     {subcommand_storage, "storage", "Manipulate cache storage"},
     {subcommand_plugin, "plugin", "Interact with plugins"},
+};
+
+int main(int argc, const char **argv) {
+  CtrlCommandLine cmdline;
+  int debug = false;
+
+  CtrlVersionInfo.setup(PACKAGE_NAME, "traffic_ctl", PACKAGE_VERSION, __DATE__,
+                        __TIME__, BUILD_MACHINE, BUILD_PERSON, "");
+  program_name = CtrlVersionInfo.AppStr;
+
+  ArgumentDescription argument_descriptions[] = {
+      {"debug", '-', "Enable debugging output", "F", &debug, nullptr, nullptr},
+      {"help", 'h', "Print usage information", nullptr, nullptr, nullptr,
+       [](const ArgumentDescription *args, unsigned nargs,
+          const char *arg_unused) {
+         CtrlSubcommandUsage(nullptr, commands, countof(commands), args, nargs);
+       }},
+      VERSION_ARGUMENT_DESCRIPTION(),
+      RUNROOT_ARGUMENT_DESCRIPTION(),
   };
 
   BaseLogFile *base_log_file = new BaseLogFile("stderr");
-  diags                      = new Diags(program_name, "" /* tags */, "" /* actions */, base_log_file);
+  diags =
+      new Diags(program_name, "" /* tags */, "" /* actions */, base_log_file);
 
   // Process command line arguments and dump into variables
-  if (!CtrlProcessArguments(argc, argv, argument_descriptions, countof(argument_descriptions)))
{
-    return CtrlSubcommandUsage(nullptr, commands, countof(commands), argument_descriptions,
countof(argument_descriptions));
+  if (!CtrlProcessArguments(argc, argv, argument_descriptions,
+                            countof(argument_descriptions))) {
+    return CtrlSubcommandUsage(nullptr, commands, countof(commands),
+                               argument_descriptions,
+                               countof(argument_descriptions));
   }
 
   if (debug) {
     diags->activate_taglist("traffic_ctl", DiagsTagType_Debug);
     diags->config.enabled[DiagsTagType_Debug] = true;
-    diags->show_location                      = SHOW_LOCATION_DEBUG;
+    diags->show_location = SHOW_LOCATION_DEBUG;
   }
 
   if (n_file_arguments < 1) {
-    return CtrlSubcommandUsage(nullptr, commands, countof(commands), argument_descriptions,
countof(argument_descriptions));
+    return CtrlSubcommandUsage(nullptr, commands, countof(commands),
+                               argument_descriptions,
+                               countof(argument_descriptions));
   }
 
   runroot_handler(argv);
@@ -261,10 +240,13 @@ main(int argc, const char **argv)
 
   ats_scoped_str rundir(RecConfigReadRuntimeDir());
 
-  // Make a best effort to connect the control socket. If it turns out we are just displaying
help or something then it
-  // doesn't matter that we failed. If we end up performing some operation then that operation
will fail and display the
+  // Make a best effort to connect the control socket. If it turns out we are
+  // just displaying help or something then it
+  // doesn't matter that we failed. If we end up performing some operation then
+  // that operation will fail and display the
   // error.
-  TSInit(rundir, static_cast<TSInitOptionT>(TS_MGMT_OPT_NO_EVENTS | TS_MGMT_OPT_NO_SOCK_TESTS));
+  TSInit(rundir, static_cast<TSInitOptionT>(TS_MGMT_OPT_NO_EVENTS |
+                                            TS_MGMT_OPT_NO_SOCK_TESTS));
 
   for (unsigned i = 0; i < countof(commands); ++i) {
     if (strcmp(file_arguments[0], commands[i].name) == 0) {
@@ -277,5 +259,7 @@ main(int argc, const char **argv)
 
   // Done with the mgmt API.
   TSTerminate();
-  return CtrlSubcommandUsage(nullptr, commands, countof(commands), argument_descriptions,
countof(argument_descriptions));
+  return CtrlSubcommandUsage(nullptr, commands, countof(commands),
+                             argument_descriptions,
+                             countof(argument_descriptions));
 }

-- 
To stop receiving notification emails like this one, please contact
zwoop@apache.org.

Mime
View raw message