Skip to content

Commit

Permalink
Merge pull request #668 from openziti/style/DRY-command-errors
Browse files Browse the repository at this point in the history
DRY command parsing. Prevent unnecessary output in some cases.
  • Loading branch information
sabedevops committed Jul 5, 2023
2 parents 9093536 + e4b78e0 commit 9456a8b
Showing 1 changed file with 85 additions and 87 deletions.
172 changes: 85 additions & 87 deletions programs/ziti-edge-tunnel/ziti-edge-tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,15 @@ static tunneler_context initialize_tunneler(netif_driver tun, uv_loop_t* ziti_lo
#include <commandline.h>
#include <getopt.h>

#define CHECK_COMMAND_ERRORS(errors) \
do { \
if (errors > 0) { \
commandline_help(stderr); \
exit(EXIT_FAILURE); \
} \
} while (0)


static CommandLine main_cmd;
static void usage(int argc, char *argv[]) {
if (argc == 0) {
Expand Down Expand Up @@ -1794,9 +1803,6 @@ static const char* dns_upstream = NULL;
static bool host_only = false;

static int run_opts(int argc, char *argv[]) {
printf("About to run tunnel service... %s", main_cmd.name);
ziti_set_app_info(main_cmd.name, ziti_tunneler_version());

int c, option_index, errors = 0;
optind = 0;
bool identity_provided = false;
Expand Down Expand Up @@ -1834,17 +1840,16 @@ static int run_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

printf("About to run tunnel service... %s", main_cmd.name);
ziti_set_app_info(main_cmd.name, ziti_tunneler_version());

return optind;
}

static int run_host_opts(int argc, char *argv[]) {
printf("About to run tunnel service that hosts services... %s", main_cmd.name);
ziti_set_app_info(main_cmd.name, ziti_tunneler_version());

int c, option_index, errors = 0;
optind = 0;
bool identity_provided = false;
Expand Down Expand Up @@ -1876,10 +1881,16 @@ static int run_host_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0 || !identity_provided) {
commandline_help(stderr);
exit(1);

if (!identity_provided) {
errors++;
}

CHECK_COMMAND_ERRORS(errors);

printf("About to run tunnel service that hosts services... %s", main_cmd.name);
ziti_set_app_info(main_cmd.name, ziti_tunneler_version());

host_only = true;
return optind;
}
Expand Down Expand Up @@ -1947,7 +1958,7 @@ static void run(int argc, char *argv[]) {
int rc = sscanf(configured_cidr, "%d.%d.%d.%d/%d", &ip[0], &ip[1], &ip[2], &ip[3], &bits);
if (rc != 5) {
ZITI_LOG(ERROR, "Invalid IP range specification: n.n.n.n/m format is expected");
exit(1);
exit(EXIT_FAILURE);
}

uint32_t mask = 0;
Expand Down Expand Up @@ -2007,7 +2018,7 @@ static void run(int argc, char *argv[]) {

if (ziti_loop == NULL) {
ZITI_LOG(ERROR, "failed to initialize default uv loop");
exit(1);
exit(EXIT_FAILURE);
}

int rc;
Expand Down Expand Up @@ -2040,10 +2051,9 @@ static int version_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

return optind;
}

Expand Down Expand Up @@ -2101,10 +2111,13 @@ static int parse_enroll_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0 || enroll_opts.jwt == NULL || config_file == NULL) {
commandline_help(stderr);
exit(1);

if (enroll_opts.jwt == NULL || config_file == NULL) {
errors++;
}

CHECK_COMMAND_ERRORS(errors);

return optind;
}

Expand Down Expand Up @@ -2158,25 +2171,25 @@ static void enroll(int argc, char *argv[]) {

if (config_file == 0) {
ZITI_LOG(ERROR, "output file option(-i|--identity) is required");
exit(-1);
exit(EXIT_FAILURE);
}

if (enroll_opts.jwt == NULL) {
ZITI_LOG(ERROR, "JWT file option(-j|--jwt) is required");
exit(1);
exit(EXIT_FAILURE);
}

/* open with O_EXCL to fail if the file exists */
int outfd = open(config_file, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR);
if (outfd < 0) {
ZITI_LOG(ERROR, "failed to open file %s: %s(%d)", config_file, strerror(errno), errno);
exit(1);
exit(EXIT_FAILURE);
}
FILE *outfile = NULL;
if ((outfile = fdopen(outfd, "wb")) == NULL) {
ZITI_LOG(ERROR, "failed to open file %s: %s(%d)", config_file, strerror(errno), errno);
(void) close(outfd);
exit(1);
exit(EXIT_FAILURE);
}

struct enroll_cb_params params = { 0 };
Expand All @@ -2201,7 +2214,7 @@ static void enroll(int argc, char *argv[]) {
/* if unsuccessful, delete config_file and exit */
if (rc < 0) {
(void) unlink(config_file);
exit(1);
exit(EXIT_FAILURE);
}
}

Expand Down Expand Up @@ -2235,10 +2248,9 @@ static int dump_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_ziti_dump_to_json(dump_options, MODEL_JSON_COMPACT, &json_len);
if (dump_options != NULL) {
Expand Down Expand Up @@ -2370,10 +2382,9 @@ static int on_off_identity_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_on_off_identity_to_json(&on_off_identity_options, MODEL_JSON_COMPACT, &json_len);
on_off_identity_options.identifier = NULL; // don't try to free static memory (`optarg`)
Expand Down Expand Up @@ -2406,10 +2417,9 @@ static int enable_identity_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_load_identity_to_json(&load_identity_options, MODEL_JSON_COMPACT, &json_len);
free_tunnel_load_identity(&load_identity_options);
Expand Down Expand Up @@ -2441,10 +2451,9 @@ static int enable_mfa_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_enable_mfa_to_json(enable_mfa_options, MODEL_JSON_COMPACT, &json_len);
free(enable_mfa_options);
Expand Down Expand Up @@ -2480,10 +2489,9 @@ static int verify_mfa_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_verify_mfa_to_json(verify_mfa_options, MODEL_JSON_COMPACT, &json_len);
free(verify_mfa_options);
Expand Down Expand Up @@ -2519,10 +2527,9 @@ static int remove_mfa_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_remove_mfa_to_json(remove_mfa_options, MODEL_JSON_COMPACT, &json_len);
free(remove_mfa_options);
Expand Down Expand Up @@ -2558,10 +2565,9 @@ static int submit_mfa_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_submit_mfa_to_json(submit_mfa_options, MODEL_JSON_COMPACT, &json_len);
free(submit_mfa_options);
Expand Down Expand Up @@ -2597,10 +2603,9 @@ static int generate_mfa_codes_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_generate_mfa_codes_to_json(mfa_codes_options, MODEL_JSON_COMPACT, &json_len);
free(mfa_codes_options);
Expand Down Expand Up @@ -2636,10 +2641,9 @@ static int get_mfa_codes_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_get_mfa_codes_to_json(get_mfa_codes_options, MODEL_JSON_COMPACT, &json_len);
free(get_mfa_codes_options);
Expand Down Expand Up @@ -2671,10 +2675,9 @@ static int set_log_level_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_set_log_level_to_json(log_level_options, MODEL_JSON_COMPACT, &json_len);
free(log_level_options);
Expand Down Expand Up @@ -2718,10 +2721,9 @@ static int update_tun_ip_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_tun_ip_v4_to_json(tun_ip_v4_options, MODEL_JSON_COMPACT, &json_len);
free(tun_ip_v4_options);
Expand Down Expand Up @@ -2765,10 +2767,9 @@ static int endpoint_status_change_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_status_change_to_json(tunnel_status_change_opts, MODEL_JSON_COMPACT, &json_len);
free(tunnel_status_change_opts);
Expand Down Expand Up @@ -2822,13 +2823,12 @@ static int svc_opts(int argc, char *argv[]) {
}
}
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_service_control_to_json(tunnel_service_control_options, MODEL_JSON_COMPACT, &json_len);

if (errors > 0) {
commandline_help(stderr);
exit(1);
}
return optind;
}
#endif
Expand Down Expand Up @@ -2866,10 +2866,9 @@ static int delete_identity_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_delete_identity_to_json(delete_identity_options, MODEL_JSON_COMPACT, &json_len);
free(delete_identity_options);
Expand Down Expand Up @@ -2906,10 +2905,9 @@ static int add_identity_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
commandline_help(stderr);
exit(1);
}

CHECK_COMMAND_ERRORS(errors);

size_t json_len;
cmd->data = tunnel_add_identity_to_json(tunnel_add_identity_opt, MODEL_JSON_COMPACT, &json_len);
free(tunnel_add_identity_opt);
Expand Down

0 comments on commit 9456a8b

Please sign in to comment.