feat(c): extrude lib c
This commit is contained in:
Binary file not shown.
@@ -3,7 +3,7 @@
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include "macros.h"
|
||||
#include "macro.h"
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_DEBUG,
|
||||
|
||||
2
package/postgres/pg-neo-migration/src/macro.c
Normal file
2
package/postgres/pg-neo-migration/src/macro.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "macro.h"
|
||||
|
||||
@@ -11,16 +11,24 @@ typedef enum {
|
||||
COLOR_MODE_DISABLE
|
||||
} ColorMode;
|
||||
|
||||
// Function to set the color mode
|
||||
void set_output_color_mode(ColorMode mode);
|
||||
// Static color mode variable
|
||||
static ColorMode color_mode = COLOR_MODE_AUTO;
|
||||
|
||||
// Function to set color mode
|
||||
void set_output_color_mode(ColorMode mode) {
|
||||
color_mode = mode;
|
||||
}
|
||||
|
||||
// Macros for detecting terminal and color usage
|
||||
#define IS_TERMINAL() (isatty(fileno(stderr)))
|
||||
#define USE_COLOR() ((color_mode == COLOR_MODE_FORCE) || (color_mode == COLOR_MODE_AUTO && IS_TERMINAL()))
|
||||
|
||||
#define COLOR_RED (USE_COLOR() ? "\033[1;31m" : "")
|
||||
#define COLOR_RESET (USE_COLOR() ? "\033[0m" : "")
|
||||
|
||||
// Define color macros based on output type
|
||||
#define ERROR_PREFIX (IS_TERMINAL() ? "\033[1;31mError: " : "Error: ")
|
||||
#define ERROR_SUFFIX (IS_TERMINAL() ? "\033[0m\n" : "\n")
|
||||
#define ERROR_PREFIX PP_CAT_I(COLOR_RED, "Error: ")
|
||||
#define ERROR_SUFFIX PP_CAT_I(COLOR_RESET, "\n")
|
||||
|
||||
// Helper macros for argument counting
|
||||
// NOTE(yukkop): this ugly macroses for avoid all posible warnings
|
||||
@@ -42,4 +50,6 @@ void set_output_color_mode(ColorMode mode);
|
||||
#define eprintf(...) \
|
||||
PP_CAT(eprintf_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
#define todo fprintf(stderr, "%sNot implimented yet%s", COLOR_RED, COLOR_RESET);exit(1)
|
||||
|
||||
#endif // EPRINTF_H
|
||||
@@ -1,9 +0,0 @@
|
||||
#include "macros.h"
|
||||
|
||||
// Static color mode variable
|
||||
static ColorMode color_mode = COLOR_MODE_AUTO;
|
||||
|
||||
// Function to set color mode
|
||||
void set_output_color_mode(ColorMode mode) {
|
||||
color_mode = mode;
|
||||
}
|
||||
@@ -2,10 +2,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "logger.c"
|
||||
#include "macros.h"
|
||||
#include "macro.h"
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// TODO: check on the specific psql version
|
||||
int check_psql_installed(void) {
|
||||
@@ -58,7 +59,7 @@ void create_migration_inner(const char* migration_path, const char* type) {
|
||||
}
|
||||
|
||||
char filename[1024 + 19];
|
||||
snprintf(filename, sizeof(filename), "%s/00-entry-point.sql", path);
|
||||
snprintf(filename, sizeof(filename), "%s/0000-entry-point.sql", path);
|
||||
|
||||
FILE *fp = fopen(filename, "w");
|
||||
if (!fp) {
|
||||
@@ -101,9 +102,10 @@ void help_message(char name[]) {
|
||||
int main(int argc, char *argv[]) {
|
||||
srand(time(NULL));
|
||||
init_logger();
|
||||
|
||||
raise_log("init");
|
||||
|
||||
todo;
|
||||
|
||||
if (check_psql_installed()) { exit(1); }
|
||||
|
||||
if (argc < 2) {
|
||||
@@ -115,7 +117,9 @@ int main(int argc, char *argv[]) {
|
||||
char *migration_dir;
|
||||
char *db_url;
|
||||
char *migration_name;
|
||||
char force = 0;
|
||||
bool force = true;
|
||||
char **set_vars = NULL;
|
||||
int set_vars_count = 0;
|
||||
|
||||
/* Process global options until a known subcommand is encountered */
|
||||
int i = 1;
|
||||
@@ -171,11 +175,18 @@ int main(int argc, char *argv[]) {
|
||||
force = 1;
|
||||
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--set")) {
|
||||
if (i+1 < argc) {
|
||||
set_vars = relloc(set_vars);
|
||||
set_vars = realloc(set_vars, (set_vars_count+1) * sizeof(char*));
|
||||
set_vars[set_vars_count++] = argv[i+1];
|
||||
i++;
|
||||
} else {
|
||||
eprintf("%s requires an argument", argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!db_url) {
|
||||
eprintf("Database URL is required for %s subcommand.\n", subcommand);
|
||||
help_message(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(subcommand, "fetch") == 0) {
|
||||
@@ -187,14 +198,13 @@ int main(int argc, char *argv[]) {
|
||||
} else {
|
||||
eprintf("%s requires an argument", argv[i]);
|
||||
}
|
||||
|
||||
|
||||
if (!db_url) {
|
||||
eprintf("Database URL is required for %s subcommand.\n", subcommand);
|
||||
help_message(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!db_url) {
|
||||
eprintf("Database URL is required for migrate subcommand.\n");
|
||||
help_message(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user