Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update osc_sdk_c for better conf handling #42

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion osc-sdk-C
Submodule osc-sdk-C updated 2 files
+1 −1 COGNAC
+13 −3 osc_sdk.c
16 changes: 13 additions & 3 deletions osc_sdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <assert.h>
#include "curl/curl.h"
#include <time.h>
#include <unistd.h>
#include "osc_sdk.h"
#include "json.h"

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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; \
Expand All @@ -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; \
Expand Down Expand Up @@ -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);
Expand Down
Loading