srpc tools: add color for print info.

This commit is contained in:
holmes1412 2023-03-14 22:51:14 +08:00
parent 8a1587ff1e
commit 7ed3c96e9b
8 changed files with 203 additions and 106 deletions

View File

@ -2,7 +2,7 @@
# srpc小工具
### 一个帮你生成Workflow和SRPC项目/脚手架的小工具。
### 一个帮你快速生成Workflow和SRPC项目的脚手架小工具。
## 1. 编译
@ -24,7 +24,7 @@ make
```
Description:
A handy generator for Workflow and SRPC project.
Simple generator for building Workflow and SRPC projects.
Usage:
./srpc <COMMAND> <PROJECT_NAME> [FLAGS]

View File

@ -2,7 +2,7 @@
# srpc tools
### An easy tool to generate Workflow and SRPC project
### Simple generator for building Workflow and SRPC projects.
## 1. COMPILE
@ -20,7 +20,7 @@ make
```
Description:
A handy generator for Workflow and SRPC project.
Simple generator for building Workflow and SRPC projects.
Usage:
./srpc <COMMAND> <PROJECT_NAME> [FLAGS]

View File

@ -36,8 +36,7 @@ static std::string server_process_codes(uint8_t type)
task->get_req()->get_request_uri());
print_peer_address(task);
task->get_resp()->append_output_body("<html>Hello from server!</html>");
)");
task->get_resp()->append_output_body("<html>Hello from server!</html>");)");
if (type == PROTOCOL_TYPE_REDIS)
return std::string(R"(
@ -53,8 +52,7 @@ static std::string server_process_codes(uint8_t type)
print_peer_address(task);
val.set_status("OK"); // example: return OK to every requests
resp->set_result(val);
)");
resp->set_result(val);)");
return std::string("Unknown type");
}
@ -302,7 +300,8 @@ static bool basic_get_opt(int argc, const char **argv, struct srpc_config *confi
return false;
break;
default:
printf("Error:\n Unknown args : %s\n\n", argv[optind - 1]);
printf(COLOR_RED"Error:\n Unknown args : "
COLOR_BLUE"%s\n\n" COLOR_OFF, argv[optind - 1]);
return false;
}
}
@ -312,12 +311,15 @@ static bool basic_get_opt(int argc, const char **argv, struct srpc_config *confi
static void basic_print_usage(const char *name, const char *command)
{
printf("Usage:\n"
" %s %s <PROJECT_NAME> [FLAGS]\n\n"
"Available Flags:\n"
" -o : project output path (default: CURRENT_PATH)\n"
" -d : path of dependencies (default: COMPILE_PATH)\n"
, name, command);
printf(COLOR_PINK"Usage:\n"
COLOR_INFO" %s " COLOR_COMMAND "%s "
COLOR_INFO"<PROJECT_NAME>" COLOR_FLAG " [FLAGS]\n\n"
COLOR_PINK"Available Flags:\n"
COLOR_FLAG" -o : "
COLOR_WHITE"project output path (default: CURRENT_PATH)\n"
COLOR_FLAG" -d : "
COLOR_WHITE"path of dependencies (default: COMPILE_PATH)\n"
COLOR_OFF, name, command);
}
HttpController::HttpController()
@ -410,13 +412,14 @@ bool FileServiceController::get_opt(int argc, const char **argv)
void FileServiceController::print_success_info() const
{
printf("Success:\n make project path \" %s \" done.\n\n",
this->config.output_path);
printf("Commands:\n cd %s\n make -j\n\n",
this->config.output_path);
printf("Execute:\n ./server\n\n");
printf("Try file service:\n");
printf(" curl localhost:8080/index.html\n");
printf(" curl -i localhost:8080/a/b/\n\n");
printf(COLOR_GREEN"Success:\n make project path "
COLOR_BLUE"\" %s \"" COLOR_GREEN " done.\n\n",
this->config.output_path);
printf(COLOR_PINK"Commands:\n " COLOR_BLUE "cd %s\n make -j\n\n",
this->config.output_path);
printf(COLOR_PINK"Execute:\n " COLOR_GREEN "./server\n\n");
printf(COLOR_PINK"Try file service:\n");
printf(COLOR_WHITE" curl localhost:8080/index.html\n");
printf(" curl -i localhost:8080/a/b/\n\n" COLOR_OFF);
}

View File

@ -226,11 +226,10 @@ bool CommandController::parse_args(int argc, const char **argv)
{
if (argc < 3)
{
printf("Missing: PROJECT_NAME\n\n");
printf(COLOR_RED "Missing: PROJECT_NAME\n\n" COLOR_OFF);
return false;
}
// getcwd(this->config.output_path, MAXPATHLEN);
memset(this->config.output_path, 0, MAXPATHLEN);
this->config.project_name = argv[2];
this->config.service_name = argv[2];
@ -261,8 +260,9 @@ bool CommandController::check_args()
if (strlen(this->config.project_name) >= MAXPATHLEN - path_len - 2)
{
printf("Error:\n project name \" %s \" or path \" %s \" "\
" is too long. total limit %d.\n\n",
printf(COLOR_RED "Error:\n project name" COLOR_BLUE " \" %s \" "
COLOR_RED "or path" COLOR_BLUE " \" %s \" "
COLOR_RED " is too long. Total limit : %d.\n\n" COLOR_OFF,
this->config.project_name, this->config.output_path, MAXPATHLEN);
return false;
}
@ -283,23 +283,25 @@ bool CommandController::dependencies_and_dir()
{
struct srpc_config *config = &this->config;
DIR *dir;
int status;
dir = opendir(config->output_path);
if (dir != NULL)
{
printf("Error:\n project path \" %s \" EXISTS.\n\n",
config->output_path);
closedir(dir);
printf(COLOR_RED "Error:\n project path "
COLOR_BLUE "\" %s \"" COLOR_RED " EXISTS.\n\n" COLOR_OFF,
config->output_path);
closedir(dir);
return false;
}
dir = opendir(config->template_path);
if (dir == NULL)
{
printf("Error:\n template path \" %s \" does NOT exist.\n",
config->template_path);
printf(COLOR_RED "Error:\n template path "
COLOR_BLUE "\" %s \" " COLOR_RED "does NOT exist.\n" COLOR_OFF,
config->template_path);
return false;
}
closedir(dir);
@ -313,12 +315,12 @@ bool CommandController::dependencies_and_dir()
std::string cmd = "cd ";
cmd += config->depend_path;
cmd += "&& git submodule init && git submodule update";
system(cmd.c_str());
(void)system(cmd.c_str());
if (access(workflow_file.c_str(), 0) != 0)
{
printf(DEPENDENCIES_ERROR, config->depend_path,
config->project_name);
config->project_name);
return false;
}
}
@ -331,7 +333,7 @@ bool CommandController::dependencies_and_dir()
std::string cmd = "cd ";
cmd += config->depend_path;
cmd += " && make -j4";
system(cmd.c_str());
(void)system(cmd.c_str());
}
}
@ -417,21 +419,30 @@ bool CommandController::copy_single_file(const std::string& in_file,
ret = fwrite(buf, size, 1, write_fp) >= 0 ? true : false;
}
else
printf("Error:\n read \" %s \" failed\n\n", in_file.c_str());
{
printf(COLOR_RED "Error:\n read " COLOR_WHITE
"\" %s \" " COLOR_RED "failed\n\n" COLOR_OFF,
in_file.c_str());
}
free(buf);
}
else
printf("Error:\n system error.\n\n");
printf(COLOR_RED "Error:\n system error.\n\n" COLOR_OFF);
fclose(write_fp);
}
else
printf("Error:\n write \" %s \" failed\n\n", out_file.c_str());
{
printf(COLOR_RED "Error:\n write" COLOR_WHITE " \" %s \" "
COLOR_RED "failed\n\n" COLOR_OFF, out_file.c_str());
}
}
else
printf("Error:\n open \" %s \" failed.\n\n", in_file.c_str());
{
printf(COLOR_RED "Error:\n open" COLOR_WHITE " \" %s \""
COLOR_RED " failed.\n\n" COLOR_OFF, in_file.c_str());
}
return ret;
}
@ -460,11 +471,13 @@ bool CommandController::copy_files()
void CommandController::print_success_info() const
{
printf("Success:\n make project path \" %s \" done.\n\n",
this->config.output_path);
printf("Commands:\n cd %s\n make -j\n\n",
this->config.output_path);
printf("Execute:\n ./server\n ./client\n\n");
printf(COLOR_GREEN"Success!\n make project path "
COLOR_BLUE"\" %s \"" COLOR_GREEN " done.\n\n",
this->config.output_path);
printf(COLOR_PINK"Commands:\n " COLOR_BLUE "cd %s\n make -j\n\n",
this->config.output_path);
printf(COLOR_PINK"Execute:\n "
COLOR_GREEN "./server\n ./client\n\n" COLOR_OFF);
}
bool common_cmake_transform(const std::string& format, FILE *out,

View File

@ -19,6 +19,20 @@
#include "srpc_config.h"
#define COLOR_OFF "\033[0m"
#define COLOR_WHITE "\033[37;1m"
#define COLOR_RED "\033[31;1m"
#define COLOR_GREEN "\033[32;1m"
#define COLOR_YELLOW "\033[33;1m"
#define COLOR_PURPLE "\033[34;1m"
#define COLOR_PINK "\033[35;1m"
#define COLOR_BLUE "\033[36;1m"
#define COLOR_LPURPLE "\033[94;1m"
#define COLOR_COMMAND COLOR_BLUE
#define COLOR_INFO COLOR_YELLOW
#define COLOR_FLAG COLOR_PURPLE
class CommandController
{
public:

View File

@ -17,19 +17,68 @@
#include <stdio.h>
#include "srpc_controller.h"
//TODO: 1412
static void usage(const char *name)
{
printf("Description:\n"
" A handy generator for Workflow and SRPC project.\n\n"
"Usage:\n"
" %s <COMMAND> <PROJECT_NAME> [FLAGS]\n\n"
"Available Commands:\n"
" \"http\" - create project with both client and server\n"
" \"redis\" - create project with both client and server\n"
" \"rpc\" - create project with both client and server\n"
" \"proxy\" - create proxy for some client and server protocol\n"
" \"file\" - create project with file service\n"
, name);
printf(COLOR_PINK"Description:\n"
COLOR_PURPLE" Simple generator for building Workflow and SRPC projects.\n\n"
COLOR_PINK"Usage:\n"
COLOR_INFO" %s" COLOR_COMMAND " <COMMAND>"
COLOR_INFO" <PROJECT_NAME>" " [FLAGS]\n\n"
COLOR_PINK"Available Commands:\n"
COLOR_COMMAND" http"
COLOR_WHITE" - create project with both client and server\n"
COLOR_COMMAND" redis"
COLOR_WHITE" - create project with both client and server\n"
COLOR_COMMAND" rpc"
COLOR_WHITE" - create project with both client and server\n"
COLOR_COMMAND" proxy"
COLOR_WHITE" - create proxy for some client and server protocol\n"
COLOR_COMMAND" file"
COLOR_WHITE" - create project with file service\n"
COLOR_OFF, name);
}
static void usage1(const char *name)
{
printf(COLOR_PINK"Description:\n"
COLOR_PURPLE" Simple generator for building Workflow and SRPC projects.\n\n"
COLOR_PINK"Usage:\n"
COLOR_LPURPLE" %s" COLOR_BLUE " <COMMAND>"
COLOR_LPURPLE" <PROJECT_NAME>" " [FLAGS]\n\n"
COLOR_PINK"Available Commands:\n"
COLOR_BLUE" http"
COLOR_WHITE" - create project with both client and server\n"
COLOR_BLUE" redis"
COLOR_WHITE" - create project with both client and server\n"
COLOR_BLUE" rpc"
COLOR_WHITE" - create project with both client and server\n"
COLOR_BLUE" proxy"
COLOR_WHITE" - create proxy for some client and server protocol\n"
COLOR_BLUE" file"
COLOR_WHITE" - create project with file service\n"
COLOR_OFF, name);
}
static void usage2(const char *name)
{
printf(COLOR_PINK"Description:\n"
COLOR_PURPLE" Simple generator for building Workflow and SRPC projects.\n\n"
COLOR_PINK"Usage:\n"
COLOR_BLUE" %s" COLOR_YELLOW " <COMMAND>"
COLOR_BLUE" <PROJECT_NAME>" " [FLAGS]\n\n"
COLOR_PINK"Available Commands:\n"
COLOR_YELLOW" http"
COLOR_WHITE" - create project with both client and server\n"
COLOR_YELLOW" redis"
COLOR_WHITE" - create project with both client and server\n"
COLOR_YELLOW" rpc"
COLOR_WHITE" - create project with both client and server\n"
COLOR_YELLOW" proxy"
COLOR_WHITE" - create proxy for some client and server protocol\n"
COLOR_YELLOW" file"
COLOR_WHITE" - create project with file service\n"
COLOR_OFF, name);
}
int main(int argc, const char *argv[])

View File

@ -220,25 +220,33 @@ ProxyController::ProxyController()
void ProxyController::print_usage(const char *name) const
{
printf("Usage:\n"
" %s proxy <PROJECT_NAME> [FLAGS]\n\n"
"Available Flags:\n"
" -c : client type for proxy [ Http | Redis | SRPC | SRPCHttp"
printf(COLOR_PINK"Usage:\n"
COLOR_INFO" %s " COLOR_BLUE "proxy "
COLOR_INFO "<PROJECT_NAME>" COLOR_FLAG " [FLAGS]\n\n"
COLOR_PINK"Available Flags:\n"
COLOR_FLAG" -c :"
COLOR_WHITE" client type for proxy [ Http | Redis | SRPC | SRPCHttp"
" | BRPC | Thrift | ThriftHttp | TRPC | TRPCHttp ] (default: Http)\n"
" -s : server type for proxy [ Http | Redis | SRPC | SRPCHttp"
COLOR_FLAG" -s :"
COLOR_WHITE" server type for proxy [ Http | Redis | SRPC | SRPCHttp"
" | BRPC | Thrift | ThriftHttp | TRPC | TRPCHttp ] (default: Http)\n"
" -o : project output path (default: CURRENT_PATH)\n"
" -d : path of dependencies (default: COMPILE_PATH)\n"
, name);
COLOR_FLAG" -o :"
COLOR_WHITE" project output path (default: CURRENT_PATH)\n"
COLOR_FLAG" -d :"
COLOR_WHITE" path of dependencies (default: COMPILE_PATH)\n"
COLOR_OFF, name);
}
void ProxyController::print_success_info() const
{
printf("Success:\n make project path \" %s \" done.\n\n",
this->config.output_path);
printf("Commands:\n cd %s\n make -j\n\n",
this->config.output_path);
printf("Execute:\n ./server\n ./proxy\n ./client\n\n");
printf(COLOR_GREEN"Success:\n make project path "
COLOR_BLUE"\" %s \"" COLOR_GREEN " done.\n\n" COLOR_OFF,
this->config.output_path);
printf(COLOR_PINK"Commands:\n"
COLOR_BLUE " cd %s\n make -j\n\n" COLOR_OFF,
this->config.output_path);
printf(COLOR_PINK"Execute:\n"
COLOR_GREEN" ./server\n ./proxy\n ./client\n\n" COLOR_OFF);
}
bool ProxyController::copy_files()
@ -353,7 +361,8 @@ bool ProxyController::get_opt(int argc, const char **argv)
return false;
break;
default:
printf("Error:\n Unknown args : %s\n\n", argv[optind - 1]);
printf(COLOR_RED"Error:\n Unknown args : "
COLOR_BLUE"%s\n\n" COLOR_OFF, argv[optind - 1]);
return false;
}
}
@ -371,27 +380,23 @@ bool ProxyController::check_args()
if (client_type < 0 || server_type < 0)
{
printf("Error:\n Invalid type : %s, %s\n\n",
printf(COLOR_RED"Error:\n Invalid type :"
COLOR_BLUE" %s" COLOR_RED ", " COLOR_BLUE "%s\n\n" COLOR_OFF,
this->config.proxy_server_type_string(),
this->config.proxy_client_type_string());
return false;
}
// TODO: temperarily only support workflow to workflow, rpc to rpc
if ((client_type == BASIC_TYPE && server_type > BASIC_TYPE) ||
(server_type == BASIC_TYPE && client_type > BASIC_TYPE))
{
printf("Error:\n Temperarily not support %s and %s together\n\n",
this->config.proxy_server_type_string(),
this->config.proxy_client_type_string());
return false;
}
// TODO: temperarily NOT support protobuf with thrift
if ((client_type == PROTOBUF_TYPE && server_type == THRIFT_TYPE) ||
(server_type == BASIC_TYPE && client_type > BASIC_TYPE) ||
// TODO: temperarily only support workflow to workflow, rpc to rpc
(client_type == PROTOBUF_TYPE && server_type == THRIFT_TYPE) ||
(server_type == PROTOBUF_TYPE && client_type == THRIFT_TYPE))
// TODO: temperarily NOT support protobuf with thrift
{
printf("Error:\n Temperarily not support %s and %s together\n\n",
printf(COLOR_RED"Error:\n Temperarily not support "
COLOR_BLUE"%s" COLOR_RED " and " COLOR_BLUE "%s"
COLOR_RED" together\n\n" COLOR_OFF,
this->config.proxy_server_type_string(),
this->config.proxy_client_type_string());
return false;
@ -404,7 +409,7 @@ bool ProxyController::check_args()
else if (client_type == THRIFT_TYPE)
{
// this->config.idl_type = IDL_TYPE_THRIFT;
printf("Error:\n Temperarily not support IDL thrift.\n\n");
printf(COLOR_RED"Error:\n Temperarily not support IDL thrift.\n\n" COLOR_OFF);
return false;
}

View File

@ -58,24 +58,34 @@ RPCController::RPCController()
void RPCController::print_usage(const char *name) const
{
printf("Usage:\n"
" %s rpc <PROJECT_NAME> [FLAGS]\n\n"
"Available Flags:\n"
" -r : rpc type [ SRPC | SRPCHttp | BRPC | Thrift | "
printf(COLOR_PINK"Usage:\n"
COLOR_INFO" %s " COLOR_COMMAND "rpc "
COLOR_INFO"<PROJECT_NAME> " COLOR_FLAG "[FLAGS]\n\n"
COLOR_PINK"Available Flags:\n"
COLOR_FLAG" -r "
COLOR_WHITE": rpc type [ SRPC | SRPCHttp | BRPC | Thrift | "
"ThriftHttp | TRPC | TRPCHttp ] (default: SRPC)\n"
" -o : project output path (default: CURRENT_PATH)\n"
" -s : service name (default: PROJECT_NAME)\n"
" -i : idl type [ protobuf | thrift ] (default: protobuf)\n"
" -x : data type [ protobuf | thrift | json ] "
COLOR_FLAG" -o "
COLOR_WHITE": project output path (default: CURRENT_PATH)\n"
COLOR_FLAG" -s "
COLOR_WHITE": service name (default: PROJECT_NAME)\n"
COLOR_FLAG" -i "
COLOR_WHITE": idl type [ protobuf | thrift ] (default: protobuf)\n"
COLOR_FLAG" -x "
COLOR_WHITE": data type [ protobuf | thrift | json ] "
"(default: idl type. json for http)\n"
" -c : compress type [ gzip | zlib | snappy | lz4 ] "
COLOR_FLAG" -c "
COLOR_WHITE": compress type [ gzip | zlib | snappy | lz4 ] "
"(default: no compression)\n"
" -d : path of dependencies (default: COMPILE_PATH)\n"
" -f : specify the idl_file to generate codes "
COLOR_FLAG" -d "
COLOR_WHITE": path of dependencies (default: COMPILE_PATH)\n"
COLOR_FLAG" -f "
COLOR_WHITE": specify the idl_file to generate codes "
"(default: templates/rpc/IDL_FILE)\n"
" -p : specify the path for idl_file to depend "
COLOR_FLAG" -p "
COLOR_WHITE": specify the path for idl_file to depend "
"(default: templates/rpc/)\n"
, name);
COLOR_OFF, name);
}
bool RPCController::copy_files()
@ -109,9 +119,9 @@ bool RPCController::copy_files()
return false;
ControlGenerator gen(config);
printf("Info: srpc-ctl generator begin.\n");
printf(COLOR_PURPLE"Info: srpc-ctl generator begin.\n" COLOR_OFF);
gen.generate(params);
printf("Info: srpc-ctl generator done.\n\n");
printf(COLOR_PURPLE"Info: srpc-ctl generator done.\n\n" COLOR_OFF);
}
return CommandController::copy_files();
@ -164,7 +174,8 @@ bool RPCController::get_opt(int argc, const char **argv)
config->specified_idl_path = optarg;
break;
default:
printf("Error:\n Unknown args : %s\n\n", argv[optind - 1]);
printf(COLOR_RED "Error:\n Unknown args : "
COLOR_BLUE "%s\n\n" COLOR_OFF, argv[optind - 1]);
return false;
}
}
@ -184,7 +195,7 @@ bool RPCController::check_args()
config->data_type == DATA_TYPE_MAX ||
config->compress_type == COMPRESS_TYPE_MAX)
{
printf("Error:\n Invalid rpc args.\n");
printf(COLOR_RED"Error:\n Invalid rpc args.\n" COLOR_OFF);
return false;
}
@ -195,8 +206,9 @@ bool RPCController::check_args()
if (config->idl_type == IDL_TYPE_PROTOBUF ||
config->data_type == DATA_TYPE_PROTOBUF)
{
printf("Error:\n "
"\" %s \" does NOT support protobuf as idl or data type.\n",
printf(COLOR_RED"Error:\n "
COLOR_BLUE"\" %s \" "
COLOR_RED"does NOT support protobuf as idl or data type.\n" COLOR_OFF,
config->rpc_type_string());
return false;
}
@ -209,8 +221,9 @@ bool RPCController::check_args()
if (config->idl_type == IDL_TYPE_THRIFT ||
config->data_type == DATA_TYPE_THRIFT)
{
printf("Error:\n "
"\" %s \" does NOT support thrift as idl or data type.\n",
printf(COLOR_RED "Error:\n "
COLOR_BLUE "\" %s \" "
COLOR_RED "does NOT support thrift as idl or data type.\n" COLOR_OFF,
config->rpc_type_string());
return false;
}