Skip to content

Commit

Permalink
Refactor: use pgagroal_json_create_new_command_object to output more …
Browse files Browse the repository at this point in the history
…information
  • Loading branch information
T1t4m1un committed Apr 19, 2024
1 parent 106202d commit 5289bc2
Showing 1 changed file with 56 additions and 41 deletions.
97 changes: 56 additions & 41 deletions src/admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <logging.h>
#include <security.h>
#include <utils.h>
#include <json.h>
#include <management.h>

/* system */
#include <ctype.h>
Expand All @@ -52,9 +54,6 @@
#define ACTION_REMOVE_USER 4
#define ACTION_LIST_USERS 5

#define COMMAND_OUTPUT_FORMAT_TEXT 'T'
#define COMMAND_OUTPUT_FORMAT_JSON 'J'

static int master_key(char* password, bool generate_pwd, int pwd_length);
static int add_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length);
static int update_user(char* users_path, char* username, char* password, bool generate_pwd, int pwd_length);
Expand Down Expand Up @@ -956,30 +955,11 @@ remove_user(char* users_path, char* username)
static int
list_users(char* users_path, char output_format)
{
/* For JSON/text format output */
const char *json_fmt = "\t\t\"%s\"";
const char *text_fmt = "%s";
const char *json_newline = ",\n";
const char *text_newline = "\n";

bool is_first_line = true;
const char *fmt = NULL;
const char *newline = NULL;
if (output_format == COMMAND_OUTPUT_FORMAT_JSON)
{
fmt = json_fmt;
newline = json_newline;
}
else
{
fmt = text_fmt;
newline = text_newline;
}


FILE* users_file = NULL;
char line[MISC_LENGTH];
char* ptr = NULL;
cJSON *json = NULL;
cJSON *user_list = NULL;

users_file = fopen(users_path, "r");
if (!users_file)
Expand All @@ -989,29 +969,54 @@ list_users(char* users_path, char output_format)

if (output_format == COMMAND_OUTPUT_FORMAT_JSON)
{
printf("{\n");
printf("\t\"users\": [\n");
}
/* JSON format output */
int user_count = 0;
json = pgagroal_json_create_new_command_object("user ls", true, "pgagroal-admin");
user_list = cJSON_CreateArray();

/* List */
fmt = (output_format == COMMAND_OUTPUT_FORMAT_JSON ? json_fmt : text_fmt);
while (fgets(line, sizeof(line), users_file))
{
if (!is_first_line)
if (user_list == NULL)
{
printf("%s", newline);
goto error;
}
is_first_line = false;

ptr = strtok(line, ":");
printf(fmt, ptr);
}
while (fgets(line, sizeof(line), users_file))
{
ptr = strtok(line, ":");
cJSON *user_name = cJSON_CreateString(ptr);
if (user_name == NULL)
{
goto error;
}

printf("\n");
if (output_format == COMMAND_OUTPUT_FORMAT_JSON)
user_count++;
cJSON_AddItemToArray(user_list, user_name);
}

cJSON *command = cJSON_GetObjectItemCaseSensitive(json, "command");
if (command == NULL)
{
goto error;
}

cJSON *output = cJSON_GetObjectItemCaseSensitive(command, JSON_TAG_COMMAND_OUTPUT);
if (output == NULL)
{
goto error;
}

cJSON_AddNumberToObject(output, "count", user_count);
cJSON_AddItemToObject(output, "list", user_list);

pgagroal_json_print_and_free_json_object(json);
}
else
{
printf("\t]\n");
printf("}\n");
/* Text format output */
while (fgets(line, sizeof(line), users_file))
{
ptr = strtok(line, ":");
printf("%s\n", ptr);
}
}

fclose(users_file);
Expand All @@ -1025,5 +1030,15 @@ list_users(char* users_path, char output_format)
fclose(users_file);
}

if (json)
{
cJSON_Delete(json);
}

if (user_list)
{
cJSON_Delete(user_list);
}

return 1;
}

0 comments on commit 5289bc2

Please sign in to comment.