From 145115b5eb1278ec43822e962ad574e0749fd916 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 12 Jan 2024 17:45:03 +0100 Subject: [PATCH] update osc_sdk_c for better conf handling Signed-off-by: Matthias Gatto --- README.md | 2 +- osc-sdk-C | 2 +- osc_sdk.c | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 978a7f5..d17accb 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ source $HOME/oapi-cli-completion.bash # Config -## using `.osc/config.json` +## using `~/.osc/config.json` on unix/macOS, or .\config.json on windows ``` {"default": diff --git a/osc-sdk-C b/osc-sdk-C index 0b37f1e..9ea0e48 160000 --- a/osc-sdk-C +++ b/osc-sdk-C @@ -1 +1 @@ -Subproject commit 0b37f1e6783dc0640de9338153cbff92b6779446 +Subproject commit 9ea0e48ba4f4f3c663868d791fb0e10ffdc61347 diff --git a/osc_sdk.c b/osc_sdk.c index ad4b45d..b866789 100644 --- a/osc_sdk.c +++ b/osc_sdk.c @@ -39,6 +39,7 @@ #include #include "curl/curl.h" #include +#include #include "osc_sdk.h" #include "json.h" @@ -48,8 +49,6 @@ #define TIME_HDR_KEY "X-Osc-Date: " #define TIME_HDR_KEY_L (sizeof TIME_HDR_KEY) -#define CFG_FILE "/.osc/config.json" - #ifdef _WIN32 #define SAFE_C 0 @@ -63,8 +62,16 @@ static inline char* stpcpy(char *dest, const char *src) return dest; } +#define CFG_FILE "config.json" + +#define LOAD_CFG_GET_HOME(buf) \ + { \ + strcpy(buf, CFG_FILE); \ + } #else +#define CFG_FILE "/.osc/config.json" + #define SAFE_C 1 #endif @@ -4084,6 +4091,7 @@ static char *osc_strdup(const char *str) { if (count_args++ > 0) \ STRY(osc_str_append_string(data, "," )); +#ifndef LOAD_CFG_GET_HOME #define LOAD_CFG_GET_HOME(buf) \ { \ const char *dest = CFG_FILE; \ @@ -4093,6 +4101,7 @@ static char *osc_strdup(const char *str) { "home path too big"); \ strcpy(stpcpy(buf, home), dest); \ } +#endif #define ARG_TO_JSON_STR(separator, what) do { \ auto_osc_str struct osc_str s; \ @@ -4138,8 +4147,9 @@ int osc_load_ak_sk_from_conf(const char *profile, char **ak, char **sk) *sk = NULL; if (ak) *ak = NULL; + TRY(access(cfg, R_OK), "can't open/read %s\n", cfg); js = json_object_from_file(cfg); - TRY(!js, "can't open %s\n", cfg); + TRY(!js, "can't load json-file %s (json might have incorect syntaxe)\n", cfg); to_free = js; js = json_object_object_get(js, profile); TRY(!js, "can't find profile %s\n", profile);