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

Variables #7

Merged
merged 3 commits into from
Apr 11, 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
31 changes: 28 additions & 3 deletions cognac_gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ EOF

cat <<EOF
if (!strcmp("$l", av[i])) {
json_object *jobj;
auto_osc_json_c json_object *jobj = NULL;
auto_ptr_array struct ptr_array opa = {0};
struct ptr_array *pa = &opa;
struct osc_${snake_l}_arg a = {0};
Expand Down Expand Up @@ -346,7 +346,7 @@ EOF
goto ${snake_l}_arg;
}

if (i + 1 < ac && av[i + 1][0] == '-' && av[i + 1][1] == '-') {
if (i + 1 < ac && av[i + 1][0] == '-' && av[i + 1][1] == '-' && strcmp(av[i + 1] + 2, "set-var")) {
char *next_a = &av[i + 1][2];
char *str = next_a;
char *aa = i + 2 < ac ? av[i + 2] : 0;
Expand All @@ -365,7 +365,18 @@ EOF
++incr;
aa = read_file(files_cnt, av[i + 3], 1);
STRY(!aa);

} else if (!strcmp(aa, "--var")) {
TRY(i + 3 >= ac, "var name require");
int var_found = 0;
for (int j = 0; j < nb_cli_vars; ++j) {
if (!strcmp(cli_vars[j].name, av[i + 3])) {
var_found = 1;
aa = cli_vars[j].val;
}
}
TRY(!var_found, "--var could not find osc variable '%s'", av[i + 3]);
++incr;
STRY(!aa);
} else {
aa = 0;
incr = 1;
Expand Down Expand Up @@ -399,14 +410,28 @@ EOF
cret = osc_$snake_l(&e, &r, &a);
TRY(cret, "fail to call $l: %s\n", curl_easy_strerror(cret));
CHK_BAD_RET(!r.buf, "connection sucessful, but empty responce\n");
jobj = NULL;
if (program_flag & OAPI_RAW_OUTPUT)
puts(r.buf);
else {
jobj = json_tokener_parse(r.buf);
puts(json_object_to_json_string_ext(jobj,
JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE |
color_flag));
}
if (i + 1 < ac && !strcmp(av[i + 1], "--set-var")) {
++i;
TRY(i + 1 >= ac, "--set-var require an argument");
if (!jobj)
jobj = json_tokener_parse(r.buf);
if (parse_variable(jobj, av, ac, i))
return -1;
++i;
}

if (jobj) {
json_object_put(jobj);
jobj = NULL;
}
osc_deinit_str(&r);
} else
Expand Down
2 changes: 1 addition & 1 deletion function.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int osc_____snake_func____(struct osc_env *e, struct osc_str *out, struct osc___
curl_easy_setopt(e->c, CURLOPT_POSTFIELDS, r ? data.buf : "");
curl_easy_setopt(e->c, CURLOPT_WRITEDATA, out);
if (e->flag & OSC_VERBOSE_MODE) {
printf("<Date send to curl>\n%s\n</Date send to curl>\n", data.buf);
printf("<Data send to curl>\n%s\n</Data send to curl>\n", data.buf);
}
res = curl_easy_perform(e->c);
out:
Expand Down
24 changes: 10 additions & 14 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,6 @@ const char **osc_calls_name(void)
#define THREAD_LOCAL
#endif

static void osc_json_c_obj_put(json_object **js)
{
if (!*js)
return;
json_object_put(*js);
}

#define auto_osc_json_c_obj_put __attribute__((cleanup(osc_json_c_obj_put)))


static THREAD_LOCAL const char *cfg_path;

void osc_set_cfg_path(const char *cfg)
Expand Down Expand Up @@ -294,7 +284,7 @@ int osc_load_ak_sk_from_conf(const char *profile, char **ak, char **sk)
char buf[1024];
const char *cfg = cfg_path;
struct json_object *js, *ak_js, *sk_js;
auto_osc_json_c_obj_put struct json_object *to_free = NULL;
auto_osc_json_c struct json_object *to_free = NULL;

if (!ak && !sk)
return 0;
Expand Down Expand Up @@ -330,7 +320,7 @@ int osc_load_loging_password_from_conf(const char *profile,
{
char buf[1024];
const char *cfg = cfg_path;
auto_osc_json_c_obj_put struct json_object *to_free = NULL;
auto_osc_json_c struct json_object *to_free = NULL;
struct json_object *js, *login_js, *pass_js;

if (!email && !password)
Expand Down Expand Up @@ -370,7 +360,7 @@ int osc_load_region_from_conf(const char *profile, char **region)
const char *cfg = cfg_path;
char buf[1024];
struct json_object *js;
auto_osc_json_c_obj_put struct json_object *to_free = NULL;
auto_osc_json_c struct json_object *to_free = NULL;

if (!cfg) {
LOAD_CFG_GET_HOME(buf);
Expand All @@ -395,7 +385,7 @@ int osc_load_cert_from_conf(const char *profile, char **cert, char **key)
{
struct json_object *cert_obj, *key_obj, *js;
const char *cfg = cfg_path;
auto_osc_json_c_obj_put struct json_object *to_free = NULL;
auto_osc_json_c struct json_object *to_free = NULL;
char buf[1024];
int ret = 0;

Expand Down Expand Up @@ -447,6 +437,12 @@ void osc_init_str(struct osc_str *r)
r->buf = NULL;
}

void osc_deinit_json_c(json_object **j)
{
if (j && *j)
json_object_put(*j);
}

void osc_deinit_str(struct osc_str *r)
{
free(r->buf);
Expand Down
11 changes: 11 additions & 0 deletions lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ extern "C" {
#define auto_osc_str __attribute__((cleanup(osc_deinit_str)))
#define auto_osc_env __attribute__((cleanup(osc_deinit_sdk)))

/*
* Helper for json C
*/
#define auto_osc_json_c __attribute__((cleanup(osc_deinit_json_c)))

#endif

struct osc_str {
Expand Down Expand Up @@ -141,6 +146,12 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile,
unsigned int flag, struct osc_env_conf *cfg);
void osc_deinit_sdk(struct osc_env *e);

struct json_object;

typedef struct json_object json_object;

void osc_deinit_json_c(json_object **j);

int osc_str_append_string(struct osc_str *osc_str, const char *str);
int osc_str_append_n_string(struct osc_str *osc_str, const char *str, int l);

Expand Down
75 changes: 69 additions & 6 deletions main_tpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,24 @@

#define TRY(f, args...) \
do { \
if (f) {fprintf(stderr, args); return 1;} \
if (f) {fprintf(stderr, args); return -1;} \
} while(0)


#define VAR_NAME_SIZE 128
#define VAR_VAL_SIZE 512
#define VAR_ARRAY_SIZE 128

static int nb_cli_vars;

struct cli_var {
char name[VAR_NAME_SIZE];
char val[VAR_VAL_SIZE];
} cli_vars[VAR_ARRAY_SIZE];

static void *cascade_struct;
static int (*cascade_parser)(void *, char *, char *, struct ptr_array *);

static int argcmp2(const char *s1, const char *s2, char dst)
{
while (*s1 == *s2 && *s1 && *s2) {
Expand Down Expand Up @@ -111,6 +125,55 @@ char *string_to_jsonstr(char **file_str_p)
return *file_str_p;
}

static int parse_variable(json_object *jobj, char **av, int ac, int i)
{
const char *tmp = av[i + 1];
const char *tmp2;
TRY(nb_cli_vars >= VAR_ARRAY_SIZE, "variable asignement fail: too much variables");
struct cli_var *var = &cli_vars[nb_cli_vars++];
json_object *j = jobj;
char buf[512];

tmp2 = strchr(tmp, '=');
TRY(!tmp2, "variable asignement fail (missing '='))\n");
TRY((uintptr_t)(tmp2 - tmp) >= VAR_NAME_SIZE, "var name too long");
strncpy(var->name, tmp, tmp2 - tmp);
var->name[tmp2 - tmp] = 0;
tmp = tmp2 + 1;

while ((tmp2 = strchr(tmp, '.')) != NULL) {
char *end = NULL;
// get json
int idx = strtoul(tmp, &end, 0);
if (end != tmp) {
j = json_object_array_get_idx(j, idx);
} else {
TRY((uintptr_t)(tmp2 - tmp) >= sizeof buf - 1,
"variable asignement fail");
strncpy(buf, tmp, tmp2 - tmp);
buf[tmp2 - tmp] = 0;
j = json_object_object_get(j, buf);
}
TRY(!j, "variable asignement fail (not found)");
tmp = tmp2 + 1;
}
tmp2 = tmp + strlen(tmp);
TRY((uintptr_t)(tmp2 - tmp) >= sizeof buf - 1,
"variable asignement fail");
strncpy(buf, tmp, tmp2 - tmp);
buf[tmp2 - tmp] = 0;
j = json_object_object_get(j, buf);
if (json_object_is_type(j, json_type_string)) {
tmp = json_object_get_string(j);
} else {
tmp = json_object_to_json_string_ext(j, JSON_C_TO_STRING_PLAIN);
}
TRY(strlen(tmp) >= VAR_VAL_SIZE, "variable asignement fail: value too big");
strcpy(var->val, tmp);
return 0;

}

char *read_file(char *files_cnt[static MAX_FILES_PER_CMD], char *file_name,
int is_json)
{
Expand Down Expand Up @@ -161,10 +224,6 @@ char *read_file(char *files_cnt[static MAX_FILES_PER_CMD], char *file_name,
return NULL;
}


static void *cascade_struct;
static int (*cascade_parser)(void *, char *, char *, struct ptr_array *);

____complex_struct_func_parser____


Expand Down Expand Up @@ -306,14 +365,18 @@ int main(int ac, char **av)

if (ac < 2 || (ac == 2 && !strcmp(av[1], "--help"))) {
show_help:
printf("Usage: %s [--help] CallName [options] [--Params <param_argument | [--file | --jsonstr-file] <file_name>>]\n"
printf("Usage: %s [--help] CallName [options] [--Params <param_argument | [--file | --jsonstr-file] <file_name>>] | --var <variable_name>\n"
"options:\n"
"\t --auth-method=METHODE set authentification method, password|accesskey|none\n"
"\t --color try to colorize json if json-c support it\n"
"\t --config=PATH config file path\n"
"\t --file PATH use content of PATH as an agrument for a call, example:\n"
"\t\t\t\toapi-cli CreateCa --CaPem --file /$CA_DIR/cert.pem\n"
"\t --jsonstr-file PATH same as --file, except the content is surrounded by \"\n"
"\t --set-var ID=VARIABLE_PATH Create an oapi-cli variable, that can be use with --var\n"
"\t\t\t\tExamples: ./oapi-cli ReadVms --Filters.TagValues[] VM_NAME --set-var id=Vms.0.VmId ReadVms --Filters.VmIds[] --var id\n"
"\t\t\t\twill find the vm with VM_NAME as it's tag, and read it again, but using it's VmId as filter this time\n"
"\t --var use variabble content created by --set-var\n"
"\t\t\t\tand \" inside the file are escape with a \\, this option is useful for CreatePolicy\n"
"\t-h, --help [CallName] this, can be used with call name, example:\n\t\t\t\t%s --help ReadVms\n"
"\t --list-calls list all calls\n"
Expand Down
Loading