From 7d445ee27b615e291da3b63ac5cfb3fd45b6e905 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 4 Oct 2023 17:15:15 +0200 Subject: [PATCH] Override configuration with env variables --- src/main/db.c | 20 ++++++++++++-------- src/main/include/config.h | 8 +------- src/main/utils.c | 19 ++++++++++++------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/main/db.c b/src/main/db.c index 76ca3a6..1e96b1f 100644 --- a/src/main/db.c +++ b/src/main/db.c @@ -25,8 +25,17 @@ void sqlAssert(int ret, sqlite3* db, char* file, int line) { char* get_db_path() { - #ifndef DB_FILE - //! We should check that $HOME/.config already exists even if it should be the case + /* + Checks if $TAKL_DB env variable is set + else, the DB is located at $HOME/.config/takl.sqlite3 + */ + char* env_db_path = getenv("TAKL_DB"); + if (env_db_path) { + char* db_path = malloc(sizeof(char)*(strlen(env_db_path)+1)); + strcpy(db_path, env_db_path); + + return db_path; + } else { char* base_path = ".config/takl.sqlite3"; char* home_dir = getenv("HOME"); @@ -36,12 +45,7 @@ char* get_db_path() { sprintf(db_path, "%s/%s", home_dir, base_path); return db_path; - #else - char* db_path = malloc(sizeof(char)*(strlen(DB_FILE)+1)); - memcpy(db_path, DB_FILE, sizeof(char)*(strlen(DB_FILE)+1)); - - return db_path; - #endif + } } diff --git a/src/main/include/config.h b/src/main/include/config.h index eb1bf8e..9d5ea76 100644 --- a/src/main/include/config.h +++ b/src/main/include/config.h @@ -1,13 +1,7 @@ #ifndef DEF_CONFIG_H #define DEF_CONFIG_H -#define VERSION "1.3.0" - -// By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here -//#define DB_FILE "takl.sqlite3" - -// By default, /tmp/takl.$USER is used. You can change this behaviour here -//#define SOCKET_FILE "takl.sock" +#define VERSION "1.3.1" #define MAX_TASK_ID 10000 // max is set to MAX_TASK_ID-1 #define NEW_TASK_ID_MAX_RETRIES 10000 // number of retries before giving up diff --git a/src/main/utils.c b/src/main/utils.c index 9fca405..6b470af 100644 --- a/src/main/utils.c +++ b/src/main/utils.c @@ -8,7 +8,17 @@ #include "include/struct.h" char* get_socket_path() { - #ifndef SOCKET_FILE + /* + Checks if $TAKL_SOCKET env variable is set + else, the socket is located at /tmp/takl + */ + char* env_socket_path = getenv("TAKL_SOCKET"); + if (env_socket_path) { + char* socket_path = malloc(sizeof(char)*(strlen(env_socket_path)+1)); + strcpy(socket_path, env_socket_path); + + return socket_path; + } else { char* base_path = "/tmp/takl"; char* username = getenv("USER"); @@ -18,12 +28,7 @@ char* get_socket_path() { sprintf(socket_path, "%s.%s", base_path, username); return socket_path; - #else - char* socket_path = malloc(sizeof(char)*(strlen(SOCKET_FILE)+1)); - memcpy(socket_path, SOCKET_FILE, sizeof(char)*(strlen(SOCKET_FILE)+1)); - - return socket_path; - #endif + } }