From c50045ad0ca363f4e08c2eee0f0b5faef7524ff4 Mon Sep 17 00:00:00 2001 From: "Paulo F. Oliveira" Date: Mon, 13 Nov 2023 18:16:00 +0000 Subject: [PATCH] Move implementation to Bash script --- README.md | 14 +++----------- erlang/kivra_api_errors.erl | 23 +---------------------- util/to-developer.kivra.com.sh | 13 +++++++++++++ 3 files changed, 17 insertions(+), 33 deletions(-) create mode 100755 util/to-developer.kivra.com.sh diff --git a/README.md b/README.md index f0010ec..5170746 100644 --- a/README.md +++ b/README.md @@ -130,21 +130,13 @@ Run the linter to validate the error definitions in [`api-errors.json`](./api-er ## Export for use in `developer.kivra.com` -A helper function is made available to allow copy-pasting the entire list for +A `util` script is made available to allow copy-pasting the entire list for acceptance in . -### Erlang - -Boot up an Erlang shell +For this you need to have [`jq`](https://github.com/jqlang/jq#installation) installed. ```console -rebar3 shell -``` - -Call the `kivra_api_errors:to_developer_kivra_com/0` function: - -```erlang -kivra_api_errors:to_developer_kivra_com(). +./util/to-developer.kivra.com.sh ``` Whatever is between `` and `` is deemed usable diff --git a/erlang/kivra_api_errors.erl b/erlang/kivra_api_errors.erl index bf662dd..f298763 100644 --- a/erlang/kivra_api_errors.erl +++ b/erlang/kivra_api_errors.erl @@ -5,8 +5,7 @@ -export([ load/0, load/1, - from_code/1, from_code/2, from_code/3, - to_developer_kivra_com/0 + from_code/1, from_code/2, from_code/3 ]). %%%_* Types =================================================================== @@ -38,26 +37,6 @@ load(Config) -> Error end. --spec to_developer_kivra_com() -> ok. -to_developer_kivra_com() -> - ErrorsFile = filename:join([code:priv_dir(?MODULE), <<"api-errors.json">>]), - {ok, ErrorsAsJSON} = file:read_file(ErrorsFile), - Errors = jiffy:decode(ErrorsAsJSON, [return_maps]), - ErrorsList = maps:to_list(Errors), - SortedErrorsList = lists:sort(fun ({CodeLeft, _}, {CodeRight, _}) -> CodeLeft < CodeRight end, ErrorsList), - - io:format("Copy-paste to developer.kivra.com's swagger.yml~n"), - io:format("~n"), - io:format(" | Code | Short Message | Long Message |~n"), - io:format(" | ---- | ------------- | ------------ |~n"), - lists:foreach( - fun ({Code, #{<<"short_message">> := ShortMessage, <<"long_message">> := LongMessage}}) -> - io:format(" | ~ts | ~ts | ~ts |~n", [Code, ShortMessage, LongMessage]) - end, - SortedErrorsList - ), - io:format("~n"). - -spec from_code(binary() | pos_integer()) -> {ok, {status_code(), payload_map() | payload_kv()}} | {error, notfound}. from_code(ErrorCode) when is_integer(ErrorCode) -> from_code(integer_to_binary(ErrorCode)); diff --git a/util/to-developer.kivra.com.sh b/util/to-developer.kivra.com.sh new file mode 100755 index 0000000..22bb45d --- /dev/null +++ b/util/to-developer.kivra.com.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +if ! command -v jq &>/dev/null; then + echo "You need 'jq' to run this script." + exit 1 +fi + +echo "Copy-paste to developer.kivra.com's swagger.yml" +echo "" +echo " | Code | Short Message | Long Message |" +echo " | ---- | ------------- | ------------ |" +jq -r 'to_entries | sort_by(.key) | .[] | " | \(.key) | \(.value.short_message) | \(.value.long_message) |"' "