refactor: use bitwise for options instead two booleans

This commit is contained in:
Jozef Steinhübl 2024-07-28 22:45:33 +02:00
parent f9e9a5868c
commit 1ee984bd69
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
2 changed files with 16 additions and 11 deletions

View file

@ -19,6 +19,12 @@
break; \ break; \
} }
#define MASK_OPTION(opt, mask, field) \
case opt: { \
mask |= field; \
break; \
}
void cli_print_help(); void cli_print_help();
void cli_print_version(); void cli_print_version();

View file

@ -8,6 +8,9 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#define PUSH_TAGS_OPTION 1 << 0
#define VERBOSE_OPTION 1 << 1
char *get_current_branch_name() { char *get_current_branch_name() {
FILE *file_ptr; FILE *file_ptr;
char output[256]; char output[256];
@ -31,7 +34,7 @@ char *get_current_branch_name() {
return branch; return branch;
} }
int git_push(char *provider_name, char *branch_name, bool tags, bool verbose) { int git_push(char *provider_name, char *branch_name, int options) {
FILE *file_ptr; FILE *file_ptr;
char output[1024]; char output[1024];
char command[256]; char command[256];
@ -44,7 +47,7 @@ int git_push(char *provider_name, char *branch_name, bool tags, bool verbose) {
} }
char branch_or_tags[256] = ""; char branch_or_tags[256] = "";
if (tags) { if (options & PUSH_TAGS_OPTION) {
strcat(branch_or_tags, "--tags"); strcat(branch_or_tags, "--tags");
} else { } else {
strcat(branch_or_tags, branch_name); strcat(branch_or_tags, branch_name);
@ -60,7 +63,7 @@ int git_push(char *provider_name, char *branch_name, bool tags, bool verbose) {
while (fgets(output, sizeof(output), file_ptr) != while (fgets(output, sizeof(output), file_ptr) !=
NULL) { // need to process for valid exit code NULL) { // need to process for valid exit code
if (verbose) { if (options & VERBOSE_OPTION) {
printf("%s", output); printf("%s", output);
} }
} }
@ -69,26 +72,22 @@ int git_push(char *provider_name, char *branch_name, bool tags, bool verbose) {
return ret; return ret;
} }
void set_verbose(bool *verbose) { *verbose = true; }
void set_tags(bool *tags) { *tags = true; }
int cli_command_push(int argc, char **argv) { int cli_command_push(int argc, char **argv) {
struct gimi_config *cfg = config_read(); struct gimi_config *cfg = config_read();
ASSERT_CONFIG_EXIST(cfg); ASSERT_CONFIG_EXIST(cfg);
bool tags = false; int options = 0;
bool verbose = false;
HANDLE_OPTIONS(argc, argv, "tv", HANDLE_OPTIONS(argc, argv, "tv",
OPTION('t', set_tags, &tags) MASK_OPTION('t', options, PUSH_TAGS_OPTION)
OPTION('v', set_verbose, &verbose)); MASK_OPTION('v', options, VERBOSE_OPTION));
char *branch_name = get_current_branch_name(); char *branch_name = get_current_branch_name();
for (int i = 0; i < cfg->providers_size; i++) { for (int i = 0; i < cfg->providers_size; i++) {
struct gimi_config_provider *provider = cfg->providers[i]; struct gimi_config_provider *provider = cfg->providers[i];
int ret = git_push(provider->name, branch_name, tags, verbose); int ret = git_push(provider->name, branch_name, options);
if (ret != 0) { if (ret != 0) {
printf("error: failed to push into '%s' with git's exit code %d.\n", printf("error: failed to push into '%s' with git's exit code %d.\n",
provider->name, ret); provider->name, ret);