Skip to content

Commit

Permalink
Move implementation to Bash script
Browse files Browse the repository at this point in the history
  • Loading branch information
kivra-pauoli committed Nov 13, 2023
1 parent 20665f7 commit c50045a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://developer.kivra.com>.

### 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 `<copy-paste>` and `</copy-paste>` is deemed usable
Expand Down
23 changes: 1 addition & 22 deletions erlang/kivra_api_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===================================================================
Expand Down Expand Up @@ -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("<copy-paste>~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("</copy-paste>~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));
Expand Down
13 changes: 13 additions & 0 deletions util/to-developer.kivra.com.sh
Original file line number Diff line number Diff line change
@@ -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 "<copy-paste>"
echo " | Code | Short Message | Long Message |"
echo " | ---- | ------------- | ------------ |"
jq -r 'to_entries | sort_by(.key) | .[] | " | \(.key) | \(.value.short_message) | \(.value.long_message) |"' <api-errors.json
echo "</copy-paste>"

0 comments on commit c50045a

Please sign in to comment.