Skip to content

Commit

Permalink
update osc_sdk_c for better conf handling
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <[email protected]>
  • Loading branch information
outscale-mgo committed Jan 12, 2024
1 parent f9333fd commit 145115b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
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

0 comments on commit 145115b

Please sign in to comment.