From 65e1ce3cd376e701705b6593e9531f05032b9ce4 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 8 Nov 2024 14:25:04 +0100 Subject: [PATCH 01/29] add an option to funclist to generate function from paths Signed-off-by: Matthias Gatto --- bin/funclist.c | 56 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/bin/funclist.c b/bin/funclist.c index b34cf5a..18ae72a 100644 --- a/bin/funclist.c +++ b/bin/funclist.c @@ -1,34 +1,58 @@ +#include #include #include #include "json.h" int main(int ac, char **av) { + bool funclist_from_path = ac > 2; struct json_object *j_file = json_object_from_file(av[1]); - struct json_object *compo; - struct json_object *schema; int ret = 0; - ret = json_object_object_get_ex(j_file, "components", &compo); - if (!ret) - return 1; - json_object_object_get_ex(compo, "schemas", &schema); - if (!ret) - return 1; + int func_ret = 1; int first = 1; - json_object_object_foreach(schema, k, v_) { - char *new_k = strdup(k); - char *resquest = strstr(new_k, "Request"); - if (resquest) { + if (!funclist_from_path) { + struct json_object *compo; + struct json_object *schema; + + ret = json_object_object_get_ex(j_file, "components", &compo); + if (!ret) + goto err; + json_object_object_get_ex(compo, "schemas", &schema); + if (!ret) + goto err; + + json_object_object_foreach(schema, k, v_) { + char *new_k = strdup(k); + char *resquest = strstr(new_k, "Request"); + + if (resquest) { + if (!first) + putchar(' '); + first = 0; + *resquest = 0; + printf("%s", new_k); + } + free(new_k); + } + } else { + struct json_object *paths; + + ret = json_object_object_get_ex(j_file, "paths", &paths); + if (!ret) + goto err; + + json_object_object_foreach(paths, k, v_) { if (!first) putchar(' '); first = 0; - *resquest = 0; - printf("%s", new_k); + printf("%s", k+1); } - free(new_k); } + + func_ret = 0; +err: json_object_put(j_file); - return 0; + return func_ret; } From 651460a06755f2f3d16f01e8c17ba7a98817648a Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 8 Nov 2024 15:58:42 +0100 Subject: [PATCH 02/29] remove argument-list.json Signed-off-by: Matthias Gatto --- Makefile | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index ee5066a..da9f369 100644 --- a/Makefile +++ b/Makefile @@ -36,16 +36,16 @@ bin/funclist: bin/funclist.c bin/line_check: bin/line_check.c $(CC) -O3 bin/line_check.c -o bin/line_check -main.c: bin/line_check osc-api.json call_list arguments-list.json config.sh main_tpl.c cognac_gen.sh mk_args.c.sh +main.c: bin/line_check osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh ./cognac_gen.sh main_tpl.c main.c c -osc_sdk.c: bin/line_check osc-api.json call_list arguments-list.json config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh +osc_sdk.c: bin/line_check osc-api.json call_list config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh ./cognac_gen.sh lib.c osc_sdk.c c -osc_sdk.h: bin/line_check osc-api.json call_list arguments-list.json config.sh lib.h cognac_gen.sh mk_args.c.sh +osc_sdk.h: bin/line_check osc-api.json call_list config.sh lib.h cognac_gen.sh mk_args.c.sh ./cognac_gen.sh lib.h osc_sdk.h c -$(CLI_NAME)-completion.bash: bin/line_check osc-api.json call_list arguments-list.json config.sh oapi-cli-completion-tpl.bash cognac_gen.sh +$(CLI_NAME)-completion.bash: bin/line_check osc-api.json call_list config.sh oapi-cli-completion-tpl.bash cognac_gen.sh ./cognac_gen.sh oapi-cli-completion-tpl.bash $(CLI_NAME)-completion.bash bash config.sh: @@ -53,18 +53,12 @@ config.sh: echo $(SED_ALIAS) >> config.sh echo "export CLI_NAME=$(CLI_NAME)" >> config.sh -arguments-list.json: osc-api.json - $(JSON_SEARCH) -s Request osc-api.json | $(JSON_SEARCH) -K properties \ - | sed 's/]/ /g' \ - | tr -d "\n[],\"" | sed -r 's/ +/ \n/g' \ - | sort | uniq | tr -d "\n" > arguments-list.json - call_list: osc-api.json bin/funclist - bin/funclist osc-api.json > call_list + bin/funclist osc-api.json $(FUNCLIST_ARG) > call_list clean: - rm -vf osc-api.json call_list osc_sdk.c arguments-list.json osc_sdk.h main.c $(CLI_NAME) config.sh $(CLI_NAME)-completion.bash bin/line_check bin/funclist + rm -vf osc-api.json call_list osc_sdk.c osc_sdk.h main.c $(CLI_NAME) config.sh $(CLI_NAME)-completion.bash bin/line_check bin/funclist .PHONY: clean list_api_version help make_cli From d6bad3534edad1b84b1d649460542fef86cb334c Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Thu, 14 Nov 2024 13:30:52 +0100 Subject: [PATCH 03/29] add debug function Signed-off-by: Matthias Gatto --- Makefile | 4 ++++ configure | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/Makefile b/Makefile index da9f369..71c1c6e 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,10 @@ config.sh: echo "alias json-search=$(JSON_SEARCH)" > config.sh echo $(SED_ALIAS) >> config.sh echo "export CLI_NAME=$(CLI_NAME)" >> config.sh + echo -e "debug()\n{" >> config.sh + echo -e '\tif [[ "$$DEBUG_MODE" == "1" ]] ; then echo "$$@" >&2 ; fi\n}' >> config.sh + echo export DEBUG_MODE=$(DEBUG_MODE) >> config.sh + echo 'debug "debug mode is on"' >> config.sh call_list: osc-api.json bin/funclist bin/funclist osc-api.json $(FUNCLIST_ARG) > call_list diff --git a/configure b/configure index 7de164b..5478a23 100755 --- a/configure +++ b/configure @@ -28,6 +28,7 @@ for arg in $@; do --wget-json-search wget json-search.Appimage --gnu-sed-alias use gsed for sed --cli-name=Name set cli name, default: oapi-cli + --debug-mode enable debug in cognac (output on stderr) --api-script=script how to generate osc-api.json --yq-go use go version of yq, that use diferents arguments --curl-path=PATH path to curl source root @@ -68,6 +69,8 @@ EOF YQ_ARG="-o json" elif [ "--gnu-sed-alias" = $arg ]; then SED_ALIAS="'alias sed=gsed'" + elif [ "--debug-mode" = $arg ]; then + DEBUG_MODE=1 elif [ "--target-api" = $( echo "$arg" | cut -d '=' -f 1) ]; then API_VERSION=$( echo $arg | cut -f 2 -d '=' ) elif [ "--cli-name" = $( echo "$arg" | cut -d '=' -f 1) ]; then @@ -114,6 +117,7 @@ echo JSON_C_CFLAGS=$JSON_C_CFLAGS >> config.mk echo JSON_C_RULE=$JSON_C_RULE >> config.mk echo API_VERSION=$API_VERSION >> config.mk echo CLI_NAME=$CLI_NAME >> config.mk +echo DEBUG_MODE=$DEBUG_MODE >> config.mk echo -e "osc-api.json:: config.sh\n\t$API_SCRIPT" >> config.mk echo $SDK_VERSION > sdk-version echo "config done:" From 85afe61870b8fe704ac32a4414812e1244900a15 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Thu, 14 Nov 2024 18:10:19 +0100 Subject: [PATCH 04/29] add some debug Signed-off-by: Matthias Gatto --- Makefile | 1 - cognac_gen.sh | 9 +++++++++ configure | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 71c1c6e..d7f5405 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,6 @@ config.sh: echo -e "debug()\n{" >> config.sh echo -e '\tif [[ "$$DEBUG_MODE" == "1" ]] ; then echo "$$@" >&2 ; fi\n}' >> config.sh echo export DEBUG_MODE=$(DEBUG_MODE) >> config.sh - echo 'debug "debug mode is on"' >> config.sh call_list: osc-api.json bin/funclist bin/funclist osc-api.json $(FUNCLIST_ARG) > call_list diff --git a/cognac_gen.sh b/cognac_gen.sh index b9f117c..71ea7e6 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -11,6 +11,8 @@ shopt -s expand_aliases source ./helper.sh +debug "debug mode is on" + dash_this_arg() { local cur="$1" @@ -210,8 +212,10 @@ replace_args() arg_check=$(bin/line_check ____args____ ____func_code____ ____functions_proto____ ____cli_parser____ ____complex_struct_func_parser____ ____complex_struct_to_string_func____ ____call_list_dec____ ____call_list_descriptions____ ____call_list_args_descriptions____ <<< "$line") if [ "$arg_check" == "____args____" ]; then + debug "____args____" ./mk_args.${lang}.sh elif [ "$arg_check" == "____call_list_descriptions____" ]; then + debug "____call_list_descriptions____" DELIMES=$(cut -d '(' -f 2 <<< $line | tr -d ')') D1=$(cut -d ';' -f 1 <<< $DELIMES | tr -d "'") D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'") @@ -228,6 +232,7 @@ replace_args() done echo -ne $D3 elif [ "$arg_check" == "____call_list_args_descriptions____" ]; then + debug "____call_list_args_descriptions____" DELIMES=$(cut -d '(' -f 2 <<< $line | tr -d ')') D1=$(cut -d ';' -f 1 <<< $DELIMES | tr -d "'") D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'") @@ -247,6 +252,7 @@ replace_args() done echo -ne $D3 elif [ "$arg_check" == "____call_list_dec____" ]; then + debug "____call_list_dec____" DELIMES=$(cut -d '(' -f 2 <<< $line | tr -d ')') D1=$(cut -d ';' -f 1 <<< $DELIMES | tr -d "'") D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'") @@ -349,6 +355,7 @@ EOF done elif [ "$arg_check" == "____cli_parser____" ] ; then + debug "____cli_parser____" for l in $CALL_LIST; do snake_l=$(to_snakecase <<< $l) arg_list=$(json-search ${l}Request < osc-api.json \ @@ -457,11 +464,13 @@ EOF EOF done elif [ "$arg_check" == "____functions_proto____" ] ; then + debug "____functions_proto____" for l in $CALL_LIST; do local snake_l=$(to_snakecase <<< $l) echo "int osc_${snake_l}(struct osc_env *e, struct osc_str *out, struct osc_${snake_l}_arg *args);" done elif [ "$arg_check" == "____func_code____" ]; then + debug "____func_code____" for x in $CALL_LIST; do local snake_x=$(to_snakecase <<< $x) local args=$(json-search ${x}Request < osc-api.json \ diff --git a/configure b/configure index 5478a23..3ebaf27 100755 --- a/configure +++ b/configure @@ -17,6 +17,7 @@ API_VERSION=master SDK_VERSION=0xC061AC CLI_NAME=oapi-cli API_SCRIPT='curl -s https://raw.githubusercontent.com/outscale/osc-api/$(API_VERSION)/outscale.yaml | yq $(YQ_ARG) > osc-api.json' +DEBUG_MODE=0 inc=1 for arg in $@; do From 2416cfe63150c1287b5d9d56f43944e8de15013d Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Thu, 14 Nov 2024 18:10:50 +0100 Subject: [PATCH 05/29] enable to choose prefix for Function Signed-off-by: Matthias Gatto --- Makefile | 1 + bin/osc-api-seems-valid.sh | 9 +++++++-- cognac_gen.sh | 29 ++++++++++++++++++++++------- configure | 11 +++++++++++ construct_data.c.sh | 2 +- helper.sh | 2 +- mk_args.c.sh | 4 ++-- 7 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index d7f5405..d2133db 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ $(CLI_NAME)-completion.bash: bin/line_check osc-api.json call_list config.sh oap config.sh: echo "alias json-search=$(JSON_SEARCH)" > config.sh echo $(SED_ALIAS) >> config.sh + echo FUNCTION_SUFFIX=$(FUNCTION_SUFFIX) >> config.sh echo "export CLI_NAME=$(CLI_NAME)" >> config.sh echo -e "debug()\n{" >> config.sh echo -e '\tif [[ "$$DEBUG_MODE" == "1" ]] ; then echo "$$@" >&2 ; fi\n}' >> config.sh diff --git a/bin/osc-api-seems-valid.sh b/bin/osc-api-seems-valid.sh index 85640c7..b2c3ae0 100755 --- a/bin/osc-api-seems-valid.sh +++ b/bin/osc-api-seems-valid.sh @@ -6,8 +6,13 @@ config_path=$(realpath $(dirname $0))/../config.sh shopt -s expand_aliases source $config_path -test=$(json-search -sn Request $1 | json-search -n properties) -test_ret=$? +if [ -z "${FUNCTION_SUFFIX}" ]; then + test=$(json-search -n properties $1) + test_ret=$? +else + test=$(json-search -sn ${FUNCTION_SUFFIX} $1 | json-search -n properties) + test_ret=$? +fi if [ "$test_ret" == "1" -o "$test" == "null" ]; then echo "$1 is invalide" >&2 diff --git a/cognac_gen.sh b/cognac_gen.sh index 71ea7e6..615a76a 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -222,7 +222,7 @@ replace_args() D3=$(cut -d ';' -f 3 <<< $DELIMES | tr -d "'") for x in $CALL_LIST ; do echo -en $D1 - local required=$(json-search ${x}Request < osc-api.json | json-search required 2>&1 | tr -d '[]\n"' | tr -s ' ' | sed 's/nothing found//g') + local required=$(json-search ${x}${FUNCTION_SUFFIX} < osc-api.json | json-search required 2>&1 | tr -d '[]\n"' | tr -s ' ' | sed 's/nothing found//g') local usage_required=$( for a in $(echo $required | tr -d ','); do echo -n " --${a}=${a,,}"; done ) local usage="\"Usage: oapi-cli $x ${usage_required} [OPTIONS]\n\"" local call_desc=$(jq .paths.\""/$x"\".description < osc-api.json | sed 's/
//g' | tr -d '"' | fold -s | sed 's/^/"/;s/$/\\n"/') @@ -238,7 +238,7 @@ replace_args() D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'") D3=$(cut -d ';' -f 3 <<< $DELIMES | tr -d "'") for x in $CALL_LIST ; do - st_info=$(json-search -s ${x}Request < osc-api.json) + st_info=$(json-search -s ${x}${FUNCTION_SUFFIX} < osc-api.json) A_LST=$(json-search -K properties <<< $st_info | tr -d '",[]') echo -en $D1 @@ -264,7 +264,17 @@ replace_args() done echo -ne $D3 elif [ "$arg_check" == "____complex_struct_to_string_func____" ]; then - COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v Request) + debug "____complex_struct_to_string_func____" + local CALLS=$(for c in $(cat call_list) ; do echo ${c}${FUNCTION_SUFFIX} ; done) + COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response) + local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) + + debug "CALL_LIST: $CALLS" + debug "COMPLEX_STRUCT: $COMPLEX_STRUCT" + debug "diff:" "$DIFF" + + COMPLEX_STRUCT="$DIFF" + debug "COMPLEX_STRUCT NOW:" "$COMPLEX_STRUCT" for s in $COMPLEX_STRUCT; do struct_name=$(to_snakecase <<< $s) @@ -293,7 +303,12 @@ EOF fi done elif [ "$arg_check" == "____complex_struct_func_parser____" ]; then - COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v Request) + debug "____complex_struct_func_parser____" + local CALLS=$(for c in $(cat call_list) ; do echo ${c}${FUNCTION_SUFFIX} ; done) + COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response) + local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) + + COMPLEX_STRUCT="$DIFF" # prototypes for s in $COMPLEX_STRUCT; do @@ -358,7 +373,7 @@ EOF debug "____cli_parser____" for l in $CALL_LIST; do snake_l=$(to_snakecase <<< $l) - arg_list=$(json-search ${l}Request < osc-api.json \ + arg_list=$(json-search ${l}${FUNCTION_SUFFIX} < osc-api.json \ | json-search -K properties \ | tr -d "[]\"," | sed '/^$/d') @@ -473,7 +488,7 @@ EOF debug "____func_code____" for x in $CALL_LIST; do local snake_x=$(to_snakecase <<< $x) - local args=$(json-search ${x}Request < osc-api.json \ + local args=$(json-search ${x}${FUNCTION_SUFFIX} < osc-api.json \ | json-search -K properties | tr -d "[]\",") dashed_args="" for arg in $args; do @@ -490,7 +505,7 @@ EOF done < function.${lang} done else - sed "s/____call_list____/${CALL_LIST}/g;s/____piped_call_list____/${PIPED_CALL_LIST}/;s/____api_version____/${API_VERSION}/g;s/____sdk_version____/${SDK_VERSION}/g;s/____cli_version____/$(cat cli-version)/g;s/____cli_name____/${CLI_NAME}/" <<< "$line"; + sed "s/____call_list____/${CALL_LIST}/g;s/____piped_call_list____/${PIPED_CALL_LIST}/;s/____api_version____/${API_VERSION}/g;s/____sdk_version____/${SDK_VERSION}/g;s/____cli_version____/$(cat cli-version)/g;s/____cli_name____/${CLI_NAME}/" <<< "$line" fi done < $1 } diff --git a/configure b/configure index 3ebaf27..44cef33 100755 --- a/configure +++ b/configure @@ -16,6 +16,8 @@ LD_JSONCLIB_PATH='' API_VERSION=master SDK_VERSION=0xC061AC CLI_NAME=oapi-cli +FUNCTION_SUFFIX=Request +FUNCLIST_ARG='' API_SCRIPT='curl -s https://raw.githubusercontent.com/outscale/osc-api/$(API_VERSION)/outscale.yaml | yq $(YQ_ARG) > osc-api.json' DEBUG_MODE=0 @@ -30,6 +32,9 @@ for arg in $@; do --gnu-sed-alias use gsed for sed --cli-name=Name set cli name, default: oapi-cli --debug-mode enable debug in cognac (output on stderr) + --function-suffix=suffix suffix use to find function in componants, default: Request + --funclist-arg=arg arguments to pass to funclist + --cli-name=Name set cli name, default: oapi-cli --api-script=script how to generate osc-api.json --yq-go use go version of yq, that use diferents arguments --curl-path=PATH path to curl source root @@ -68,6 +73,10 @@ EOF HAVE_RECOMPILE_JSON_C=1 elif [ "--yq-go" = $arg ]; then YQ_ARG="-o json" + elif [ "--function-suffix" = $( echo "$arg" | cut -d '=' -f 1) ]; then + FUNCTION_SUFFIX=$( echo $arg | cut -f 2 -d '=' ) + elif [ "--funclist-arg" = $( echo "$arg" | cut -d '=' -f 1) ]; then + FUNCLIST_ARG=$( echo $arg | cut -f 2 -d '=' ) elif [ "--gnu-sed-alias" = $arg ]; then SED_ALIAS="'alias sed=gsed'" elif [ "--debug-mode" = $arg ]; then @@ -118,6 +127,8 @@ echo JSON_C_CFLAGS=$JSON_C_CFLAGS >> config.mk echo JSON_C_RULE=$JSON_C_RULE >> config.mk echo API_VERSION=$API_VERSION >> config.mk echo CLI_NAME=$CLI_NAME >> config.mk +echo FUNCTION_SUFFIX=$FUNCTION_SUFFIX >> config.mk +echo FUNCLIST_ARG=$FUNCLIST_ARG >> config.mk echo DEBUG_MODE=$DEBUG_MODE >> config.mk echo -e "osc-api.json:: config.sh\n\t$API_SCRIPT" >> config.mk echo $SDK_VERSION > sdk-version diff --git a/construct_data.c.sh b/construct_data.c.sh index b4cd58e..d0a8ff6 100755 --- a/construct_data.c.sh +++ b/construct_data.c.sh @@ -11,7 +11,7 @@ if [[ "complex_struct" == "$2" ]]; then args=$(json-search -Kn properties <<< $base | tr -d '",[]') alias get_type=get_type2 else - base=$(json-search ${func}Request < osc-api.json) + base=$(json-search ${func}${FUNCTION_SUFFIX} < osc-api.json) args=$(json-search -Kn properties <<< $base | tr -d "\n[],\"" | sed 's/ / /g') fi diff --git a/helper.sh b/helper.sh index c721d4d..030bdcd 100644 --- a/helper.sh +++ b/helper.sh @@ -168,7 +168,7 @@ get_type_description() { get_type() { x=$2 func=$1 - local arg_info=$(json-search ${func}Request < osc-api.json | json-search $x) + local arg_info=$(json-search ${func}${FUNCTION_SUFFIX} < osc-api.json | json-search $x) local direct_ref=$(jq -r '.["$ref"]' 2> /dev/null <<< $arg_info) local have_direct_ref=$? if [[ $have_direct_ref == 0 && "$direct_ref" != 'null' ]]; then diff --git a/mk_args.c.sh b/mk_args.c.sh index 77c906f..fe13333 100755 --- a/mk_args.c.sh +++ b/mk_args.c.sh @@ -7,7 +7,7 @@ source "./helper.sh" CALL_LIST_FILE=./call_list CALL_LIST=$(cat $CALL_LIST_FILE) -COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v Request) +COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v ${FUNCTION_SUFFIX}) type_to_ctype() { local t="$1" @@ -124,7 +124,7 @@ done for l in $CALL_LIST ;do snake_l=$(to_snakecase <<< $l) - request=$(json-search -s ${l}Request < osc-api.json) + request=$(json-search -s ${l}${FUNCTION_SUFFIX} < osc-api.json) ARGS_LIST=$(json-search -KR "properties" <<< $request | tr -d '"' | sed 's/,/\n/g') echo "struct osc_${snake_l}_arg {" From 8bcaa35376b522b869e8192299b53d9ac9186486 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 15 Nov 2024 15:15:38 +0100 Subject: [PATCH 06/29] funclist now support costum function suffix Signed-off-by: Matthias Gatto --- bin/funclist.c | 23 ++++++++++++++++++++--- configure | 3 +++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bin/funclist.c b/bin/funclist.c index 18ae72a..830b8f3 100644 --- a/bin/funclist.c +++ b/bin/funclist.c @@ -5,12 +5,29 @@ int main(int ac, char **av) { - bool funclist_from_path = ac > 2; - struct json_object *j_file = json_object_from_file(av[1]); + bool funclist_from_path = 0; + struct json_object *j_file = NULL; + char *suffix = "Request"; int ret = 0; int func_ret = 1; int first = 1; + int i; + + for (i = 1; i < ac; ++i) { + if (!strcmp(av[i], "--func-suffix")) { + ++i; + if (i == ac) { + printf("usage: %s --func-suffix SUFFIX", av[0]); + return 1; + } + suffix = av[i]; + } else if (!j_file) { + j_file = json_object_from_file(av[1]); + } else { + funclist_from_path = 1; + } + } if (!funclist_from_path) { struct json_object *compo; @@ -25,7 +42,7 @@ int main(int ac, char **av) json_object_object_foreach(schema, k, v_) { char *new_k = strdup(k); - char *resquest = strstr(new_k, "Request"); + char *resquest = strstr(new_k, suffix); if (resquest) { if (!first) diff --git a/configure b/configure index 44cef33..575b7d8 100755 --- a/configure +++ b/configure @@ -128,6 +128,9 @@ echo JSON_C_RULE=$JSON_C_RULE >> config.mk echo API_VERSION=$API_VERSION >> config.mk echo CLI_NAME=$CLI_NAME >> config.mk echo FUNCTION_SUFFIX=$FUNCTION_SUFFIX >> config.mk +if [[ "$FUNCTION_SUFFIX" != "Request" && "$FUNCTION_SUFFIX" != "" ]]; then + FUNCLIST_ARG="--func-suffix $FUNCTION_SUFFIX $FUNCLIST_ARG" +fi echo FUNCLIST_ARG=$FUNCLIST_ARG >> config.mk echo DEBUG_MODE=$DEBUG_MODE >> config.mk echo -e "osc-api.json:: config.sh\n\t$API_SCRIPT" >> config.mk From 92945b26b9c8dd0853bfe668fcfaec9a0657f499 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 15 Nov 2024 15:34:46 +0100 Subject: [PATCH 07/29] Document --function-suffix=suffix Signed-off-by: Matthias Gatto --- README.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0f19cb9..ed4da3a 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,11 @@ The language argument is crucial as certain keywords might be interpreted differ The script uses a file called osc-api.json, which represents the OpenAPI specification in JSON format. For the Outscale API, the YAML source is converted to JSON using yq. -When generating API calls, COGNAC assumes that the OpenAPI file contains components named CallRequest. +When generating API calls, COGNAC by default assumes that the OpenAPI file contains components named CallRequest. For example, if the API has a call named `CreatePony`, the corresponding component should be located at `#/components/schemas/CreatePonyRequest`. +You can change the suffix of functions using `./configure --function-suffix FUNCTION_SUFFIX` + *Note: There are two versions of yq: one written in Python and one in Go. The default version depends on your distribution. On Arch-based distributions, the Python version is typically the default, whereas on Debian-based distributions, the Go version is default. COGNAC supports both, but to use the Go version, you need to pass `--yq-go` to `./configure`.* ### Example: Generating a CLI for a New API @@ -44,14 +46,21 @@ To configure the Makefile to generate the CLI with the name `pony-cli`, and adju Run the following command: ```bash -./configure --cli-name=pony-cli --api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG) | sed "s/Input/Request/" > osc-api.json' +./configure --cli-name=pony-cli --function-suffix Input --api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG)" > osc-api.json' ``` `-cli-name=pony-cli` set the generated binary name to `pony-cli` ```bash ---api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG) | sed "s/Input/Request/" > osc-api.json' +--function-suffix Input +``` +Look for Function named XInput instead of XRequest. + +```bash +--api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG) > osc-api.json' ``` + + This script is used to fetch the API file. @@ -59,7 +68,6 @@ Here’s what the script does: 1. Retrieves the API in YAML format using curl -s `https://ponyapi.yolo/.` 2. Converts the YAML to JSON using yq `$(YQ_ARG)`. *Note the usage of `$(YQ_ARG)`, so ./configure can handle go version of yq* -3. Renames all components named `XInput` to `XRequest`. Once this setup is complete, you can now use the Makefile. It's also a good idea to run ./configure --help, as it contains several useful options. - `--wget-json-search`: Helps with downloading `json-search`, which can be tricky to install, **If unsure, we recommend using this by default** From 9e45172090c53bcbaa19bef6f412492ec9ab550d Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 19 Nov 2024 15:28:00 +0100 Subject: [PATCH 08/29] path is on thw way Signed-off-by: Matthias Gatto --- Makefile | 17 ++++++++++------- bin/path_to_snakecase.c | 40 ++++++++++++++++++++++++++++++++++++++++ cognac_gen.sh | 11 +++++------ configure | 12 ++++++++++++ 4 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 bin/path_to_snakecase.c diff --git a/Makefile b/Makefile index d2133db..a022ccb 100644 --- a/Makefile +++ b/Makefile @@ -33,26 +33,30 @@ make_cli: $(CLI_NAME) $(CLI_NAME)-completion.bash bin/funclist: bin/funclist.c $(CC) -O3 bin/funclist.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/funclist +bin/path_to_snakecase: bin/path_to_snakecase.c + $(CC) -O3 bin/path_to_snakecase.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/path_to_snakecase + bin/line_check: bin/line_check.c $(CC) -O3 bin/line_check.c -o bin/line_check -main.c: bin/line_check osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh +main.c: bin/line_check osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh bin/path_to_snakecase ./cognac_gen.sh main_tpl.c main.c c -osc_sdk.c: bin/line_check osc-api.json call_list config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh +osc_sdk.c: bin/line_check osc-api.json call_list config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh bin/path_to_snakecase ./cognac_gen.sh lib.c osc_sdk.c c -osc_sdk.h: bin/line_check osc-api.json call_list config.sh lib.h cognac_gen.sh mk_args.c.sh +osc_sdk.h: bin/line_check osc-api.json call_list config.sh lib.h cognac_gen.sh mk_args.c.sh bin/path_to_snakecase ./cognac_gen.sh lib.h osc_sdk.h c -$(CLI_NAME)-completion.bash: bin/line_check osc-api.json call_list config.sh oapi-cli-completion-tpl.bash cognac_gen.sh +$(CLI_NAME)-completion.bash: bin/line_check osc-api.json call_list config.sh oapi-cli-completion-tpl.bash cognac_gen.sh bin/path_to_snakecase ./cognac_gen.sh oapi-cli-completion-tpl.bash $(CLI_NAME)-completion.bash bash -config.sh: +config.sh: configure config.mk echo "alias json-search=$(JSON_SEARCH)" > config.sh echo $(SED_ALIAS) >> config.sh echo FUNCTION_SUFFIX=$(FUNCTION_SUFFIX) >> config.sh echo "export CLI_NAME=$(CLI_NAME)" >> config.sh + echo "export FROM_PATH=$(FROM_PATH)" >> config.sh echo -e "debug()\n{" >> config.sh echo -e '\tif [[ "$$DEBUG_MODE" == "1" ]] ; then echo "$$@" >&2 ; fi\n}' >> config.sh echo export DEBUG_MODE=$(DEBUG_MODE) >> config.sh @@ -60,9 +64,8 @@ config.sh: call_list: osc-api.json bin/funclist bin/funclist osc-api.json $(FUNCLIST_ARG) > call_list - clean: - rm -vf osc-api.json call_list osc_sdk.c osc_sdk.h main.c $(CLI_NAME) config.sh $(CLI_NAME)-completion.bash bin/line_check bin/funclist + rm -vf osc-api.json call_list osc_sdk.c osc_sdk.h main.c $(CLI_NAME) config.sh $(CLI_NAME)-completion.bash bin/line_check bin/funclist bin/path_to_snakecase .PHONY: clean list_api_version help make_cli diff --git a/bin/path_to_snakecase.c b/bin/path_to_snakecase.c new file mode 100644 index 0000000..ec57048 --- /dev/null +++ b/bin/path_to_snakecase.c @@ -0,0 +1,40 @@ +#include +#include + +/** + * reset first to 1, so 'my/Func' give my_func, and not + * my__func + */ +#define PUT_UNDERSCORE() do { \ + putchar('_'); \ + first = 1; \ + } while (0) + +int main(int ac, char **av) +{ + const char *str = av[1]; + int first = 1; + + for (const char *c_ptr = str; *c_ptr; ++c_ptr) { + char c = *c_ptr; + + if (c_ptr == str && c == '/') { + continue; + } + if (c == '{' || c == '}') { + continue; + } + if (c == '/') { + PUT_UNDERSCORE(); + } else if (isupper(c)) { + if (!first) { + PUT_UNDERSCORE(); + } + putchar(tolower(c)); + } else { + putchar(c); + first = 0; + } + } + putchar('\n'); +} diff --git a/cognac_gen.sh b/cognac_gen.sh index 615a76a..c8293d2 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -12,6 +12,7 @@ shopt -s expand_aliases source ./helper.sh debug "debug mode is on" +debug "Get functions call from osc-api.json path: $FROM_PATH" dash_this_arg() { @@ -269,9 +270,6 @@ replace_args() COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response) local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) - debug "CALL_LIST: $CALLS" - debug "COMPLEX_STRUCT: $COMPLEX_STRUCT" - debug "diff:" "$DIFF" COMPLEX_STRUCT="$DIFF" debug "COMPLEX_STRUCT NOW:" "$COMPLEX_STRUCT" @@ -312,8 +310,9 @@ EOF # prototypes for s in $COMPLEX_STRUCT; do - struct_name=$(to_snakecase <<< $s) - A_LST=$(jq .components.schemas.$s < osc-api.json | json-search -Kn properties | tr -d '",[]') + struct_name=$(bin/path_to_snakecase "$s") + debug "structs name: $s $struct_name" + A_LST=$(jq .components.schemas[\"$s\"] < osc-api.json | json-search -Kn properties | tr -d '",[]') if [ "$A_LST" != "null" ]; then echo "int ${struct_name}_parser(void *s, char *str, char *aa, struct ptr_array *pa);" fi @@ -505,7 +504,7 @@ EOF done < function.${lang} done else - sed "s/____call_list____/${CALL_LIST}/g;s/____piped_call_list____/${PIPED_CALL_LIST}/;s/____api_version____/${API_VERSION}/g;s/____sdk_version____/${SDK_VERSION}/g;s/____cli_version____/$(cat cli-version)/g;s/____cli_name____/${CLI_NAME}/" <<< "$line" + sed "s|____call_list____|${CALL_LIST}|g;s+____piped_call_list____+${PIPED_CALL_LIST}+;s/____api_version____/${API_VERSION}/g;s/____sdk_version____/${SDK_VERSION}/g;s/____cli_version____/$(cat cli-version)/g;s/____cli_name____/${CLI_NAME}/" <<< "$line" fi done < $1 } diff --git a/configure b/configure index 575b7d8..c1595a3 100755 --- a/configure +++ b/configure @@ -17,6 +17,7 @@ API_VERSION=master SDK_VERSION=0xC061AC CLI_NAME=oapi-cli FUNCTION_SUFFIX=Request +FROM_PATH=0 FUNCLIST_ARG='' API_SCRIPT='curl -s https://raw.githubusercontent.com/outscale/osc-api/$(API_VERSION)/outscale.yaml | yq $(YQ_ARG) > osc-api.json' DEBUG_MODE=0 @@ -34,6 +35,7 @@ for arg in $@; do --debug-mode enable debug in cognac (output on stderr) --function-suffix=suffix suffix use to find function in componants, default: Request --funclist-arg=arg arguments to pass to funclist + --from-path use paths instead of componant suffix for functions generations --cli-name=Name set cli name, default: oapi-cli --api-script=script how to generate osc-api.json --yq-go use go version of yq, that use diferents arguments @@ -81,6 +83,8 @@ EOF SED_ALIAS="'alias sed=gsed'" elif [ "--debug-mode" = $arg ]; then DEBUG_MODE=1 + elif [ "--from-path" = $arg ]; then + FROM_PATH=1 elif [ "--target-api" = $( echo "$arg" | cut -d '=' -f 1) ]; then API_VERSION=$( echo $arg | cut -f 2 -d '=' ) elif [ "--cli-name" = $( echo "$arg" | cut -d '=' -f 1) ]; then @@ -127,12 +131,20 @@ echo JSON_C_CFLAGS=$JSON_C_CFLAGS >> config.mk echo JSON_C_RULE=$JSON_C_RULE >> config.mk echo API_VERSION=$API_VERSION >> config.mk echo CLI_NAME=$CLI_NAME >> config.mk +if [[ "$FROM_PATH" == "1" ]]; then + FUNCLIST_ARG="--from-path $FUNCLIST_ARG" + FUNCTION_SUFFIX="" +fi + echo FUNCTION_SUFFIX=$FUNCTION_SUFFIX >> config.mk + if [[ "$FUNCTION_SUFFIX" != "Request" && "$FUNCTION_SUFFIX" != "" ]]; then FUNCLIST_ARG="--func-suffix $FUNCTION_SUFFIX $FUNCLIST_ARG" fi + echo FUNCLIST_ARG=$FUNCLIST_ARG >> config.mk echo DEBUG_MODE=$DEBUG_MODE >> config.mk +echo FROM_PATH=$FROM_PATH >> config.mk echo -e "osc-api.json:: config.sh\n\t$API_SCRIPT" >> config.mk echo $SDK_VERSION > sdk-version echo "config done:" From 0927c7ad908cdc8b7bf27259c99ce41dd4c427ab Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Thu, 21 Nov 2024 11:56:13 +0100 Subject: [PATCH 09/29] add more helper functions Signed-off-by: Matthias Gatto --- Makefile | 13 +++++--- bin/README.md | 19 ++++++++++++ bin/funclist.c | 2 +- bin/get_argument_list.c | 66 +++++++++++++++++++++++++++++++++++++++++ cognac_gen.sh | 22 ++++++++------ 5 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 bin/get_argument_list.c diff --git a/Makefile b/Makefile index a022ccb..0678bf3 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,8 @@ help: include oapi-cli.mk +BIN_DEPENDANCIES=bin/path_to_snakecase bin/line_check bin/get_argument_list bin/funclist + osc-api.json:: ./bin/osc-api-seems-valid.sh osc-api.json "need_remove" @@ -39,16 +41,19 @@ bin/path_to_snakecase: bin/path_to_snakecase.c bin/line_check: bin/line_check.c $(CC) -O3 bin/line_check.c -o bin/line_check -main.c: bin/line_check osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh bin/path_to_snakecase +bin/get_argument_list: bin/get_argument_list.c + $(CC) -O3 -g bin/get_argument_list.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/get_argument_list + +main.c: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh ./cognac_gen.sh main_tpl.c main.c c -osc_sdk.c: bin/line_check osc-api.json call_list config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh bin/path_to_snakecase +osc_sdk.c: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh lib.c cognac_gen.sh construct_data.c.sh mk_args.c.sh ./cognac_gen.sh lib.c osc_sdk.c c -osc_sdk.h: bin/line_check osc-api.json call_list config.sh lib.h cognac_gen.sh mk_args.c.sh bin/path_to_snakecase +osc_sdk.h: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh lib.h cognac_gen.sh mk_args.c.sh ./cognac_gen.sh lib.h osc_sdk.h c -$(CLI_NAME)-completion.bash: bin/line_check osc-api.json call_list config.sh oapi-cli-completion-tpl.bash cognac_gen.sh bin/path_to_snakecase +$(CLI_NAME)-completion.bash: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh oapi-cli-completion-tpl.bash cognac_gen.sh ./cognac_gen.sh oapi-cli-completion-tpl.bash $(CLI_NAME)-completion.bash bash config.sh: configure config.mk diff --git a/bin/README.md b/bin/README.md index bc5a9a2..49acdd4 100644 --- a/bin/README.md +++ b/bin/README.md @@ -23,3 +23,22 @@ This is also much faster than multiple calls to grep. # osc-api-seems-valid.sh This utility is used to check if osc-api.json has been generated correctly. It verifies that the file is a valid JSON and contains at least one API function. + +# path_to_snakecase +snakecasise an object name. +example `ReadVms` become `read_vms` +`/my/{NAME}/getId` become `my_name_get_id` + +usage: +``` +./path_to_snakecase STRING +``` + +# get_argument_list + +Take an object componant in `paths` or in `components.schema`, and give a list of all it arguments. + +usage: +``` +./get_argument_list file.json componant_name +``` diff --git a/bin/funclist.c b/bin/funclist.c index 830b8f3..7c52c7a 100644 --- a/bin/funclist.c +++ b/bin/funclist.c @@ -64,7 +64,7 @@ int main(int ac, char **av) if (!first) putchar(' '); first = 0; - printf("%s", k+1); + printf("%s", k); } } diff --git a/bin/get_argument_list.c b/bin/get_argument_list.c new file mode 100644 index 0000000..34da87b --- /dev/null +++ b/bin/get_argument_list.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include "json.h" + +#define OBJ_GET(src, name, dst) do { \ + ret = json_object_object_get_ex(src, name, dst); \ + if (!ret) { \ + puts("null"); \ + goto err; \ + } \ + } while (0) \ + +int main(int ac, char **av) +{ + struct json_object *j_file = json_object_from_file(av[1]); + char *componant_name = av[2]; + int func_ret = 1; + int ret; + + /* if / is found, assume in path */ + if (strchr(componant_name, '/')) { + int ret = 0; + struct json_object *paths; + struct json_object *func; + struct json_object *parameters; + struct json_object *post_or_get; + int len; + + OBJ_GET(j_file, "paths", &paths); + OBJ_GET(paths, componant_name, &func); + ret = json_object_object_get_ex(func, "post", &post_or_get); + if (!ret) { + OBJ_GET(func, "get", &post_or_get); + } + OBJ_GET(post_or_get, "parameters", ¶meters); + len = json_object_array_length(parameters); + for (int i = 0; i < len; ++i) { + struct json_object *name; + + OBJ_GET(json_object_array_get_idx(parameters, i), + "name", &name); + printf(" %s" + !i, json_object_get_string(name)); + } + } else { + struct json_object *compo; + struct json_object *schema; + struct json_object *func; + struct json_object *properties; + int i = 0; + + OBJ_GET(j_file, "components", &compo); + OBJ_GET(compo, "schemas", &schema); + OBJ_GET(schema, componant_name, &func); + OBJ_GET(func, "properties", &properties); + json_object_object_foreach(properties, k, v_) { + printf(" %s" + !i++, k); + } + } + putchar('\n'); + func_ret = 0; +err: + json_object_put(j_file); + return func_ret; + +} diff --git a/cognac_gen.sh b/cognac_gen.sh index c8293d2..c8c6ea8 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -302,17 +302,20 @@ EOF done elif [ "$arg_check" == "____complex_struct_func_parser____" ]; then debug "____complex_struct_func_parser____" - local CALLS=$(for c in $(cat call_list) ; do echo ${c}${FUNCTION_SUFFIX} ; done) COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response) - local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) - COMPLEX_STRUCT="$DIFF" + if [[ "$FROM_PATH" != "1" ]]; then + local CALLS=$(for c in $(cat call_list) ; do echo ${c}${FUNCTION_SUFFIX} ; done) + local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) + + COMPLEX_STRUCT="$DIFF" + fi # prototypes for s in $COMPLEX_STRUCT; do struct_name=$(bin/path_to_snakecase "$s") - debug "structs name: $s $struct_name" - A_LST=$(jq .components.schemas[\"$s\"] < osc-api.json | json-search -Kn properties | tr -d '",[]') + A_LST=$(bin/get_argument_list osc-api.json "$s") + if [ "$A_LST" != "null" ]; then echo "int ${struct_name}_parser(void *s, char *str, char *aa, struct ptr_array *pa);" fi @@ -325,7 +328,7 @@ EOF struct_name=$(to_snakecase <<< $s) local componant=$(jq .components.schemas.$s < osc-api.json) - A_LST=$(json-search -Kn properties <<< $componant | tr -d '",[]') + A_LST=$(bin/get_argument_list osc-api.json "$s") if [ "$A_LST" != "null" ]; then echo "int ${struct_name}_parser(void *v_s, char *str, char *aa, struct ptr_array *pa) {" @@ -372,9 +375,10 @@ EOF debug "____cli_parser____" for l in $CALL_LIST; do snake_l=$(to_snakecase <<< $l) - arg_list=$(json-search ${l}${FUNCTION_SUFFIX} < osc-api.json \ - | json-search -K properties \ - | tr -d "[]\"," | sed '/^$/d') + arg_list=$(bin/get_argument_list osc-api.json ${l}${FUNCTION_SUFFIX}) + + debug "arg list of " ${l}${FUNCTION_SUFFIX} ":" + debug $arg_list cat < Date: Mon, 25 Nov 2024 13:03:07 +0100 Subject: [PATCH 10/29] add more helper to handle path type Signed-off-by: Matthias Gatto --- Makefile | 8 ++++++- bin/README.md | 23 ++++++++++++++++++++ bin/get_argument_list.c | 48 +++++++++++++++++++++++++++-------------- bin/path_to_camelcase.c | 26 ++++++++++++++++++++++ cognac_gen.sh | 29 ++++++++++++------------- construct_data.c.sh | 2 +- helper.sh | 17 +++++++++++++++ mk_args.c.sh | 24 +++++++++++++++------ 8 files changed, 138 insertions(+), 39 deletions(-) create mode 100644 bin/path_to_camelcase.c diff --git a/Makefile b/Makefile index 0678bf3..79825f2 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ help: include oapi-cli.mk -BIN_DEPENDANCIES=bin/path_to_snakecase bin/line_check bin/get_argument_list bin/funclist +BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type osc-api.json:: ./bin/osc-api-seems-valid.sh osc-api.json "need_remove" @@ -38,12 +38,18 @@ bin/funclist: bin/funclist.c bin/path_to_snakecase: bin/path_to_snakecase.c $(CC) -O3 bin/path_to_snakecase.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/path_to_snakecase +bin/path_to_camelcase: bin/path_to_camelcase.c + $(CC) -O3 bin/path_to_camelcase.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/path_to_camelcase + bin/line_check: bin/line_check.c $(CC) -O3 bin/line_check.c -o bin/line_check bin/get_argument_list: bin/get_argument_list.c $(CC) -O3 -g bin/get_argument_list.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/get_argument_list +bin/get_path_type: bin/get_path_type.c + $(CC) -O3 -g bin/get_path_type.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/get_path_type + main.c: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh ./cognac_gen.sh main_tpl.c main.c c diff --git a/bin/README.md b/bin/README.md index 49acdd4..95882e9 100644 --- a/bin/README.md +++ b/bin/README.md @@ -34,6 +34,16 @@ usage: ./path_to_snakecase STRING ``` +# path_to_camelcase +camelcasise an object name. +example `ReadVms` become `read_vms` +`/my/{NAME}/getId` become `my_name_get_id` + +usage: +``` +./path_to_camelcase STRING +``` + # get_argument_list Take an object componant in `paths` or in `components.schema`, and give a list of all it arguments. @@ -42,3 +52,16 @@ usage: ``` ./get_argument_list file.json componant_name ``` + +# get_path_type + +usage: +``` +./bin/get_path_type osc-api.json PATH ARGUMENT_NAME +``` + +example: +``` +./bin/get_path_type osc-api.json /projects id +``` + diff --git a/bin/get_argument_list.c b/bin/get_argument_list.c index 34da87b..ac72eb8 100644 --- a/bin/get_argument_list.c +++ b/bin/get_argument_list.c @@ -1,34 +1,33 @@ #include #include #include +#include "helper.h" #include "json.h" -#define OBJ_GET(src, name, dst) do { \ - ret = json_object_object_get_ex(src, name, dst); \ - if (!ret) { \ - puts("null"); \ - goto err; \ - } \ - } while (0) \ - int main(int ac, char **av) { struct json_object *j_file = json_object_from_file(av[1]); char *componant_name = av[2]; + char *option = ac > 2 ? av[3] : NULL; int func_ret = 1; + int only_require = 0; int ret; + if (option && !strcmp(option, "--require")) + only_require = 1; + /* if / is found, assume in path */ if (strchr(componant_name, '/')) { int ret = 0; - struct json_object *paths; struct json_object *func; struct json_object *parameters; struct json_object *post_or_get; int len; - OBJ_GET(j_file, "paths", &paths); - OBJ_GET(paths, componant_name, &func); + func = get_path_from_file(j_file, componant_name); + post_or_get = get_or_post_from_path(func); + if (!post_or_get) + goto err; ret = json_object_object_get_ex(func, "post", &post_or_get); if (!ret) { OBJ_GET(func, "get", &post_or_get); @@ -37,9 +36,16 @@ int main(int ac, char **av) len = json_object_array_length(parameters); for (int i = 0; i < len; ++i) { struct json_object *name; + struct json_object *param = json_object_array_get_idx(parameters, i); - OBJ_GET(json_object_array_get_idx(parameters, i), - "name", &name); + if (only_require) { + struct json_object *required; + json_object_object_get_ex(param, "required", &required); + if (!required || !json_object_get_boolean(required)) { + continue; + } + } + OBJ_GET(param, "name", &name); printf(" %s" + !i, json_object_get_string(name)); } } else { @@ -52,9 +58,19 @@ int main(int ac, char **av) OBJ_GET(j_file, "components", &compo); OBJ_GET(compo, "schemas", &schema); OBJ_GET(schema, componant_name, &func); - OBJ_GET(func, "properties", &properties); - json_object_object_foreach(properties, k, v_) { - printf(" %s" + !i++, k); + if (only_require) { + OBJ_GET(func, "required", &properties); + + int len = json_object_array_length(properties); + for (int i = 0; i < len; ++i) { + const char *str = json_object_get_string(json_object_array_get_idx(properties, i)); + printf(" %s" + !i, str); + } + } else { + OBJ_GET(func, "properties", &properties); + json_object_object_foreach(properties, k, v_) { + printf(" %s" + !i++, k); + } } } putchar('\n'); diff --git a/bin/path_to_camelcase.c b/bin/path_to_camelcase.c new file mode 100644 index 0000000..f7b3d2a --- /dev/null +++ b/bin/path_to_camelcase.c @@ -0,0 +1,26 @@ +#include +#include + +int main(int ac, char **av) +{ + const char *str = av[1]; + int first = 0; + + for (const char *c_ptr = str; *c_ptr; ++c_ptr) { + char c = *c_ptr; + if (c == '/' || c == '_') { + first = 1; + continue; + } + if (c == '{' || c == '}') { + continue; + } + if (first) { + putchar(toupper(c)); + first = 0; + } else { + putchar(c); + } + } + putchar('\n'); +} diff --git a/cognac_gen.sh b/cognac_gen.sh index c8c6ea8..56dd252 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -240,7 +240,7 @@ replace_args() D3=$(cut -d ';' -f 3 <<< $DELIMES | tr -d "'") for x in $CALL_LIST ; do st_info=$(json-search -s ${x}${FUNCTION_SUFFIX} < osc-api.json) - A_LST=$(json-search -K properties <<< $st_info | tr -d '",[]') + A_LST=$(bin/get_argument_list osc-api.json ${x}${FUNCTION_SUFFIX}) echo -en $D1 for a in $A_LST; do @@ -266,13 +266,14 @@ replace_args() echo -ne $D3 elif [ "$arg_check" == "____complex_struct_to_string_func____" ]; then debug "____complex_struct_to_string_func____" - local CALLS=$(for c in $(cat call_list) ; do echo ${c}${FUNCTION_SUFFIX} ; done) COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response) - local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) + if [[ "$FROM_PATH" != "1" ]]; then + local CALLS=$(for c in $(cat call_list) ; do echo ${c}${FUNCTION_SUFFIX} ; done) + local DIFF=$(echo ${CALLS[@]} ${COMPLEX_STRUCT[@]} | tr ' ' '\n' | sort | uniq -u) - COMPLEX_STRUCT="$DIFF" - debug "COMPLEX_STRUCT NOW:" "$COMPLEX_STRUCT" + COMPLEX_STRUCT="$DIFF" + fi for s in $COMPLEX_STRUCT; do struct_name=$(to_snakecase <<< $s) @@ -374,14 +375,12 @@ EOF elif [ "$arg_check" == "____cli_parser____" ] ; then debug "____cli_parser____" for l in $CALL_LIST; do - snake_l=$(to_snakecase <<< $l) + snake_l=$(bin/path_to_snakecase "$l") + local caml_l=$(bin/path_to_camelcase "$l") arg_list=$(bin/get_argument_list osc-api.json ${l}${FUNCTION_SUFFIX}) - debug "arg list of " ${l}${FUNCTION_SUFFIX} ":" - debug $arg_list - cat < /dev/null <<< $arg_info) local have_direct_ref=$? diff --git a/mk_args.c.sh b/mk_args.c.sh index fe13333..a5c7476 100755 --- a/mk_args.c.sh +++ b/mk_args.c.sh @@ -7,7 +7,11 @@ source "./helper.sh" CALL_LIST_FILE=./call_list CALL_LIST=$(cat $CALL_LIST_FILE) +if [[ "$FROM_PATH" != "1" ]]; then COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response | grep -v ${FUNCTION_SUFFIX}) +else +COMPLEX_STRUCT=$(jq .components < osc-api.json | json-search -KR schemas | tr -d '"' | sed 's/,/\n/g' | grep -v Response) +fi type_to_ctype() { local t="$1" @@ -118,27 +122,35 @@ create_struct() { declare -A structs +debug "____args____: create_struct loop" for s in $COMPLEX_STRUCT; do create_struct "$s" done +debug "____args____: call list loop" for l in $CALL_LIST ;do - snake_l=$(to_snakecase <<< $l) - request=$(json-search -s ${l}${FUNCTION_SUFFIX} < osc-api.json) - ARGS_LIST=$(json-search -KR "properties" <<< $request | tr -d '"' | sed 's/,/\n/g') + snake_l=$(bin/path_to_snakecase "$l") + required=$(bin/get_argument_list osc-api.json "${l}${FUNCTION_SUFFIX}" --require) + ARGS_LIST=$(bin/get_argument_list osc-api.json "${l}${FUNCTION_SUFFIX}") echo "struct osc_${snake_l}_arg {" - echo -n " /* Required:" - json-search required 2>&1 <<< $request | tr -d "[]\"\n" | tr -s ' ' | sed 's/nothing found/none/g' | to_snakecase + echo " /* Required: $required" echo " */" + debug "descriptions now" for x in $ARGS_LIST ;do snake_name=$(to_snakecase <<< "$x") t=$(get_type "$l" "$x") #echo "get type: $func $x" echo ' /*' - get_type_description "$request" "$x" | tr -d '"' | fold -s -w70 | sed -e 's/^/ * /g' | sed "s/null/See '$snake_name' type documentation/" + if [[ "$FROM_PATH" != "1" ]]; then + request=$(json-search -s ${l}${FUNCTION_SUFFIX} < osc-api.json) + + get_type_description "$request" "$x" | tr -d '"' | fold -s -w70 | sed -e 's/^/ * /g' | sed "s/null/See '$snake_name' type documentation/" + else + echo " * $x" + fi echo ' */' #echo "/* TYPE: $t */" type_to_ctype "$t" "${snake_name}" From e4ddaa534ce122c4130f83d749cea4faf1101800 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 29 Nov 2024 15:57:36 +0100 Subject: [PATCH 11/29] descripttion for path mode Signed-off-by: Matthias Gatto --- Makefile | 5 ++- bin/get_path_description.c | 67 ++++++++++++++++++++++++++++++++++++++ cognac_gen.sh | 14 ++++++-- helper.sh | 9 +++++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 bin/get_path_description.c diff --git a/Makefile b/Makefile index 79825f2..4084557 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ help: include oapi-cli.mk -BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type +BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type bin/get_path_description osc-api.json:: ./bin/osc-api-seems-valid.sh osc-api.json "need_remove" @@ -50,6 +50,9 @@ bin/get_argument_list: bin/get_argument_list.c bin/get_path_type: bin/get_path_type.c $(CC) -O3 -g bin/get_path_type.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/get_path_type +bin/get_path_description: bin/get_path_description.c + $(CC) -O3 -g bin/get_path_description.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/get_path_description + main.c: $(BIN_DEPENDANCIES) osc-api.json call_list config.sh main_tpl.c cognac_gen.sh mk_args.c.sh ./cognac_gen.sh main_tpl.c main.c c diff --git a/bin/get_path_description.c b/bin/get_path_description.c new file mode 100644 index 0000000..8e2eacc --- /dev/null +++ b/bin/get_path_description.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include "json.h" +#include "helper.h" + +/* lot of commented code here, left here if I need better description */ +int main(int ac, char **av) +{ + char *element = av[1]; + char *arg = av[2]; + int ret; + int func_ret = 1; + struct json_object *j_elem = json_tokener_parse(element); + struct json_object *post_or_get; + #if 0 + struct json_object *parameters; + struct json_object *name_obj; + #endif + + //printf("element: %s\n", element); + + post_or_get = get_or_post_from_path(j_elem); + if (!post_or_get) + goto err; + + dprintf(2, "%s\n", element); + printf("\"\"\n"); + func_ret = 0; + goto out; + + #if 0 + OBJ_GET(post_or_get, "parameters", ¶meters); + int len = json_object_array_length(parameters); + for (int i = 0; i < len; ++i) { + struct json_object *param = json_object_array_get_idx(parameters, i); + struct json_object *type; + struct json_object *schema; + const char *name; + + OBJ_GET(param, "name", &name_obj); + name = json_object_get_string(name_obj); + + if (strcmp(name, arg)) + continue; + func_ret = 0; + OBJ_GET(param, "schema", &schema); + ret = json_object_object_get_ex(schema, "type", &type); + if (!ret) { + struct json_object *anyof_or_oneof = oneof_or_anyof(schema); + + if (!anyof_or_oneof) + goto err; + anyof_or_oneof = json_object_array_get_idx(anyof_or_oneof, 0); + OBJ_GET(anyof_or_oneof, "type", &type); + } + putchar('"'); + printf("%s", json_object_get_string(type)); + putchar('"'); + putchar('\n'); + } + #endif +out: +err: + json_object_put(j_elem); + return func_ret; +} diff --git a/cognac_gen.sh b/cognac_gen.sh index 56dd252..5b69e52 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -239,15 +239,23 @@ replace_args() D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'") D3=$(cut -d ';' -f 3 <<< $DELIMES | tr -d "'") for x in $CALL_LIST ; do - st_info=$(json-search -s ${x}${FUNCTION_SUFFIX} < osc-api.json) + st_info=$(json-search "${x}${FUNCTION_SUFFIX}" < osc-api.json) A_LST=$(bin/get_argument_list osc-api.json ${x}${FUNCTION_SUFFIX}) + #debug "st info: $st_info" echo -en $D1 for a in $A_LST; do local t=$(get_type3 "$st_info" "$a") - local snake_n=$(to_snakecase <<< $a) + local snake_n=$(bin/path_to_snakecase $a) echo "\"--$a: $t\\n\"" - get_type_description "$st_info" "$a" | sed 's/
//g;s/\\"/\"/g' | tr -d '"' | fold -s -w92 | sed 's/^/\t" /;s/$/\\n"/;s/\"/\\"/g' + PATH_DESC=$(bin/get_path_description "$st_info" "$a") + local PATH_RET=$? + + if [[ "$PATH_RET" == "0" ]]; then + echo $PATH_DESC + else + get_type_description "$st_info" "$a" | sed 's/
//g;s/\\"/\"/g' | tr -d '"' | fold -s -w92 | sed 's/^/\t" /;s/$/\\n"/;s/\"/\\"/g' + fi done echo -en $D2 done diff --git a/helper.sh b/helper.sh index 1ffa5da..db70025 100644 --- a/helper.sh +++ b/helper.sh @@ -158,6 +158,15 @@ get_sub_type_description() { # usage: get_type_description "{FULL COMPONANT JSON}" "ARGUMENT" # ex: get_type_description "$(jq .components.schemas.ReadVmsRequest < osc-api.json)" "VmId" get_type_description() { + PATH_DESC=$(bin/get_path_description "$1" "$2") + local PATH_RET=$? + + if [[ "$PATH_RET" == "0" ]]; then + echo $PATH_DESC + return + fi + + local properties=$(jq .properties.$2 <<< "$1") local desc=$(jq .description <<< "$properties") local ref=$(json-search '$ref' <<< "$properties" 2>&1 ) From 7377d15f605e11df94e42f872b7bbd2a41151e6f Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Mon, 2 Dec 2024 14:55:48 +0100 Subject: [PATCH 12/29] remove debug Signed-off-by: Matthias Gatto --- bin/get_path_description.c | 1 - cognac_gen.sh | 9 +-------- mk_args.c.sh | 1 - 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/bin/get_path_description.c b/bin/get_path_description.c index 8e2eacc..31138fb 100644 --- a/bin/get_path_description.c +++ b/bin/get_path_description.c @@ -24,7 +24,6 @@ int main(int ac, char **av) if (!post_or_get) goto err; - dprintf(2, "%s\n", element); printf("\"\"\n"); func_ret = 0; goto out; diff --git a/cognac_gen.sh b/cognac_gen.sh index 5b69e52..4aaee54 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -242,20 +242,13 @@ replace_args() st_info=$(json-search "${x}${FUNCTION_SUFFIX}" < osc-api.json) A_LST=$(bin/get_argument_list osc-api.json ${x}${FUNCTION_SUFFIX}) - #debug "st info: $st_info" echo -en $D1 for a in $A_LST; do local t=$(get_type3 "$st_info" "$a") local snake_n=$(bin/path_to_snakecase $a) echo "\"--$a: $t\\n\"" - PATH_DESC=$(bin/get_path_description "$st_info" "$a") - local PATH_RET=$? - if [[ "$PATH_RET" == "0" ]]; then - echo $PATH_DESC - else - get_type_description "$st_info" "$a" | sed 's/
//g;s/\\"/\"/g' | tr -d '"' | fold -s -w92 | sed 's/^/\t" /;s/$/\\n"/;s/\"/\\"/g' - fi + get_type_description "$st_info" "$a" | sed 's/
//g;s/\\"/\"/g' | tr -d '"' | fold -s -w92 | sed 's/^/\t" /;s/$/\\n"/;s/\"/\\"/g' done echo -en $D2 done diff --git a/mk_args.c.sh b/mk_args.c.sh index a5c7476..46f60fe 100755 --- a/mk_args.c.sh +++ b/mk_args.c.sh @@ -137,7 +137,6 @@ for l in $CALL_LIST ;do echo " /* Required: $required" echo " */" - debug "descriptions now" for x in $ARGS_LIST ;do snake_name=$(to_snakecase <<< "$x") From 311f5b43746297eba5ff96b507b1d6765d5b6664 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Mon, 2 Dec 2024 16:27:55 +0100 Subject: [PATCH 13/29] fix name for path calls Signed-off-by: Matthias Gatto --- cognac_gen.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cognac_gen.sh b/cognac_gen.sh index 4aaee54..606bab8 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -223,9 +223,10 @@ replace_args() D3=$(cut -d ';' -f 3 <<< $DELIMES | tr -d "'") for x in $CALL_LIST ; do echo -en $D1 - local required=$(json-search ${x}${FUNCTION_SUFFIX} < osc-api.json | json-search required 2>&1 | tr -d '[]\n"' | tr -s ' ' | sed 's/nothing found//g') + local caml_x=$(bin/path_to_camelcase "$x") + local required=$(bin/get_argument_list osc-api.json "${x}${FUNCTION_SUFFIX}" --require) local usage_required=$( for a in $(echo $required | tr -d ','); do echo -n " --${a}=${a,,}"; done ) - local usage="\"Usage: oapi-cli $x ${usage_required} [OPTIONS]\n\"" + local usage="\"Usage: oapi-cli $caml_x ${usage_required} [OPTIONS]\n\"" local call_desc=$(jq .paths.\""/$x"\".description < osc-api.json | sed 's/
//g' | tr -d '"' | fold -s | sed 's/^/"/;s/$/\\n"/') echo $usage $call_desc \""\nRequired Argument:" $required "\n\"" @@ -260,8 +261,9 @@ replace_args() D2=$(cut -d ';' -f 2 <<< $DELIMES | tr -d "'") D3=$(cut -d ';' -f 3 <<< $DELIMES | tr -d "'") for x in $CALL_LIST ;do + local caml_x=$(bin/path_to_camelcase "$x") echo -en $D1 - echo -n $x + echo -n $caml_x echo -en $D2 done echo -ne $D3 From 21eddf549056e14f60b1ee35adeae6c701c451b4 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 3 Dec 2024 17:27:56 +0100 Subject: [PATCH 14/29] somewhat generate arguments corectly with path Signed-off-by: Matthias Gatto --- Makefile | 8 +++++++- bin/arg_placement.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ bin/construct_path.c | 39 +++++++++++++++++++++++++++++++++++++++ cognac_gen.sh | 13 +++++++++++-- construct_data.c.sh | 19 +++++++++++++++++++ function.c | 19 ++++++++++--------- 6 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 bin/arg_placement.c create mode 100644 bin/construct_path.c diff --git a/Makefile b/Makefile index 4084557..0299ede 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ help: include oapi-cli.mk -BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type bin/get_path_description +BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type bin/get_path_description bin/arg_placement bin/construct_path osc-api.json:: ./bin/osc-api-seems-valid.sh osc-api.json "need_remove" @@ -35,9 +35,15 @@ make_cli: $(CLI_NAME) $(CLI_NAME)-completion.bash bin/funclist: bin/funclist.c $(CC) -O3 bin/funclist.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/funclist +bin/construct_path: bin/construct_path.c + $(CC) -O3 bin/construct_path.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/construct_path + bin/path_to_snakecase: bin/path_to_snakecase.c $(CC) -O3 bin/path_to_snakecase.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/path_to_snakecase +bin/arg_placement: bin/arg_placement.c + $(CC) -O3 bin/arg_placement.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/arg_placement + bin/path_to_camelcase: bin/path_to_camelcase.c $(CC) -O3 bin/path_to_camelcase.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/path_to_camelcase diff --git a/bin/arg_placement.c b/bin/arg_placement.c new file mode 100644 index 0000000..109afc8 --- /dev/null +++ b/bin/arg_placement.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include "json.h" +#define UNFOUND "data" +#include "helper.h" + +int main(int ac, char **av) +{ + struct json_object *j_file = json_object_from_file(av[1]); + char *componant_name = av[2]; + char *arg_name = av[3]; + int ret; + struct json_object *path; + struct json_object *post_or_get; + struct json_object *parameters; + + path = get_path_from_file(j_file, componant_name); + post_or_get = get_or_post_from_path(path); + if (!post_or_get) + goto err; + + OBJ_GET(post_or_get, "parameters", ¶meters); + int len = json_object_array_length(parameters); + for (int i = 0; i < len; ++i) { + struct json_object *param = json_object_array_get_idx(parameters, i); + struct json_object *name_obj; + struct json_object *in; + + OBJ_GET(param, "name", &name_obj); + const char *name = json_object_get_string(name_obj); + struct json_object *type; + struct json_object *schema; + + if (strcmp(name, arg_name)) + continue; + OBJ_GET(param, "in", &in); + puts(json_object_get_string(in)); + goto out; + } +out: +err: + json_object_put(j_file); +} diff --git a/bin/construct_path.c b/bin/construct_path.c new file mode 100644 index 0000000..b3192ac --- /dev/null +++ b/bin/construct_path.c @@ -0,0 +1,39 @@ +#include +#include + +int main(int ac, char **av) +{ + char buf[1024]; + int buf_i; + char *componant_name = av[1]; + + if (*componant_name == '/') + ++componant_name; + char *brk = strchr(componant_name, '{'); + if (!brk) { + printf("\tosc_str_append_string(&end_call, \"/api/v1/%s\");\n", componant_name); + return 0; + } + + *brk = 0; + printf("\tosc_str_append_string(&end_call, \"/api/v1/%s\");\n", componant_name); + +again: + printf("\tosc_str_append_string(&end_call, args->"); + for (++brk; *brk != '}'; ++brk) + putchar(*brk); + puts(");"); + buf_i = 0; + for (++brk; *brk; ++brk) { + if (*brk == '{') { + buf[buf_i] = 0; + printf("\tosc_str_append_string(&end_call, \"%s\");\n", buf); + goto again; + } + buf[buf_i++] = *brk; + } + if (buf_i) { + buf[buf_i] = 0; + printf("\tosc_str_append_string(&end_call, \"%s\");\n", buf); + } +} diff --git a/cognac_gen.sh b/cognac_gen.sh index 606bab8..cb3a3b7 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -493,8 +493,9 @@ EOF debug "____func_code____" for x in $CALL_LIST; do local snake_x=$(bin/path_to_snakecase $x) - local caml_l=$(bin/path_to_camelcase "$x") + #local caml_l=$(bin/path_to_camelcase "$x") local args=$(bin/get_argument_list osc-api.json ${x}${FUNCTION_SUFFIX}) + local have_post=$(cat osc-api.json | json-search ${x}${FUNCTION_SUFFIX} | jq .post) dashed_args="" for arg in $args; do dash_this "$x" "$arg" @@ -504,8 +505,16 @@ EOF do if [[ $( grep -q ____construct_data____ <<< "$fline" )$? == 0 ]]; then ./construct_data.${lang}.sh $x + elif [[ $( grep -q ____construct_path____ <<< "$fline" )$? == 0 ]]; then + bin/construct_path $x + elif [[ $( grep -q ____curl_set_methode____ <<< "$fline" )$? == 0 ]]; then + if [[ "$FROM_PATH" == "1" && "$have_post" == "null" ]]; then + echo ' /* get doesn t need curl_easy_setopt */' + else + echo ' curl_easy_setopt(e->c, CURLOPT_POSTFIELDS, r ? data.buf : "");' + fi else - sed "s/____func____/$caml_l/g; s/____snake_func____/$snake_x/g;s/____dashed_args____/$dashed_args/g" <<< "$fline" + sed "s/____snake_func____/$snake_x/g;s/____dashed_args____/$dashed_args/g" <<< "$fline" fi done < function.${lang} done diff --git a/construct_data.c.sh b/construct_data.c.sh index 296052a..255cada 100755 --- a/construct_data.c.sh +++ b/construct_data.c.sh @@ -40,10 +40,29 @@ if [[ "$args" == "null" ]]; then fi for x in $args ;do + placement=$(bin/arg_placement osc-api.json $func $x) snake_x=$(to_snakecase <<< $x) snake_x=$( if [ "default" == "$snake_x" ] ; then echo default_arg; else echo $snake_x; fi ) t=$(get_type $func $x) + if [[ $placement == "path" ]]; then + debug "skip $x" + continue; + elif [[ $placement == "header" ]]; then + cat <$snake_x) { + struct osc_str hdr; + + osc_init_str(&hdr); + osc_str_append_string(&hdr, "$x: "); + osc_str_append_string(&hdr, args->$snake_x); + curl_easy_setopt(e->c, CURLOPT_HTTPHEADER, hdr.buf); + osc_deinit_str(&hdr); + } +EOF + continue; + fi + if [ "$t" == 'long long int' ]; then t='int' fi diff --git a/function.c b/function.c index 3424361..1bd219b 100644 --- a/function.c +++ b/function.c @@ -1,13 +1,20 @@ -static int ____snake_func_____data(struct osc_____snake_func_____arg *args, struct osc_str *data) +static int ____snake_func_____data(struct osc_env *e, struct osc_____snake_func_____arg *args, struct osc_str *data) { + struct osc_str end_call; int ret = 0; int count_args = 0; if (!args) return 0; + osc_init_str(&end_call); + osc_str_append_string(&end_call, e->endpoint.buf); osc_str_append_string(data, "{"); ____construct_data____ osc_str_append_string(data, "}"); + ____construct_path____ + + curl_easy_setopt(e->c, CURLOPT_URL, end_call.buf); + osc_deinit_str(&end_call); return !!ret; } @@ -15,26 +22,20 @@ int osc_____snake_func____(struct osc_env *e, struct osc_str *out, struct osc___ { CURLcode res = CURLE_OUT_OF_MEMORY; struct osc_str data; - struct osc_str end_call; int r; osc_init_str(&data); - osc_init_str(&end_call); - r = ____snake_func_____data(args, &data); + r = ____snake_func_____data(e, args, &data); if (r < 0) goto out; - osc_str_append_string(&end_call, e->endpoint.buf); - osc_str_append_string(&end_call, "/api/v1/____func____"); - curl_easy_setopt(e->c, CURLOPT_URL, end_call.buf); - curl_easy_setopt(e->c, CURLOPT_POSTFIELDS, r ? data.buf : ""); + ____curl_set_methode____ curl_easy_setopt(e->c, CURLOPT_WRITEDATA, out); if (e->flag & OSC_VERBOSE_MODE) { printf("\n%s\n\n", data.buf); } res = curl_easy_perform(e->c); out: - osc_deinit_str(&end_call); osc_deinit_str(&data); return res; } From 11ecececd86d46068db52fdb1965468d3ff153b2 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 3 Dec 2024 18:47:26 +0100 Subject: [PATCH 15/29] fix header, and handle query maybe Signed-off-by: Matthias Gatto --- cognac_gen.sh | 15 +++++++++++++++ construct_data.c.sh | 14 +++++++++++++- function.c | 4 ++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cognac_gen.sh b/cognac_gen.sh index cb3a3b7..f91b715 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -497,7 +497,12 @@ EOF local args=$(bin/get_argument_list osc-api.json ${x}${FUNCTION_SUFFIX}) local have_post=$(cat osc-api.json | json-search ${x}${FUNCTION_SUFFIX} | jq .post) dashed_args="" + local have_quey=0 for arg in $args; do + placement=$(bin/arg_placement osc-api.json $x $arg) + if [[ $placement == "query" ]]; then + have_quey=1 + fi dash_this "$x" "$arg" done @@ -507,6 +512,16 @@ EOF ./construct_data.${lang}.sh $x elif [[ $( grep -q ____construct_path____ <<< "$fline" )$? == 0 ]]; then bin/construct_path $x + elif [[ $( grep -q ____maybe_query_init____ <<< "$fline" )$? == 0 ]]; then + if [[ "$have_quey" == "1" ]]; then + echo -e "\tstruct osc_str query;" + echo -e "\tosc_init_str(&query);" + fi + elif [[ $( grep -q ____maybe_query_set____ <<< "$fline" )$? == 0 ]]; then + if [[ "$have_quey" == "1" ]]; then + echo -e "\tosc_str_append_string(&end_call, query.buf);" + echo -e "\tosc_deinit_str(&query);" + fi elif [[ $( grep -q ____curl_set_methode____ <<< "$fline" )$? == 0 ]]; then if [[ "$FROM_PATH" == "1" && "$have_post" == "null" ]]; then echo ' /* get doesn t need curl_easy_setopt */' diff --git a/construct_data.c.sh b/construct_data.c.sh index 255cada..b1f8f6e 100755 --- a/construct_data.c.sh +++ b/construct_data.c.sh @@ -56,9 +56,21 @@ for x in $args ;do osc_init_str(&hdr); osc_str_append_string(&hdr, "$x: "); osc_str_append_string(&hdr, args->$snake_x); - curl_easy_setopt(e->c, CURLOPT_HTTPHEADER, hdr.buf); + e->headers = curl_slist_append(e->headers, hdr.buf); + curl_easy_setopt(e->c, CURLOPT_HTTPHEADER, e->headers); osc_deinit_str(&hdr); } +EOF + continue; + elif [[ $placement == "query" ]]; then + cat <$snake_x) { + if (!query.len) + osc_str_append_string(&query, "?$x="); + else + osc_str_append_string(&query, "&$x="); + osc_str_append_string(&query, args->$snake_x); + } EOF continue; fi diff --git a/function.c b/function.c index 1bd219b..0f04ef2 100644 --- a/function.c +++ b/function.c @@ -4,8 +4,11 @@ static int ____snake_func_____data(struct osc_env *e, struct osc_____snake_func int ret = 0; int count_args = 0; + (void)count_args; /* if use only query/header and path, this is unused */ if (!args) return 0; + + ____maybe_query_init____ osc_init_str(&end_call); osc_str_append_string(&end_call, e->endpoint.buf); osc_str_append_string(data, "{"); @@ -13,6 +16,7 @@ static int ____snake_func_____data(struct osc_env *e, struct osc_____snake_func osc_str_append_string(data, "}"); ____construct_path____ + ____maybe_query_set____ curl_easy_setopt(e->c, CURLOPT_URL, end_call.buf); osc_deinit_str(&end_call); return !!ret; From 65b5cd37a191b9df2e5bdc976fad9ba7cc0a5aca Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Mon, 9 Dec 2024 20:31:45 +0100 Subject: [PATCH 16/29] handle path post data mix with other path arguments Signed-off-by: Matthias Gatto --- bin/get_argument_list.c | 8 ++++ bin/helper.h | 98 +++++++++++++++++++++++++++++++++++++++++ cognac_gen.sh | 3 +- construct_data.c.sh | 2 + helper.sh | 36 ++++++++++----- 5 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 bin/helper.h diff --git a/bin/get_argument_list.c b/bin/get_argument_list.c index ac72eb8..0dd3936 100644 --- a/bin/get_argument_list.c +++ b/bin/get_argument_list.c @@ -48,11 +48,18 @@ int main(int ac, char **av) OBJ_GET(param, "name", &name); printf(" %s" + !i, json_object_get_string(name)); } + componant_name = componant_name_from_get_or_post(post_or_get); + if (!componant_name) + goto out; + putchar(' '); + goto in_schema; } else { struct json_object *compo; struct json_object *schema; struct json_object *func; struct json_object *properties; + + in_schema: int i = 0; OBJ_GET(j_file, "components", &compo); @@ -73,6 +80,7 @@ int main(int ac, char **av) } } } +out: putchar('\n'); func_ret = 0; err: diff --git a/bin/helper.h b/bin/helper.h new file mode 100644 index 0000000..4e75740 --- /dev/null +++ b/bin/helper.h @@ -0,0 +1,98 @@ +#ifndef HELPER_H__ +#define HELPER_H__ + +#include "json.h" + +#ifndef UNFOUND +#define UNFOUND "null" +#endif + +#define OBJ_GET(src, name, dst) do { \ + ret = json_object_object_get_ex(src, name, dst); \ + if (!ret) { \ + puts(UNFOUND); \ + goto err; \ + } \ + } while (0) \ + + +char *componant_name_from_get_or_post(struct json_object *post_or_get) +{ + char *componant_name; + struct json_object *request_body; + + int ret = json_object_object_get_ex(post_or_get, "requestBody", &request_body); + if (!ret) + return NULL; + + struct json_object *content; + struct json_object *application_json; + struct json_object *schema; + struct json_object *ref; + + OBJ_GET(request_body, "content", &content); + OBJ_GET(content, "application/json", &application_json); + OBJ_GET(application_json, "schema", &schema); + ret = json_object_object_get_ex(schema, "$ref", &ref); + if (!ref) + return NULL; + componant_name = strrchr(json_object_get_string(ref), '/'); + if (!componant_name) + return NULL;; + ++componant_name; + + return componant_name; +err: + return NULL; +} + +struct json_object *oneof_or_anyof(struct json_object *param) +{ + struct json_object *anyof_or_oneof; + int ret; + + if (!param) + goto err; + ret = json_object_object_get_ex(param, "anyOf", &anyof_or_oneof); + + if (!ret) { + OBJ_GET(param, "oneOf", &anyof_or_oneof); + } + return anyof_or_oneof; +err: + return NULL; +} + + +struct json_object *get_or_post_from_path(struct json_object *path) +{ + struct json_object *post_or_get; + int ret; + + if (!path) + goto err; + ret = json_object_object_get_ex(path, "post", &post_or_get); + + if (!ret) { + OBJ_GET(path, "get", &post_or_get); + } + return post_or_get; +err: + return NULL; +} + +struct json_object *get_path_from_file(struct json_object *j_file, char *path) +{ + struct json_object *paths; + struct json_object *func; + int ret; + + OBJ_GET(j_file, "paths", &paths); + OBJ_GET(paths, path, &func); + + return func; +err: + return NULL; +} + +#endif diff --git a/cognac_gen.sh b/cognac_gen.sh index f91b715..29c0f05 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -55,7 +55,6 @@ dash_this() else dashed_args="$dashed_args --$arg" fi - } cli_c_type_parser() @@ -495,6 +494,7 @@ EOF local snake_x=$(bin/path_to_snakecase $x) #local caml_l=$(bin/path_to_camelcase "$x") local args=$(bin/get_argument_list osc-api.json ${x}${FUNCTION_SUFFIX}) + debug "wil unknow ?" local have_post=$(cat osc-api.json | json-search ${x}${FUNCTION_SUFFIX} | jq .post) dashed_args="" local have_quey=0 @@ -506,6 +506,7 @@ EOF dash_this "$x" "$arg" done + debug "--- here 11 ----" while IFS= read -r fline do if [[ $( grep -q ____construct_data____ <<< "$fline" )$? == 0 ]]; then diff --git a/construct_data.c.sh b/construct_data.c.sh index b1f8f6e..289aeaf 100755 --- a/construct_data.c.sh +++ b/construct_data.c.sh @@ -6,6 +6,8 @@ func=$1 source ./helper.sh +debug "========= $func ========" + if [[ "complex_struct" == "$2" ]]; then base=$(jq .components.schemas.$func < osc-api.json) args=$(json-search -Kn properties <<< $base | tr -d '",[]') diff --git a/helper.sh b/helper.sh index db70025..0942b54 100644 --- a/helper.sh +++ b/helper.sh @@ -91,12 +91,18 @@ get_type3() { arg="$2" local path_type=$(bin/get_path_type "$st_info" $arg) if [[ "$path_type" != "null" ]]; then - debug "info: $path_type" - echo $path_type - return 0 + if [[ "$path_type" != "string" ]]; then + debug "get_type3 of $2: '$path_type'" + st_info=$(jq .components.schemas.$path_type < osc-api.json) + else + debug "info: $path_type" + echo $path_type + return 0 + fi fi - arg_info=$(json-search $arg <<< $st_info) + arg_info=$(jq .properties[\"$arg\"] <<< $st_info) get_type_direct "$arg_info" + debug "type 3 ($arg): " $(get_type_direct "$arg_info") return 0 } @@ -108,8 +114,13 @@ get_type2() { arg="$2" local path_type=$(bin/get_path_type ./osc-api.json $struct $arg) if [[ "$path_type" != "$struct" ]]; then - echo $path_type - return 0 + if [[ "$path_type" != 'string' && 'path_type' != 'null' ]]; then + debug "get_type2: $path_type" + struct=$path_type + else + echo $path_type + return 0 + fi fi st_info=$(jq .components.schemas.$struct < osc-api.json) @@ -188,10 +199,15 @@ get_type_description() { get_type() { x=$2 func=$1 - local path_type=$(bin/get_path_type ./osc-api.json $x $func) - if [[ "$?" == "0" ]]; then - echo $path_type - return 0 + local path_type=$(bin/get_path_type ./osc-api.json $func $x) + if [[ "$path_type" != "$func" ]]; then + if [[ "$path_type" != 'string' && 'path_type' != 'null' ]]; then + debug "get_type: $path_type $func $x" + func=$path_type + else + echo $path_type + return 0 + fi fi local arg_info=$(json-search ${func}${FUNCTION_SUFFIX} < osc-api.json | json-search $x) From a358f07adca59bf54d8d0488d70810c9fd6713b3 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 10 Dec 2024 11:15:19 +0100 Subject: [PATCH 17/29] bin: add missing get_path_type Signed-off-by: Matthias Gatto --- bin/get_path_type.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 bin/get_path_type.c diff --git a/bin/get_path_type.c b/bin/get_path_type.c new file mode 100644 index 0000000..4a68d2d --- /dev/null +++ b/bin/get_path_type.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include "json.h" +#include "helper.h" + +int main(int ac, char **av) +{ + char *componant_name; + struct json_object *j_file = NULL; + char *arg_name; + int func_ret = 1; + int ret; + struct json_object *path; + struct json_object *post_or_get; + struct json_object *parameters; + + if (av[1][0] == '{') { + j_file = json_tokener_parse(av[1]); + path = j_file; + arg_name = av[2]; + } else { + j_file = json_object_from_file(av[1]); + componant_name = av[2]; + arg_name = av[3]; + if (!strchr(componant_name, '/')) { + puts(componant_name); + goto err; + } + path = get_path_from_file(j_file, componant_name); + } + + + post_or_get = get_or_post_from_path(path); + if (!post_or_get) + goto err; + OBJ_GET(post_or_get, "parameters", ¶meters); + int len = json_object_array_length(parameters); + for (int i = 0; i < len; ++i) { + struct json_object *param = json_object_array_get_idx(parameters, i); + struct json_object *name_obj; + + OBJ_GET(param, "name", &name_obj); + const char *name = json_object_get_string(name_obj); + struct json_object *type; + struct json_object *schema; + + if (strcmp(name, arg_name)) + continue; + OBJ_GET(param, "schema", &schema); + ret = json_object_object_get_ex(schema, "type", &type); + if (!ret) { + struct json_object *anyof_or_oneof = oneof_or_anyof(schema); + + if (!anyof_or_oneof) + goto err; + anyof_or_oneof = json_object_array_get_idx(anyof_or_oneof, 0); + OBJ_GET(anyof_or_oneof, "type", &type); + } + puts(json_object_get_string(type)); + func_ret = 0; + goto err; + } + componant_name = componant_name_from_get_or_post(post_or_get); + if (!componant_name) + goto err; + puts(componant_name); + func_ret = 0; + +err: + json_object_put(j_file); + return func_ret; +} From 8a5cb45dfc1d5ac2e5eb808bb00a54ed689083f1 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 10 Dec 2024 12:00:54 +0100 Subject: [PATCH 18/29] add new C helpers documentation Signed-off-by: Matthias Gatto --- bin/README.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/bin/README.md b/bin/README.md index 95882e9..d9719dc 100644 --- a/bin/README.md +++ b/bin/README.md @@ -60,8 +60,75 @@ usage: ./bin/get_path_type osc-api.json PATH ARGUMENT_NAME ``` +example: + +``` +$ ./bin/get_path_type osc-api.json /projects id +int +``` + +assuming id is of type int + +or + +usage: +``` +./bin/get_path_type JSON_ELEM ARGUMENT_NAME +``` + example: ``` -./bin/get_path_type osc-api.json /projects id +$ ./bin/get_path_type '{"post": {"parameters": [ {"name": "a", "type": "string"} ]}}' "a" +string + +``` + +# get_path_description + +Just return "" so far, as the api I test this with, had no description in the path + +usage: +``` +./get_path_description JSON_ELEM ARG +``` + +example: +``` +./get_path_description "{...}" argument_named_titi +``` + +# arg_placement + +return where the argument is used. +`path`, if the argument is use in the path +`header` if it is an http header +`query` if it is in a query string +`data` if it is use in post data + + +usage: +``` +./arg_placement osc-api.json PATH ARGUMENT_NAME +``` + +example: +``` +$ ./arg_placement osc-api.json /projects id +query +``` + +# construct_path + +generate the C code to create a osc_str that the path of the call. + +example: + +``` +$ ./construct_path /project/{id}/get + +osc_str_append_string(&end_call, "/project/"); +osc_str_append_string(&end_call, args->id); +osc_str_append_string(&end_call, "/get"); ``` +The code generated is not exhaustive, but you get the idea. From 855bff12a0d71e6594bf4c82d200315a247e9173 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 10 Dec 2024 13:57:02 +0100 Subject: [PATCH 19/29] fix from thiery feedback Signed-off-by: Matthias Gatto --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed4da3a..929819b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ It takes three arguments: a source file, a destination file, and a language. The language argument is crucial as certain keywords might be interpreted differently depending on the target language. The script uses a file called osc-api.json, which represents the OpenAPI specification in JSON format. -For the Outscale API, the YAML source is converted to JSON using yq. +For the Outscale API, the YAML source is converted to JSON using `yq`: (https://kislyuk.github.io/yq/ or https://github.com/mikefarah/yq) When generating API calls, COGNAC by default assumes that the OpenAPI file contains components named CallRequest. For example, if the API has a call named `CreatePony`, the corresponding component should be located at `#/components/schemas/CreatePonyRequest`. From 45fcbfc06f70bce3aa2a60c029b5a459812a31d2 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 13 Dec 2024 14:26:24 +0100 Subject: [PATCH 20/29] default endpoint is now retrive from osc-api.json Signed-off-by: Matthias Gatto --- Makefile | 5 ++++- bin/construct_endpoint.c | 26 ++++++++++++++++++++++++++ bin/construct_path.c | 29 ++++++++++++----------------- bin/get_path_begin.sh | 6 ++++++ cognac_gen.sh | 30 ++++++++++++++++++++++++++---- helper.sh | 3 --- lib.c | 4 +--- 7 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 bin/construct_endpoint.c create mode 100755 bin/get_path_begin.sh diff --git a/Makefile b/Makefile index 0299ede..e980ead 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ help: include oapi-cli.mk -BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type bin/get_path_description bin/arg_placement bin/construct_path +BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type bin/get_path_description bin/arg_placement bin/construct_path bin/construct_endpoint osc-api.json:: ./bin/osc-api-seems-valid.sh osc-api.json "need_remove" @@ -38,6 +38,9 @@ bin/funclist: bin/funclist.c bin/construct_path: bin/construct_path.c $(CC) -O3 bin/construct_path.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/construct_path +bin/construct_endpoint: bin/construct_endpoint.c + $(CC) -O3 bin/construct_endpoint.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/construct_endpoint + bin/path_to_snakecase: bin/path_to_snakecase.c $(CC) -O3 bin/path_to_snakecase.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/path_to_snakecase diff --git a/bin/construct_endpoint.c b/bin/construct_endpoint.c new file mode 100644 index 0000000..a896b22 --- /dev/null +++ b/bin/construct_endpoint.c @@ -0,0 +1,26 @@ +#include +#include + + +int main(int ac, char **av) +{ + int buf_i; + char *componant_name = av[1]; + +again: + char *brk = strchr(componant_name, '{'); + if (!brk) { + printf("\tosc_str_append_string(&e->endpoint, \"%s\");\n", componant_name); + return 0; + } + + *brk = 0; + printf("\tosc_str_append_string(&e->endpoint, \"%s\");\n", componant_name); + + printf("\tosc_str_append_string(&e->endpoint, e->"); + for (++brk; *brk != '}'; ++brk) + putchar(*brk); + puts(");"); + componant_name = brk + 1; + goto again; +} diff --git a/bin/construct_path.c b/bin/construct_path.c index b3192ac..5068f8c 100644 --- a/bin/construct_path.c +++ b/bin/construct_path.c @@ -3,37 +3,32 @@ int main(int ac, char **av) { - char buf[1024]; - int buf_i; char *componant_name = av[1]; + char *path_begin = ac > 2 ? av[2] : ""; if (*componant_name == '/') ++componant_name; + char *brk = strchr(componant_name, '{'); if (!brk) { - printf("\tosc_str_append_string(&end_call, \"/api/v1/%s\");\n", componant_name); + printf("\tosc_str_append_string(&end_call, \"%s/%s\");\n", path_begin, componant_name); return 0; } *brk = 0; - printf("\tosc_str_append_string(&end_call, \"/api/v1/%s\");\n", componant_name); + printf("\tosc_str_append_string(&end_call, \"%s/%s\");\n", path_begin, componant_name); again: printf("\tosc_str_append_string(&end_call, args->"); for (++brk; *brk != '}'; ++brk) putchar(*brk); puts(");"); - buf_i = 0; - for (++brk; *brk; ++brk) { - if (*brk == '{') { - buf[buf_i] = 0; - printf("\tosc_str_append_string(&end_call, \"%s\");\n", buf); - goto again; - } - buf[buf_i++] = *brk; - } - if (buf_i) { - buf[buf_i] = 0; - printf("\tosc_str_append_string(&end_call, \"%s\");\n", buf); - } + componant_name = brk + 1; + brk = strchr(componant_name, '{'); + if (brk) + *brk = 0; + if (*componant_name) + printf("\tosc_str_append_string(&end_call, \"%s\");\n", componant_name); + if (brk) + goto again; } diff --git a/bin/get_path_begin.sh b/bin/get_path_begin.sh new file mode 100755 index 0000000..6436893 --- /dev/null +++ b/bin/get_path_begin.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +e=$1 +e=${e:8} + +echo $(cut -s -d "/" -f2- <<< "$e") | tr -d '\n' diff --git a/cognac_gen.sh b/cognac_gen.sh index 29c0f05..8668532 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -14,6 +14,26 @@ source ./helper.sh debug "debug mode is on" debug "Get functions call from osc-api.json path: $FROM_PATH" +default_endpoint=$(cat osc-api-api.json | jq -r .servers[0].url) +if [[ "$default_endpoint" == "null" ]]; then + default_endpoint='https://api.{region}.outscale.com/api/v1' +fi +#default_endpoint='https://api.{region}.outscale.com' + +debug $default_endpoint + +path_begin=$(bin/get_path_begin.sh "$default_endpoint") +path_begin_l=${#path_begin} +if [[ $path_begin_l != 0 ]]; then + path_begin="/$path_begin" + path_begin_l=$(($path_begin_l + 1)) + default_endpoint=${default_endpoint:0:-$path_begin_l} +fi + +debug $path_begin +debug $default_endpoint + + dash_this_arg() { local cur="$1" @@ -209,7 +229,7 @@ replace_args() SDK_VERSION=$(cat sdk-version) while IFS= read -r line do - arg_check=$(bin/line_check ____args____ ____func_code____ ____functions_proto____ ____cli_parser____ ____complex_struct_func_parser____ ____complex_struct_to_string_func____ ____call_list_dec____ ____call_list_descriptions____ ____call_list_args_descriptions____ <<< "$line") + arg_check=$(bin/line_check ____args____ ____func_code____ ____functions_proto____ ____cli_parser____ ____complex_struct_func_parser____ ____complex_struct_to_string_func____ ____call_list_dec____ ____call_list_descriptions____ ____call_list_args_descriptions____ ____make_default_endpoint____ <<< "$line") if [ "$arg_check" == "____args____" ]; then debug "____args____" @@ -232,6 +252,10 @@ replace_args() echo -en $D2 done echo -ne $D3 + elif [ "$arg_check" == "____make_default_endpoint____" ]; then + debug "____make_default_endpoint____" + debug "$default_endpoint" + bin/construct_endpoint "$default_endpoint" elif [ "$arg_check" == "____call_list_args_descriptions____" ]; then debug "____call_list_args_descriptions____" DELIMES=$(cut -d '(' -f 2 <<< $line | tr -d ')') @@ -494,7 +518,6 @@ EOF local snake_x=$(bin/path_to_snakecase $x) #local caml_l=$(bin/path_to_camelcase "$x") local args=$(bin/get_argument_list osc-api.json ${x}${FUNCTION_SUFFIX}) - debug "wil unknow ?" local have_post=$(cat osc-api.json | json-search ${x}${FUNCTION_SUFFIX} | jq .post) dashed_args="" local have_quey=0 @@ -506,13 +529,12 @@ EOF dash_this "$x" "$arg" done - debug "--- here 11 ----" while IFS= read -r fline do if [[ $( grep -q ____construct_data____ <<< "$fline" )$? == 0 ]]; then ./construct_data.${lang}.sh $x elif [[ $( grep -q ____construct_path____ <<< "$fline" )$? == 0 ]]; then - bin/construct_path $x + bin/construct_path $x "$path_begin" elif [[ $( grep -q ____maybe_query_init____ <<< "$fline" )$? == 0 ]]; then if [[ "$have_quey" == "1" ]]; then echo -e "\tstruct osc_str query;" diff --git a/helper.sh b/helper.sh index 0942b54..57dd672 100644 --- a/helper.sh +++ b/helper.sh @@ -102,7 +102,6 @@ get_type3() { fi arg_info=$(jq .properties[\"$arg\"] <<< $st_info) get_type_direct "$arg_info" - debug "type 3 ($arg): " $(get_type_direct "$arg_info") return 0 } @@ -115,7 +114,6 @@ get_type2() { local path_type=$(bin/get_path_type ./osc-api.json $struct $arg) if [[ "$path_type" != "$struct" ]]; then if [[ "$path_type" != 'string' && 'path_type' != 'null' ]]; then - debug "get_type2: $path_type" struct=$path_type else echo $path_type @@ -202,7 +200,6 @@ get_type() { local path_type=$(bin/get_path_type ./osc-api.json $func $x) if [[ "$path_type" != "$func" ]]; then if [[ "$path_type" != 'string' && 'path_type' != 'null' ]]; then - debug "get_type: $path_type $func $x" func=$path_type else echo $path_type diff --git a/lib.c b/lib.c index 8809aa9..50d347a 100644 --- a/lib.c +++ b/lib.c @@ -591,9 +591,7 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile, unsigned int flag, e->region = "eu-west-2"; if (!endpoint && !e->endpoint_allocated_) { - osc_str_append_string(&e->endpoint, "https://api."); - osc_str_append_string(&e->endpoint, e->region); - osc_str_append_string(&e->endpoint, ".outscale.com"); +____make_default_endpoint____ } else { if (e->endpoint_allocated_) { osc_str_append_string(&e->endpoint, From 18c75ace7c172a75b9d966c080f972ef02f53d32 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 13 Dec 2024 17:44:50 +0100 Subject: [PATCH 21/29] enable to set user agent Signed-off-by: Matthias Gatto --- Makefile | 1 + cognac_gen.sh | 2 +- configure | 6 +++++- lib.c | 4 ++-- main_tpl.c | 4 ++-- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index e980ead..2676976 100644 --- a/Makefile +++ b/Makefile @@ -79,6 +79,7 @@ config.sh: configure config.mk echo $(SED_ALIAS) >> config.sh echo FUNCTION_SUFFIX=$(FUNCTION_SUFFIX) >> config.sh echo "export CLI_NAME=$(CLI_NAME)" >> config.sh + echo "export SDK_USER_AGENT=$(SDK_USER_AGENT)" >> config.sh echo "export FROM_PATH=$(FROM_PATH)" >> config.sh echo -e "debug()\n{" >> config.sh echo -e '\tif [[ "$$DEBUG_MODE" == "1" ]] ; then echo "$$@" >&2 ; fi\n}' >> config.sh diff --git a/cognac_gen.sh b/cognac_gen.sh index 8668532..68ed082 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -557,7 +557,7 @@ EOF done < function.${lang} done else - sed "s|____call_list____|${CALL_LIST}|g;s+____piped_call_list____+${PIPED_CALL_LIST}+;s/____api_version____/${API_VERSION}/g;s/____sdk_version____/${SDK_VERSION}/g;s/____cli_version____/$(cat cli-version)/g;s/____cli_name____/${CLI_NAME}/" <<< "$line" + sed "s|____call_list____|${CALL_LIST}|g;s+____piped_call_list____+${PIPED_CALL_LIST}+;s/____api_version____/${API_VERSION}/g;s/____sdk_version____/${SDK_VERSION}/g;s/____cli_version____/$(cat cli-version)/g;s/____cli_name____/${CLI_NAME}/;s/____sdk_user_agent____/$SDK_USER_AGENT/g" <<< "$line" fi done < $1 } diff --git a/configure b/configure index c1595a3..8484da3 100755 --- a/configure +++ b/configure @@ -18,6 +18,7 @@ SDK_VERSION=0xC061AC CLI_NAME=oapi-cli FUNCTION_SUFFIX=Request FROM_PATH=0 +SDK_USER_AGENT='osc-sdk' FUNCLIST_ARG='' API_SCRIPT='curl -s https://raw.githubusercontent.com/outscale/osc-api/$(API_VERSION)/outscale.yaml | yq $(YQ_ARG) > osc-api.json' DEBUG_MODE=0 @@ -32,11 +33,11 @@ for arg in $@; do --wget-json-search wget json-search.Appimage --gnu-sed-alias use gsed for sed --cli-name=Name set cli name, default: oapi-cli + --sdk-user-agent=user-agent set sdk UserAgent, default: osc-sdk --debug-mode enable debug in cognac (output on stderr) --function-suffix=suffix suffix use to find function in componants, default: Request --funclist-arg=arg arguments to pass to funclist --from-path use paths instead of componant suffix for functions generations - --cli-name=Name set cli name, default: oapi-cli --api-script=script how to generate osc-api.json --yq-go use go version of yq, that use diferents arguments --curl-path=PATH path to curl source root @@ -91,6 +92,8 @@ EOF CLI_NAME=$( echo $arg | cut -f 2 -d '=' ) elif [ "--api-script" = $( echo "$arg" | cut -d '=' -f 1) ]; then API_SCRIPT="$( echo ${!inc} | cut -f 2 -d '=' )" + elif [ "--sdk-user-agent" = $( echo "$arg" | cut -d '=' -f 1) ]; then + SDK_USER_AGENT="$( echo ${!inc} | cut -f 2 -d '=' )" elif [ "--sdk-version" = $( echo "$arg" | cut -d '=' -f 1) ]; then SDK_VERSION=$( echo $arg | cut -f 2 -d '=' ) elif [ "--curl-path" = $( echo "$arg" | cut -d '=' -f 1) ]; then @@ -142,6 +145,7 @@ if [[ "$FUNCTION_SUFFIX" != "Request" && "$FUNCTION_SUFFIX" != "" ]]; then FUNCLIST_ARG="--func-suffix $FUNCTION_SUFFIX $FUNCLIST_ARG" fi +echo SDK_USER_AGENT=$SDK_USER_AGENT >> config.mk echo FUNCLIST_ARG=$FUNCLIST_ARG >> config.mk echo DEBUG_MODE=$DEBUG_MODE >> config.mk echo FROM_PATH=$FROM_PATH >> config.mk diff --git a/lib.c b/lib.c index 50d347a..d0d23f5 100644 --- a/lib.c +++ b/lib.c @@ -506,14 +506,14 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile, unsigned int flag, *e = (struct osc_env){0}; char *ca = getenv("CURL_CA_BUNDLE"); char *endpoint; - char user_agent[sizeof "osc-sdk-c/" + OSC_SDK_VERSON_L]; + char user_agent[sizeof "____sdk_user_agent____-c/" + OSC_SDK_VERSON_L]; char *cert = getenv("OSC_X509_CLIENT_CERT"); char *sslkey = getenv("OSC_X509_CLIENT_KEY"); char *auth = getenv("OSC_AUTH_METHOD"); char *force_log = cfg_login(cfg); char *force_pass = cfg_pass(cfg); - strcpy(stpcpy(user_agent, "osc-sdk-c/"), osc_sdk_version_str()); + strcpy(stpcpy(user_agent, "____sdk_user_agent____-c/"), osc_sdk_version_str()); e->region = getenv("OSC_REGION"); e->flag = flag; e->auth_method = cfg ? cfg->auth_method : OSC_AKSK_METHOD; diff --git a/main_tpl.c b/main_tpl.c index 097e368..c66bba9 100644 --- a/main_tpl.c +++ b/main_tpl.c @@ -49,7 +49,7 @@ #define OAPI_CLI_VERSION "____cli_version____" -#define OAPI_CLI_UAGENT "____cli_name____/"OAPI_CLI_VERSION"; osc-sdk-c/" +#define OAPI_CLI_UAGENT "____cli_name____/"OAPI_CLI_VERSION"; ____sdk_user_agent____-c/" #define STRY(f, args...) \ do { \ @@ -296,7 +296,7 @@ int main(int ac, char **av) flag |= OSC_INSECURE_MODE; } else if (!strcmp("--version", av[i])) { printf("%s version: %s\n" - "osc-sdk-c version: %s\n" + "____sdk_user_agent____-c version: %s\n" "based on osc-api: %s\n", program_name, OAPI_CLI_VERSION, From 3d9786ec92228cdf9e4e224db4f45a0a80b9098e Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 13 Dec 2024 18:08:07 +0100 Subject: [PATCH 22/29] support ssl_verify in config.json Signed-off-by: Matthias Gatto --- lib.c | 40 ++++++++++++++++++++++++++++++++++++++++ lib.h | 19 ++++++++++--------- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/lib.c b/lib.c index d0d23f5..08c74be 100644 --- a/lib.c +++ b/lib.c @@ -279,6 +279,45 @@ static char *osc_strdup(const char *str) { } while (0) +int osc_set_extra_flag_from_conf(const char *profile, unsigned int *flag) +{ + char buf[1024]; + const char *cfg = cfg_path; + auto_osc_json_c struct json_object *to_free = NULL; + struct json_object *json_tmp, *js = NULL; + + if (!cfg) { + LOAD_CFG_GET_HOME(buf); + cfg = buf; + } + TRY(access(cfg, R_OK), "can't open/read %s\n", cfg); + js = json_object_from_file(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); + + json_tmp = json_object_object_get(js, "ssl_verify"); + if (json_tmp) { + if (!json_object_get_boolean(json_tmp) || + !json_object_get_int(json_tmp)) { + *flag = *flag | OSC_INSECURE_MODE; + } else { + *flag = *flag & (~OSC_INSECURE_MODE); + } + } + json_tmp = json_object_object_get(js, "verbose"); + if (json_tmp) { + if (json_object_get_boolean(json_tmp) || + json_object_get_int(json_tmp)) { + *flag = *flag | OSC_VERBOSE_MODE; + } else { + *flag = *flag & (~OSC_VERBOSE_MODE); + } + } + return 0; +} + int osc_load_ak_sk_from_conf(const char *profile, char **ak, char **sk) { char buf[1024]; @@ -585,6 +624,7 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile, unsigned int flag, if (f < 0) return -1; e->flag |= f; + osc_set_extra_flag_from_conf(profile, &flag); } if (!e->region) diff --git a/lib.h b/lib.h index 2ee5202..6894c27 100644 --- a/lib.h +++ b/lib.h @@ -65,15 +65,15 @@ struct osc_str { char *buf; }; -#define OSC_ENV_FREE_AK 1 << 0 -#define OSC_ENV_FREE_REGION 1 << 1 -#define OSC_VERBOSE_MODE 1 << 2 /* curl verbose mode + print request content */ -#define OSC_INSECURE_MODE 1 << 3 /* see --insecure option of curl */ -#define OSC_ENV_FREE_CERT 1 << 4 -#define OSC_ENV_FREE_SSLKEY 1 << 5 -#define OSC_ENV_FREE_SK 1 << 6 -#define OSC_ENV_FREE_PROXY 1 << 7 -#define OSC_ENV_FREE_ENDPOINT 1 << 8 +#define OSC_ENV_FREE_AK (1 << 0) +#define OSC_ENV_FREE_REGION (1 << 1) +#define OSC_VERBOSE_MODE (1 << 2) /* curl verbose mode + print request content */ +#define OSC_INSECURE_MODE (1 << 3) /* see --insecure option of curl */ +#define OSC_ENV_FREE_CERT (1 << 4) +#define OSC_ENV_FREE_SSLKEY (1 << 5) +#define OSC_ENV_FREE_SK (1 << 6) +#define OSC_ENV_FREE_PROXY (1 << 7) +#define OSC_ENV_FREE_ENDPOINT (1 << 8) #define OSC_ENV_FREE_AK_SK (OSC_ENV_FREE_AK | OSC_ENV_FREE_SK) @@ -134,6 +134,7 @@ struct osc_additional_strings { ____args____ +int osc_set_extra_flag_from_conf(const char *profile, unsigned int *flag); int osc_load_ak_sk_from_conf(const char *profile, char **ak, char **sk); int osc_load_region_from_conf(const char *profile, char **region); int osc_load_loging_password_from_conf(const char *profile, From f7e951c22bad61c0941b6179717d53819a9925a4 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Mon, 16 Dec 2024 10:14:18 +0100 Subject: [PATCH 23/29] rename sdk-user-agent to sdk-name Signed-off-by: Matthias Gatto --- Makefile | 2 +- cognac_gen.sh | 2 +- configure | 10 +++++----- lib.c | 4 ++-- main_tpl.c | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 2676976..f092209 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,7 @@ config.sh: configure config.mk echo $(SED_ALIAS) >> config.sh echo FUNCTION_SUFFIX=$(FUNCTION_SUFFIX) >> config.sh echo "export CLI_NAME=$(CLI_NAME)" >> config.sh - echo "export SDK_USER_AGENT=$(SDK_USER_AGENT)" >> config.sh + echo "export SDK_NAME=$(SDK_NAME)" >> config.sh echo "export FROM_PATH=$(FROM_PATH)" >> config.sh echo -e "debug()\n{" >> config.sh echo -e '\tif [[ "$$DEBUG_MODE" == "1" ]] ; then echo "$$@" >&2 ; fi\n}' >> config.sh diff --git a/cognac_gen.sh b/cognac_gen.sh index 68ed082..07a5e8d 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -557,7 +557,7 @@ EOF done < function.${lang} done else - sed "s|____call_list____|${CALL_LIST}|g;s+____piped_call_list____+${PIPED_CALL_LIST}+;s/____api_version____/${API_VERSION}/g;s/____sdk_version____/${SDK_VERSION}/g;s/____cli_version____/$(cat cli-version)/g;s/____cli_name____/${CLI_NAME}/;s/____sdk_user_agent____/$SDK_USER_AGENT/g" <<< "$line" + sed "s|____call_list____|${CALL_LIST}|g;s+____piped_call_list____+${PIPED_CALL_LIST}+;s/____api_version____/${API_VERSION}/g;s/____sdk_version____/${SDK_VERSION}/g;s/____cli_version____/$(cat cli-version)/g;s/____cli_name____/${CLI_NAME}/;s/____sdk_name____/$SDK_NAME/g" <<< "$line" fi done < $1 } diff --git a/configure b/configure index 8484da3..3a535b1 100755 --- a/configure +++ b/configure @@ -18,7 +18,7 @@ SDK_VERSION=0xC061AC CLI_NAME=oapi-cli FUNCTION_SUFFIX=Request FROM_PATH=0 -SDK_USER_AGENT='osc-sdk' +SDK_NAME='osc-sdk' FUNCLIST_ARG='' API_SCRIPT='curl -s https://raw.githubusercontent.com/outscale/osc-api/$(API_VERSION)/outscale.yaml | yq $(YQ_ARG) > osc-api.json' DEBUG_MODE=0 @@ -33,7 +33,7 @@ for arg in $@; do --wget-json-search wget json-search.Appimage --gnu-sed-alias use gsed for sed --cli-name=Name set cli name, default: oapi-cli - --sdk-user-agent=user-agent set sdk UserAgent, default: osc-sdk + --sdk-name=name set sdk Name, default: osc-sdk --debug-mode enable debug in cognac (output on stderr) --function-suffix=suffix suffix use to find function in componants, default: Request --funclist-arg=arg arguments to pass to funclist @@ -92,8 +92,8 @@ EOF CLI_NAME=$( echo $arg | cut -f 2 -d '=' ) elif [ "--api-script" = $( echo "$arg" | cut -d '=' -f 1) ]; then API_SCRIPT="$( echo ${!inc} | cut -f 2 -d '=' )" - elif [ "--sdk-user-agent" = $( echo "$arg" | cut -d '=' -f 1) ]; then - SDK_USER_AGENT="$( echo ${!inc} | cut -f 2 -d '=' )" + elif [ "--sdk-name" = $( echo "$arg" | cut -d '=' -f 1) ]; then + SDK_NAME="$( echo ${!inc} | cut -f 2 -d '=' )" elif [ "--sdk-version" = $( echo "$arg" | cut -d '=' -f 1) ]; then SDK_VERSION=$( echo $arg | cut -f 2 -d '=' ) elif [ "--curl-path" = $( echo "$arg" | cut -d '=' -f 1) ]; then @@ -145,7 +145,7 @@ if [[ "$FUNCTION_SUFFIX" != "Request" && "$FUNCTION_SUFFIX" != "" ]]; then FUNCLIST_ARG="--func-suffix $FUNCTION_SUFFIX $FUNCLIST_ARG" fi -echo SDK_USER_AGENT=$SDK_USER_AGENT >> config.mk +echo SDK_NAME=$SDK_NAME >> config.mk echo FUNCLIST_ARG=$FUNCLIST_ARG >> config.mk echo DEBUG_MODE=$DEBUG_MODE >> config.mk echo FROM_PATH=$FROM_PATH >> config.mk diff --git a/lib.c b/lib.c index 08c74be..38319d9 100644 --- a/lib.c +++ b/lib.c @@ -545,14 +545,14 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile, unsigned int flag, *e = (struct osc_env){0}; char *ca = getenv("CURL_CA_BUNDLE"); char *endpoint; - char user_agent[sizeof "____sdk_user_agent____-c/" + OSC_SDK_VERSON_L]; + char user_agent[sizeof "____sdk_name____-c/" + OSC_SDK_VERSON_L]; char *cert = getenv("OSC_X509_CLIENT_CERT"); char *sslkey = getenv("OSC_X509_CLIENT_KEY"); char *auth = getenv("OSC_AUTH_METHOD"); char *force_log = cfg_login(cfg); char *force_pass = cfg_pass(cfg); - strcpy(stpcpy(user_agent, "____sdk_user_agent____-c/"), osc_sdk_version_str()); + strcpy(stpcpy(user_agent, "____sdk_name____-c/"), osc_sdk_version_str()); e->region = getenv("OSC_REGION"); e->flag = flag; e->auth_method = cfg ? cfg->auth_method : OSC_AKSK_METHOD; diff --git a/main_tpl.c b/main_tpl.c index c66bba9..9758c35 100644 --- a/main_tpl.c +++ b/main_tpl.c @@ -49,7 +49,7 @@ #define OAPI_CLI_VERSION "____cli_version____" -#define OAPI_CLI_UAGENT "____cli_name____/"OAPI_CLI_VERSION"; ____sdk_user_agent____-c/" +#define OAPI_CLI_UAGENT "____cli_name____/"OAPI_CLI_VERSION"; ____sdk_name____-c/" #define STRY(f, args...) \ do { \ @@ -296,7 +296,7 @@ int main(int ac, char **av) flag |= OSC_INSECURE_MODE; } else if (!strcmp("--version", av[i])) { printf("%s version: %s\n" - "____sdk_user_agent____-c version: %s\n" + "____sdk_name____-c version: %s\n" "based on osc-api: %s\n", program_name, OAPI_CLI_VERSION, From a68744dffa846140596fd315492b54a257d31625 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Mon, 16 Dec 2024 10:55:10 +0100 Subject: [PATCH 24/29] improve doc Signed-off-by: Matthias Gatto --- HACKING.md | 29 +++++++++++++++++++++++++++++ README.md | 2 ++ 2 files changed, 31 insertions(+) diff --git a/HACKING.md b/HACKING.md index b0a36fd..ab4f762 100644 --- a/HACKING.md +++ b/HACKING.md @@ -2,6 +2,13 @@ *Essential Information for Hacking COGNAC* +## Enabling debug + +To enable debug, use `--debug-mode` in `./configure` + +then the `debug` commande in the shell script will print some information. +without this option debug will print nothing + ## Unix Philosophy and COGNAC Paradigms. Most people have heard the phrase "Do One Thing and Do It Well" and associate it with the Unix philosophy. It’s said that this is how Unix operates and how it should be. @@ -35,6 +42,28 @@ A relatively easy way to add a feature in Cognac is to start by modifying the ge For exemple, if you modify a function like `parse_thatarg()` in "osc_sdk.c", once the changes are working, you can then search (using a tool like grep) through the generator code to find where `parse_thatarg` is generated. From there, you can add modifications to the generator to make the change permanent and automated. +## Example to add a new API, that doesn't work at first. + +Let's say you ahve configure cognac like this + +```sh + ./configure --sdk-name=myapi-sdk --cli-name=guru --api-script='cat myapi-api.json > osc-api.json' --debug-mode --from-path +``` + +doing that you will use try to generate the api from path, with debug enable. + +now let's say you have this output while calling `make`: +``` +____complex_struct_func_parser____ +nothing found____cli_parser____ +nothing founderror in jsonnothing founderror in jsonnothing founderror in jsonnothing founderror in jsonnothing founderror in jsonnothing founderror in jsonnothing founderror in json./cognac_gen.sh lib.h osc_sdk.h c +debug mode is on +``` + +This mean that durring `____complex_struct_func_parser____` `____cli_parser____`, the scripts fails to pasre some part of osc-api json. +To debug it, you should look at what happen durring `____complex_struct_func_parser____` and `____cli_parser____` inside `cognac_gen.sh` + + ## helpers.sh Most functions in helper.sh are documented, so read through it if you want to understand some of the most commonly used functions in cognac_gen. diff --git a/README.md b/README.md index 929819b..a55bdb5 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ For example, if the API has a call named `CreatePony`, the corresponding compone You can change the suffix of functions using `./configure --function-suffix FUNCTION_SUFFIX` +Or you can generate functions using what is inside `paths`, using `./configure --from-path` + *Note: There are two versions of yq: one written in Python and one in Go. The default version depends on your distribution. On Arch-based distributions, the Python version is typically the default, whereas on Debian-based distributions, the Go version is default. COGNAC supports both, but to use the Go version, you need to pass `--yq-go` to `./configure`.* ### Example: Generating a CLI for a New API From b6195ee7f1e22683084d0236dda26bdd4799a79d Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 17 Dec 2024 16:06:52 +0100 Subject: [PATCH 25/29] work with guru API Signed-off-by: Matthias Gatto --- bin/get_argument_list.c | 6 +++++ bin/get_path_type.c | 1 + bin/helper.h | 49 +++++++++++++++++++++++++++++++++++++---- bin/path_to_snakecase.c | 2 +- cognac_gen.sh | 37 ++++++++++++++++--------------- construct_data.c.sh | 4 ++-- helper.sh | 13 +++++++++-- mk_args.c.sh | 14 ++++++------ 8 files changed, 92 insertions(+), 34 deletions(-) diff --git a/bin/get_argument_list.c b/bin/get_argument_list.c index 0dd3936..0eab558 100644 --- a/bin/get_argument_list.c +++ b/bin/get_argument_list.c @@ -38,6 +38,12 @@ int main(int ac, char **av) struct json_object *name; struct json_object *param = json_object_array_get_idx(parameters, i); + param = try_get_ref(j_file, param); + if (!param) { + puts(UNFOUND); + goto err; + } + if (only_require) { struct json_object *required; json_object_object_get_ex(param, "required", &required); diff --git a/bin/get_path_type.c b/bin/get_path_type.c index 4a68d2d..0221479 100644 --- a/bin/get_path_type.c +++ b/bin/get_path_type.c @@ -40,6 +40,7 @@ int main(int ac, char **av) struct json_object *param = json_object_array_get_idx(parameters, i); struct json_object *name_obj; + param = try_get_ref(j_file, param); OBJ_GET(param, "name", &name_obj); const char *name = json_object_get_string(name_obj); struct json_object *type; diff --git a/bin/helper.h b/bin/helper.h index 4e75740..362684b 100644 --- a/bin/helper.h +++ b/bin/helper.h @@ -16,7 +16,7 @@ } while (0) \ -char *componant_name_from_get_or_post(struct json_object *post_or_get) +static char *componant_name_from_get_or_post(struct json_object *post_or_get) { char *componant_name; struct json_object *request_body; @@ -46,7 +46,7 @@ char *componant_name_from_get_or_post(struct json_object *post_or_get) return NULL; } -struct json_object *oneof_or_anyof(struct json_object *param) +static struct json_object *oneof_or_anyof(struct json_object *param) { struct json_object *anyof_or_oneof; int ret; @@ -64,7 +64,7 @@ struct json_object *oneof_or_anyof(struct json_object *param) } -struct json_object *get_or_post_from_path(struct json_object *path) +static struct json_object *get_or_post_from_path(struct json_object *path) { struct json_object *post_or_get; int ret; @@ -81,7 +81,7 @@ struct json_object *get_or_post_from_path(struct json_object *path) return NULL; } -struct json_object *get_path_from_file(struct json_object *j_file, char *path) +static struct json_object *get_path_from_file(struct json_object *j_file, char *path) { struct json_object *paths; struct json_object *func; @@ -95,4 +95,45 @@ struct json_object *get_path_from_file(struct json_object *j_file, char *path) return NULL; } +static struct json_object *try_get_ref(struct json_object *in, struct json_object *param) +{ + int ret; + struct json_object *ref = NULL; + struct json_object *json_ret = NULL; + + ret = json_object_object_get_ex(param, "$ref", &ref); + if (!ret) + return param; + const char *ref_str = json_object_get_string(ref); + + ref_str = strchr(ref_str, '/'); + if (!ref_str || !*ref_str) { + return NULL; + } + param = in; + ++ref_str; + char *cpy = strdup(ref_str); + ref_str = cpy; + + char *ref_str_2; + +again: + + ref_str_2 = strchr(ref_str, '/'); + if (ref_str_2) { + *ref_str_2 = 0; + } + ret = json_object_object_get_ex(param, ref_str, ¶m); + if (!ret) + goto err; + if (ref_str_2) { + ref_str = ref_str_2 + 1; + goto again; + } + json_ret = param; +err: + free(cpy); + return json_ret; +} + #endif diff --git a/bin/path_to_snakecase.c b/bin/path_to_snakecase.c index ec57048..68495d5 100644 --- a/bin/path_to_snakecase.c +++ b/bin/path_to_snakecase.c @@ -24,7 +24,7 @@ int main(int ac, char **av) if (c == '{' || c == '}') { continue; } - if (c == '/') { + if (c == '/' || c == '.') { PUT_UNDERSCORE(); } else if (isupper(c)) { if (!first) { diff --git a/cognac_gen.sh b/cognac_gen.sh index 07a5e8d..01cf3cf 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -14,7 +14,7 @@ source ./helper.sh debug "debug mode is on" debug "Get functions call from osc-api.json path: $FROM_PATH" -default_endpoint=$(cat osc-api-api.json | jq -r .servers[0].url) +default_endpoint=$(cat osc-api.json | jq -r .servers[0].url) if [[ "$default_endpoint" == "null" ]]; then default_endpoint='https://api.{region}.outscale.com/api/v1' fi @@ -30,8 +30,8 @@ if [[ $path_begin_l != 0 ]]; then default_endpoint=${default_endpoint:0:-$path_begin_l} fi -debug $path_begin -debug $default_endpoint +debug "endpoint:" $default_endpoint +debug "path begin:" $path_begin dash_this_arg() @@ -83,7 +83,7 @@ cli_c_type_parser() type=$2 indent_base=$3 indent_plus="$indent_base " - snake_a=$(to_snakecase <<< $a) + snake_a=$(bin/path_to_snakecase $a) snake_a=$( if [ "default" == "$snake_a" ] ; then echo default_arg; else echo $snake_a; fi ) if [ 'int' == "$type" ]; then @@ -254,7 +254,6 @@ replace_args() echo -ne $D3 elif [ "$arg_check" == "____make_default_endpoint____" ]; then debug "____make_default_endpoint____" - debug "$default_endpoint" bin/construct_endpoint "$default_endpoint" elif [ "$arg_check" == "____call_list_args_descriptions____" ]; then debug "____call_list_args_descriptions____" @@ -302,16 +301,16 @@ replace_args() fi for s in $COMPLEX_STRUCT; do - struct_name=$(to_snakecase <<< $s) + struct_name=$(bin/path_to_snakecase $s) - A_LST=$(jq .components.schemas.$s < osc-api.json | json-search -Kn properties | tr -d '",[]') + A_LST=$(./bin/get_argument_list osc-api.json "$s") if [ "$A_LST" != "null" ]; then echo "static int ${struct_name}_setter(struct ${struct_name} *args, struct osc_str *data);" fi done for s in $COMPLEX_STRUCT; do - struct_name=$(to_snakecase <<< $s) - A_LST=$(jq .components.schemas.$s < osc-api.json | json-search -Kn properties | tr -d '",[]') + struct_name=$(bin/path_to_snakecase $s) + A_LST=$(./bin/get_argument_list osc-api.json "$s") if [ "$A_LST" != "null" ]; then cat < /dev/null) osub_ref=$(cut -c 2- <<< $osub_ref | sed 's|/|.|g') local sub_ref_properties=$(jq $osub_ref.properties < osc-api.json 2> /dev/null) @@ -79,7 +83,12 @@ get_type_direct() { local the_one=$(jq .[0] <<< $one_of) get_type_direct "$the_one" else - echo ref $(json-search -R '$ref' <<< ${arg_info} | cut -d '/' -f 4) + local ref=$(json-search -R '$ref' <<< ${arg_info}) + if [[ $ref == "null" ]]; then + echo "ref null" + else + echo ref $(cut -d '/' -f 4 <<< $ref) + fi fi } diff --git a/mk_args.c.sh b/mk_args.c.sh index 46f60fe..7688ce0 100755 --- a/mk_args.c.sh +++ b/mk_args.c.sh @@ -43,7 +43,7 @@ type_to_ctype() { int is_set_${snake_name}; EOF t=$( cut -f 2 -d ' ' <<< $t ) - c_type="struct $(to_snakecase <<< $t) " + c_type="struct $(bin/path_to_snakecase $t) " elif [ "array" == $( cut -d ' ' -f 1 <<< $t) ]; then if [ "ref" == $( cut -d ' ' -f 2 <<< $t) ]; then t=$( cut -f 3 -d ' ' <<< $t ) @@ -51,7 +51,7 @@ EOF char *${snake_name}_str; int nb_${snake_name}; EOF - c_type="struct $(to_snakecase <<< $t) *" + c_type="struct $(bin/path_to_snakecase $t) *" fi fi echo " ${c_type}${snake_name};" @@ -64,15 +64,15 @@ write_struct() { if [ "$st_info" == "" ]; then st_info=$(jq .components.schemas.$s0 < osc-api.json) - A_LST=$(json-search -K properties <<< $st_info | tr -d '",[]') + A_LST=$(./bin/get_argument_list osc-api.json "$s") fi - st_s_name=$(to_snakecase <<< $s0) + st_s_name=$(bin/path_to_snakecase $s0) echo "struct $st_s_name {" for a in $A_LST; do local t=$(get_type3 "$st_info" "$a") - local snake_n=$(to_snakecase <<< $a) + local snake_n=$(bin/path_to_snakecase $a) echo ' /*' get_type_description "$st_info" "$a" | tr -d '"' | fold -s -w70 | sed -e 's/^/ * /g' echo ' */' @@ -93,7 +93,7 @@ create_struct() { #for s in "skip"; do local s="$1" local st0_info=$(jq .components.schemas.$s < osc-api.json) - local A0_LST=$(json-search -Kn properties <<< $st0_info | tr -d '",[]') + local A0_LST=$(./bin/get_argument_list osc-api.json "$s") if [ "${structs[$s]}" != "" ]; then return @@ -138,7 +138,7 @@ for l in $CALL_LIST ;do echo " */" for x in $ARGS_LIST ;do - snake_name=$(to_snakecase <<< "$x") + snake_name=$(bin/path_to_snakecase "$x") t=$(get_type "$l" "$x") #echo "get type: $func $x" From 20bee6c70fe9f5fd0b5b84e90d800c7ed8d32a25 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 17 Dec 2024 16:23:29 +0100 Subject: [PATCH 26/29] add documentation Signed-off-by: Matthias Gatto --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a55bdb5..8c48737 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,9 @@ Or you can generate functions using what is inside `paths`, using `./configure - *Note: There are two versions of yq: one written in Python and one in Go. The default version depends on your distribution. On Arch-based distributions, the Python version is typically the default, whereas on Debian-based distributions, the Go version is default. COGNAC supports both, but to use the Go version, you need to pass `--yq-go` to `./configure`.* -### Example: Generating a CLI for a New API +### Examples: Generating a CLI for a New API + +#### Configure cognac for an Api using elements in schema as entry point for API Calls Let’s say you have an API that is not the Outscale API, and you want to generate a CLI for it. You have a URL to a YAML file, such as `https://ponyapi.yolo/`, and the API request components are named `XInput` instead of `XRequest`. @@ -71,6 +73,23 @@ Here’s what the script does: 1. Retrieves the API in YAML format using curl -s `https://ponyapi.yolo/.` 2. Converts the YAML to JSON using yq `$(YQ_ARG)`. *Note the usage of `$(YQ_ARG)`, so ./configure can handle go version of yq* + +#### Configure cognac for an Api using elements in path as entry point for API Calls + +For this example we will use [guru](https://apis.guru/api-doc/) + +Run the following commande +``` +./configure --sdk-name=guru-sdk --cli-name=guru --from-path --api-script='curl -s https://api.apis.guru/v2/openapi.yaml | yq $(YQ_ARG)" > osc-api.json' +``` + +`--cli-name=guru` set the generated binary name to `guru` +`--sdk-name=guru-sdk` set the generate sdk name and UserAgent to `guru-sdk` +`--from-path` generate API calls from elements in `paths` instead of `components.schemas` + + +#### Generte the code + Once this setup is complete, you can now use the Makefile. It's also a good idea to run ./configure --help, as it contains several useful options. - `--wget-json-search`: Helps with downloading `json-search`, which can be tricky to install, **If unsure, we recommend using this by default** - `--compile-json-c`: Ensures a recent version of `json-c` is compiled, required for color support. **If unsure, we recommend using this by default** From c51d57776806bf64e2d7634b5247a2cc85216b65 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Thu, 19 Dec 2024 14:39:38 +0100 Subject: [PATCH 27/29] improve readme Signed-off-by: Matthias Gatto --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8c48737..dedd926 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ For the Outscale API, the YAML source is converted to JSON using `yq`: (https:// When generating API calls, COGNAC by default assumes that the OpenAPI file contains components named CallRequest. For example, if the API has a call named `CreatePony`, the corresponding component should be located at `#/components/schemas/CreatePonyRequest`. -You can change the suffix of functions using `./configure --function-suffix FUNCTION_SUFFIX` +You can modify the suffix of functions by using `--function-suffix=FUNCTION_SUFFIX` option with `./configure` command. Or you can generate functions using what is inside `paths`, using `./configure --from-path` @@ -58,8 +58,8 @@ Run the following command: ```bash --function-suffix Input ``` -Look for Function named XInput instead of XRequest. +Search for functions named XInput instead of XRequest. ```bash --api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG) > osc-api.json' ``` From 7f9efba1c99e26695a62dea48b8bb072f5d9afd0 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 20 Dec 2024 17:24:30 +0100 Subject: [PATCH 28/29] improve Doc Signed-off-by: Matthias Gatto --- HACKING.md | 17 ++++++++--------- README.md | 16 ++++++++-------- bin/README.md | 33 +++++++++++++++++---------------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/HACKING.md b/HACKING.md index ab4f762..3d28f02 100644 --- a/HACKING.md +++ b/HACKING.md @@ -2,12 +2,11 @@ *Essential Information for Hacking COGNAC* -## Enabling debug +## Enabling Debug Mode -To enable debug, use `--debug-mode` in `./configure` +To enable debug mode, use the `--debug-mode` option with the `./configure` command -then the `debug` commande in the shell script will print some information. -without this option debug will print nothing +Once enabled, the `debug` command in the shell script will print debug information. Without this option, the debug command will not output any information. ## Unix Philosophy and COGNAC Paradigms. @@ -42,17 +41,17 @@ A relatively easy way to add a feature in Cognac is to start by modifying the ge For exemple, if you modify a function like `parse_thatarg()` in "osc_sdk.c", once the changes are working, you can then search (using a tool like grep) through the generator code to find where `parse_thatarg` is generated. From there, you can add modifications to the generator to make the change permanent and automated. -## Example to add a new API, that doesn't work at first. +## Example: Adding a New API That Initially Doesn't Work. -Let's say you ahve configure cognac like this +Let's say you have configured Cognac like this: ```sh ./configure --sdk-name=myapi-sdk --cli-name=guru --api-script='cat myapi-api.json > osc-api.json' --debug-mode --from-path ``` -doing that you will use try to generate the api from path, with debug enable. +By doing that, you will attempt to generate the API from the path with debug enabled. -now let's say you have this output while calling `make`: +Now, let's say you get this output while running `make`: ``` ____complex_struct_func_parser____ nothing found____cli_parser____ @@ -60,7 +59,7 @@ nothing founderror in jsonnothing founderror in jsonnothing foun debug mode is on ``` -This mean that durring `____complex_struct_func_parser____` `____cli_parser____`, the scripts fails to pasre some part of osc-api json. +This mean that during `____complex_struct_func_parser____` `____cli_parser____`, the script fails to parse some parts of `osc-api.json`. To debug it, you should look at what happen durring `____complex_struct_func_parser____` and `____cli_parser____` inside `cognac_gen.sh` diff --git a/README.md b/README.md index dedd926..8f034fa 100644 --- a/README.md +++ b/README.md @@ -35,13 +35,13 @@ For example, if the API has a call named `CreatePony`, the corresponding compone You can modify the suffix of functions by using `--function-suffix=FUNCTION_SUFFIX` option with `./configure` command. -Or you can generate functions using what is inside `paths`, using `./configure --from-path` +Alternatively, you can generate functions based on the contents of `paths` by using the `./configure --from-path` option. *Note: There are two versions of yq: one written in Python and one in Go. The default version depends on your distribution. On Arch-based distributions, the Python version is typically the default, whereas on Debian-based distributions, the Go version is default. COGNAC supports both, but to use the Go version, you need to pass `--yq-go` to `./configure`.* ### Examples: Generating a CLI for a New API -#### Configure cognac for an Api using elements in schema as entry point for API Calls +#### Configure Cognac for an API using elements in the schema as entry points for API calls. Let’s say you have an API that is not the Outscale API, and you want to generate a CLI for it. You have a URL to a YAML file, such as `https://ponyapi.yolo/`, and the API request components are named `XInput` instead of `XRequest`. @@ -74,21 +74,21 @@ Here’s what the script does: 2. Converts the YAML to JSON using yq `$(YQ_ARG)`. *Note the usage of `$(YQ_ARG)`, so ./configure can handle go version of yq* -#### Configure cognac for an Api using elements in path as entry point for API Calls +#### Configure Cognac for an API using elements in the path as entry points for API calls. For this example we will use [guru](https://apis.guru/api-doc/) -Run the following commande +Run the following command: ``` ./configure --sdk-name=guru-sdk --cli-name=guru --from-path --api-script='curl -s https://api.apis.guru/v2/openapi.yaml | yq $(YQ_ARG)" > osc-api.json' ``` -`--cli-name=guru` set the generated binary name to `guru` -`--sdk-name=guru-sdk` set the generate sdk name and UserAgent to `guru-sdk` -`--from-path` generate API calls from elements in `paths` instead of `components.schemas` +`--cli-name=guru`: Sets the generated binary name to `guru`. +`--sdk-name=guru-sdk`: Sets the generated SDK name and the UserAgent to `guru-sdk`. +`--from-path`: Generates API calls from elements in `paths` instead of `components.schemas`. -#### Generte the code +#### Generate the Code Once this setup is complete, you can now use the Makefile. It's also a good idea to run ./configure --help, as it contains several useful options. - `--wget-json-search`: Helps with downloading `json-search`, which can be tricky to install, **If unsure, we recommend using this by default** diff --git a/bin/README.md b/bin/README.md index d9719dc..c5611f6 100644 --- a/bin/README.md +++ b/bin/README.md @@ -25,9 +25,9 @@ This is also much faster than multiple calls to grep. This utility is used to check if osc-api.json has been generated correctly. It verifies that the file is a valid JSON and contains at least one API function. # path_to_snakecase -snakecasise an object name. -example `ReadVms` become `read_vms` -`/my/{NAME}/getId` become `my_name_get_id` +Converts an object name to snake_case. +example `ReadVms` becomes `read_vms` +`/my/{NAME}/getId` becomes `my_name_get_id` usage: ``` @@ -35,9 +35,9 @@ usage: ``` # path_to_camelcase -camelcasise an object name. -example `ReadVms` become `read_vms` -`/my/{NAME}/getId` become `my_name_get_id` +Converts an object name to camelCase. +example `read_vms` become `ReadVms` +`/my/{NAME}/getId` become `myNameGetId` usage: ``` @@ -46,7 +46,8 @@ usage: # get_argument_list -Take an object componant in `paths` or in `components.schema`, and give a list of all it arguments. + +Retrieves an object component from `paths` or `components.schema` and returns a list of all its arguments. usage: ``` @@ -85,7 +86,8 @@ string # get_path_description -Just return "" so far, as the api I test this with, had no description in the path + +Returns an empty string "" for now, as the API I tested this with had no description in the path. usage: ``` @@ -99,12 +101,11 @@ example: # arg_placement -return where the argument is used. -`path`, if the argument is use in the path -`header` if it is an http header -`query` if it is in a query string -`data` if it is use in post data - +Return where the argument is used: +`path`: If the argument is used in the URL path. +`header`: If the argument is used as an HTTP header. +`query`: If the argument is in a query string. +`data`: If the argument is used in POST data. usage: ``` @@ -119,7 +120,7 @@ query # construct_path -generate the C code to create a osc_str that the path of the call. +Generate the C code to create an osc_str for the path of the call. example: @@ -131,4 +132,4 @@ osc_str_append_string(&end_call, args->id); osc_str_append_string(&end_call, "/get"); ``` -The code generated is not exhaustive, but you get the idea. +The generated code is not exhaustive, but it conveys the general idea. From d6d23e3d55ca04ccb27cdb737cc6b95c2aecb1a9 Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 20 Dec 2024 17:29:55 +0100 Subject: [PATCH 29/29] improve configure --help Signed-off-by: Matthias Gatto --- configure | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configure b/configure index 3a535b1..a1a40e4 100755 --- a/configure +++ b/configure @@ -37,12 +37,12 @@ for arg in $@; do --debug-mode enable debug in cognac (output on stderr) --function-suffix=suffix suffix use to find function in componants, default: Request --funclist-arg=arg arguments to pass to funclist - --from-path use paths instead of componant suffix for functions generations + --from-path Uses paths instead of component suffixes for function generation --api-script=script how to generate osc-api.json --yq-go use go version of yq, that use diferents arguments --curl-path=PATH path to curl source root --sdk-version sdk-version to generate (format 0xMajMajminMinPatchPatch(0x000302)) - --target-api=VERSION choose a version, 'master' is the default + --target-api=VERSION choose a version, 'master' is the default --curl-install-path=PATH path to curl installed root --list-api-versions list-api-versions on github EOF