Merge branch 'feat/pg-neo-migration' of github.com:hectic-lab/util.nix into feat/pg-neo-migration

This commit is contained in:
2025-03-19 01:43:46 +00:00
11 changed files with 266 additions and 26 deletions

26
package/c/lib/Makefile Normal file
View File

@@ -0,0 +1,26 @@
CC = gcc
CFLAGS = -Wall -Wextra -std=c11
LDFLAGS = -L. -lmylib # Link with your library
# Source files
LIB_SRC = mylib.c
LIB_OBJ = $(LIB_SRC:.c=.o)
TEST_SRC = test/test.c
TEST_OBJ = $(TEST_SRC:.c=.o)
TEST_BIN = test
# Build library
libmylib.a: $(LIB_OBJ)
ar rcs $@ $^
# Build test executable
$(TEST_BIN): $(TEST_OBJ) libmylib.a
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
# Compile C files
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
# Clean build artifacts
clean:
rm -f $(LIB_OBJ) $(TEST_OBJ) $(TEST_BIN) libmylib.a

View File

View File

189
package/c/lib/lib/heclib.h Normal file
View File

@@ -0,0 +1,189 @@
#ifndef EPRINTF_H
#define EPRINTF_H
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
// -------------
// -- Helpers --
// -------------
// Helper macros for argument counting
// NOTE(yukkop): this ugly macroses for avoid all posible warnings
#define PP_CAT(a, b) PP_CAT_I(a, b)
#define PP_CAT_I(a, b) a##b
#define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,N,...) N
#define PP_RSEQ_N() 9,8,7,6,5,4,3,2,1,0
// ------------
// -- Colors --
// ------------
// Color mode enumeration
typedef enum {
COLOR_MODE_AUTO,
COLOR_MODE_FORCE,
COLOR_MODE_DISABLE
} ColorMode;
// 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" : "")
// ------------
// -- Errors --
// ------------
// Define color macros based on output type
#define ERROR_PREFIX PP_CAT_I(COLOR_RED, "Error: ")
#define ERROR_SUFFIX PP_CAT_I(COLOR_RESET, "\n")
// eprintf handling 1 or more arguments
#define eprintf_1(fmt) \
fprintf(stderr, "%s" fmt "%s", ERROR_PREFIX, ERROR_SUFFIX)
#define eprintf_2(fmt, ...) \
fprintf(stderr, "%s" fmt "%s", ERROR_PREFIX, __VA_ARGS__, ERROR_SUFFIX)
#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)
// ------------
// -- Logger --
// ------------
typedef enum {
LOG_LEVEL_DEBUG,
LOG_LEVEL_LOG,
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_WARN,
LOG_LEVEL_EXCEPTION
} LogLevel;
const char* log_level_to_string(LogLevel level) {
switch (level) {
case LOG_LEVEL_DEBUG: return "DEBUG";
case LOG_LEVEL_LOG: return "LOG";
case LOG_LEVEL_INFO: return "INFO";
case LOG_LEVEL_NOTICE: return "NOTICE";
case LOG_LEVEL_WARN: return "WARN";
case LOG_LEVEL_EXCEPTION: return "EXCEPTION";
default: return "UNKNOWN";
}
}
LogLevel log_level_from_string(const char *level_str) {
if (!level_str) return LOG_LEVEL_INFO;
if (strcmp(level_str, "DEBUG") == 0)
return LOG_LEVEL_DEBUG;
else if (strcmp(level_str, "LOG") == 0)
return LOG_LEVEL_LOG;
else if (strcmp(level_str, "INFO") == 0)
return LOG_LEVEL_INFO;
else if (strcmp(level_str, "NOTICE") == 0)
return LOG_LEVEL_NOTICE;
else if (strcmp(level_str, "WARN") == 0)
return LOG_LEVEL_WARN;
else if (strcmp(level_str, "EXCEPTION") == 0)
return LOG_LEVEL_EXCEPTION;
else
return LOG_LEVEL_INFO;
}
LogLevel current_log_level = LOG_LEVEL_INFO;
void init_logger(void) {
current_log_level = log_level_from_string(getenv("LOG_LEVEL"));
}
void log_message(LogLevel level, int line, const char *format, ...) {
if (level < current_log_level) {
return;
}
time_t now = time(NULL);
struct tm tm_info;
localtime_r(&now, &tm_info);
char timeStr[20];
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &tm_info);
fprintf(stderr, "%s %d %s: ", timeStr, line, log_level_to_string(level));
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, "\n");
}
// DEBUG level
#define raise_debug_1(fmt) \
log_message(LOG_LEVEL_DEBUG, __LINE__, fmt)
#define raise_debug_2(fmt, ...) \
log_message(LOG_LEVEL_DEBUG, __LINE__, fmt, __VA_ARGS__)
#define raise_debug(...) \
PP_CAT(raise_debug_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// LOG level
#define raise_log_1(fmt) \
log_message(LOG_LEVEL_LOG, __LINE__, fmt)
#define raise_log_2(fmt, ...) \
log_message(LOG_LEVEL_LOG, __LINE__, fmt, __VA_ARGS__)
#define raise_log(...) \
PP_CAT(raise_log_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// INFO level
#define raise_info_1(fmt) \
log_message(LOG_LEVEL_INFO, __LINE__, fmt)
#define raise_info_2(fmt, ...) \
log_message(LOG_LEVEL_INFO, __LINE__, fmt, __VA_ARGS__)
#define raise_info(...) \
PP_CAT(raise_info_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// NOTICE level
#define raise_notice_1(fmt) \
log_message(LOG_LEVEL_NOTICE, __LINE__, fmt)
#define raise_notice_2(fmt, ...) \
log_message(LOG_LEVEL_NOTICE, __LINE__, fmt, __VA_ARGS__)
#define raise_notice(...) \
PP_CAT(raise_notice_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// WARN level
#define raise_warn_1(fmt) \
log_message(LOG_LEVEL_WARN, __LINE__, fmt)
#define raise_warn_2(fmt, ...) \
log_message(LOG_LEVEL_WARN, __LINE__, fmt, __VA_ARGS__)
#define raise_warn(...) \
PP_CAT(raise_warn_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// EXCEPTION level
#define raise_exception_1(fmt) \
log_message(LOG_LEVEL_EXCEPTION, __LINE__, fmt)
#define raise_exception_2(fmt, ...) \
log_message(LOG_LEVEL_EXCEPTION, __LINE__, fmt, __VA_ARGS__)
#define raise_exception(...) \
PP_CAT(raise_exception_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
#endif // EPRINTF_H

12
package/c/lib/test/test.c Normal file
View File

@@ -0,0 +1,12 @@
#include <assert.h>
#include <stdio.h>
#include "heclib.h"
void test_function() {
}
int main(void) {
test_function();
printf("All tests passed\n");
return 0;
}

View File

@@ -3,7 +3,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include "macros.h" #include "macro.h"
typedef enum { typedef enum {
LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG,

View File

@@ -0,0 +1,2 @@
#include "macro.h"

View File

@@ -11,16 +11,24 @@ typedef enum {
COLOR_MODE_DISABLE COLOR_MODE_DISABLE
} ColorMode; } ColorMode;
// Function to set the color mode // Static color mode variable
void set_output_color_mode(ColorMode mode); 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 // Macros for detecting terminal and color usage
#define IS_TERMINAL() (isatty(fileno(stderr))) #define IS_TERMINAL() (isatty(fileno(stderr)))
#define USE_COLOR() ((color_mode == COLOR_MODE_FORCE) || (color_mode == COLOR_MODE_AUTO && IS_TERMINAL())) #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 color macros based on output type
#define ERROR_PREFIX (IS_TERMINAL() ? "\033[1;31mError: " : "Error: ") #define ERROR_PREFIX PP_CAT_I(COLOR_RED, "Error: ")
#define ERROR_SUFFIX (IS_TERMINAL() ? "\033[0m\n" : "\n") #define ERROR_SUFFIX PP_CAT_I(COLOR_RESET, "\n")
// Helper macros for argument counting // Helper macros for argument counting
// NOTE(yukkop): this ugly macroses for avoid all posible warnings // NOTE(yukkop): this ugly macroses for avoid all posible warnings
@@ -42,4 +50,6 @@ void set_output_color_mode(ColorMode mode);
#define eprintf(...) \ #define eprintf(...) \
PP_CAT(eprintf_, PP_NARG(__VA_ARGS__))(__VA_ARGS__) 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 #endif // EPRINTF_H

View File

@@ -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;
}

View File

@@ -2,10 +2,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "logger.c" #include "logger.c"
#include "macros.h" #include "macro.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include <stdbool.h>
// TODO: check on the specific psql version // TODO: check on the specific psql version
int check_psql_installed(void) { int check_psql_installed(void) {
@@ -58,7 +59,7 @@ void create_migration_inner(const char* migration_path, const char* type) {
} }
char filename[1024 + 19]; 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"); FILE *fp = fopen(filename, "w");
if (!fp) { if (!fp) {
@@ -101,9 +102,10 @@ void help_message(char name[]) {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
srand(time(NULL)); srand(time(NULL));
init_logger(); init_logger();
raise_log("init"); raise_log("init");
todo;
if (check_psql_installed()) { exit(1); } if (check_psql_installed()) { exit(1); }
if (argc < 2) { if (argc < 2) {
@@ -115,7 +117,9 @@ int main(int argc, char *argv[]) {
char *migration_dir; char *migration_dir;
char *db_url; char *db_url;
char *migration_name; 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 */ /* Process global options until a known subcommand is encountered */
int i = 1; int i = 1;
@@ -171,11 +175,18 @@ int main(int argc, char *argv[]) {
force = 1; force = 1;
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--set")) { } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--set")) {
if (i+1 < argc) { 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 { } else {
eprintf("%s requires an argument", argv[i]); 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) { } else if (strcmp(subcommand, "fetch") == 0) {
@@ -188,13 +199,12 @@ int main(int argc, char *argv[]) {
eprintf("%s requires an argument", argv[i]); 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);
}
} }