Packages
oaspec
0.1.3
0.68.0
0.67.0
0.66.0
0.65.0
0.64.0
0.63.0
0.62.0
0.61.0
0.60.0
0.59.0
0.58.1
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.3
0.6.1
0.6.0
0.5.0
0.4.0
0.3.0
0.1.3
Generate Gleam code from OpenAPI 3.x specifications
Current section
Files
Jump to
Current section
Files
src/oaspec@cli.erl
-module(oaspec@cli).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/cli.gleam").
-export([app/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/oaspec/cli.gleam", 94).
?DOC(" Create a config file template.\n").
-spec run_init(binary()) -> nil.
run_init(Path) ->
Template = <<"# oaspec configuration file
# See https://github.com/nao1215/oaspec for documentation.
# Path to your OpenAPI 3.x specification (YAML or JSON).
input: openapi.yaml
# Gleam module namespace for generated code.
# Generated imports will be `import <package>/types`, etc.
package: api
# Generation mode: server, client, or both (default: both).
# mode: both
# Output settings (optional).
# output:
# dir: ./gen # Base output directory (default: ./gen)
# server: ./gen/api # Override server output path
# client: ./gen/api_client # Override client output path
"/utf8>>,
case simplifile_erl:is_file(Path) of
{ok, true} ->
gleam_stdlib:println(
<<<<"Error: "/utf8, Path/binary>>/binary,
" already exists"/utf8>>
),
erlang:halt(1);
_ ->
case simplifile:write(Path, Template) of
{ok, _} ->
gleam_stdlib:println(<<"Created "/utf8, Path/binary>>);
{error, _} ->
gleam_stdlib:println(
<<"Error: failed to write "/utf8, Path/binary>>
),
erlang:halt(1)
end
end.
-file("src/oaspec/cli.gleam", 73).
?DOC(" The init command definition.\n").
-spec init_command() -> glint:command(nil).
init_command() ->
begin
glint:flag(
begin
_pipe = glint:string_flag(<<"output"/utf8>>),
_pipe@1 = glint:flag_default(_pipe, <<"./oaspec.yaml"/utf8>>),
glint:flag_help(
_pipe@1,
<<"Output path for the config file"/utf8>>
)
end,
fun(Output_path) ->
glint:command_help(
<<"Create a oaspec.yaml config file"/utf8>>,
fun() ->
glint:command(
fun(_, _, Flags) ->
Path = case Output_path(Flags) of
{ok, P} ->
P;
{error, _} ->
<<"./oaspec.yaml"/utf8>>
end,
run_init(Path)
end
)
end
)
end
)
end.
-file("src/oaspec/cli.gleam", 233).
?DOC(" Get list length.\n").
-spec list_length(list(any())) -> integer().
list_length(Items) ->
case Items of
[] ->
0;
[_ | Rest] ->
1 + list_length(Rest)
end.
-file("src/oaspec/cli.gleam", 134).
?DOC(" Execute the generation pipeline.\n").
-spec run_generate(binary(), binary(), binary()) -> nil.
run_generate(Config_path, Mode_str, Output_str) ->
gleam_stdlib:println(<<"oaspec v"/utf8, (<<"0.1.2"/utf8>>)/binary>>),
gleam_stdlib:println(<<"Loading config from: "/utf8, Config_path/binary>>),
Cfg = case oaspec@config:load(Config_path) of
{ok, C} ->
C;
{error, E} ->
gleam_stdlib:println(
<<"Error: "/utf8, (oaspec@config:error_to_string(E))/binary>>
),
erlang:halt(1),
erlang:error(#{gleam_error => panic,
message => <<"unreachable"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"oaspec/cli"/utf8>>,
function => <<"run_generate"/utf8>>,
line => 148})
end,
Cfg@1 = case Mode_str of
<<""/utf8>> ->
Cfg;
_ ->
case oaspec@config:parse_mode(Mode_str) of
{ok, M} ->
oaspec@config:with_mode(Cfg, M);
{error, E@1} ->
gleam_stdlib:println(
<<"Error: "/utf8,
(oaspec@config:error_to_string(E@1))/binary>>
),
erlang:halt(1),
erlang:error(#{gleam_error => panic,
message => <<"unreachable"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"oaspec/cli"/utf8>>,
function => <<"run_generate"/utf8>>,
line => 161})
end
end,
Cfg@2 = case Output_str of
<<""/utf8>> ->
Cfg@1;
Path ->
oaspec@config:with_output(Cfg@1, {some, Path})
end,
gleam_stdlib:println(
<<"Parsing OpenAPI spec: "/utf8, (erlang:element(2, Cfg@2))/binary>>
),
Spec = case oaspec@openapi@parser:parse_file(erlang:element(2, Cfg@2)) of
{ok, S} ->
S;
{error, E@2} ->
Detail@3 = case E@2 of
{file_error, Detail} ->
Detail;
{yaml_error, Detail@1} ->
Detail@1;
{missing_field, Path@1, Field} ->
<<<<<<"Missing field '"/utf8, Field/binary>>/binary,
"' at "/utf8>>/binary,
Path@1/binary>>;
{invalid_value, Path@2, Detail@2} ->
<<<<<<"Invalid value at "/utf8, Path@2/binary>>/binary,
": "/utf8>>/binary,
Detail@2/binary>>
end,
gleam_stdlib:println(<<"Error: "/utf8, Detail@3/binary>>),
erlang:halt(1),
erlang:error(#{gleam_error => panic,
message => <<"unreachable"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"oaspec/cli"/utf8>>,
function => <<"run_generate"/utf8>>,
line => 188})
end,
gleam_stdlib:println(
<<<<<<"Spec loaded: "/utf8,
(erlang:element(2, erlang:element(3, Spec)))/binary>>/binary,
" v"/utf8>>/binary,
(erlang:element(4, erlang:element(3, Spec)))/binary>>
),
Ctx = oaspec@codegen@context:new(Spec, Cfg@2),
Validation_errors = oaspec@codegen@validate:validate(Ctx),
case gleam@list:is_empty(Validation_errors) of
true ->
nil;
false ->
gleam_stdlib:println(
<<"Error: OpenAPI spec contains unsupported features:"/utf8>>
),
gleam@list:each(
Validation_errors,
fun(E@3) ->
gleam_stdlib:println(
<<" - "/utf8,
(oaspec@codegen@validate:error_to_string(E@3))/binary>>
)
end
),
erlang:halt(1)
end,
gleam_stdlib:println(<<"Generating code..."/utf8>>),
case oaspec@codegen@writer:generate_all(Ctx) of
{ok, Files} ->
gleam_stdlib:println(<<""/utf8>>),
gleam_stdlib:println(
<<<<"Successfully generated "/utf8,
(erlang:integer_to_binary(list_length(Files)))/binary>>/binary,
" files"/utf8>>
);
{error, E@4} ->
gleam_stdlib:println(
<<"Error: "/utf8,
(oaspec@codegen@writer:error_to_string(E@4))/binary>>
),
erlang:halt(1)
end.
-file("src/oaspec/cli.gleam", 24).
?DOC(" The generate command definition.\n").
-spec generate_command() -> glint:command(nil).
generate_command() ->
begin
glint:flag(
begin
_pipe = glint:string_flag(<<"config"/utf8>>),
_pipe@1 = glint:flag_default(_pipe, <<"./oaspec.yaml"/utf8>>),
glint:flag_help(_pipe@1, <<"Path to config file"/utf8>>)
end,
fun(Config_path) ->
glint:flag(
begin
_pipe@2 = glint:string_flag(<<"mode"/utf8>>),
_pipe@3 = glint:flag_default(_pipe@2, <<""/utf8>>),
glint:flag_help(
_pipe@3,
<<"Generation mode: server, client, or both (overrides config)"/utf8>>
)
end,
fun(Mode) ->
glint:flag(
begin
_pipe@4 = glint:string_flag(<<"output"/utf8>>),
_pipe@5 = glint:flag_default(
_pipe@4,
<<""/utf8>>
),
glint:flag_help(
_pipe@5,
<<"Output directory override"/utf8>>
)
end,
fun(Output) ->
glint:command_help(
<<"Generate Gleam code from an OpenAPI specification"/utf8>>,
fun() ->
glint:command(
fun(_, _, Flags) ->
Config_path@1 = case Config_path(
Flags
) of
{ok, P} ->
P;
{error, _} ->
<<"./oaspec.yaml"/utf8>>
end,
Mode_str = case Mode(Flags) of
{ok, M} ->
M;
{error, _} ->
<<"both"/utf8>>
end,
Output_str = case Output(Flags) of
{ok, O} ->
O;
{error, _} ->
<<""/utf8>>
end,
run_generate(
Config_path@1,
Mode_str,
Output_str
)
end
)
end
)
end
)
end
)
end
)
end.
-file("src/oaspec/cli.gleam", 14).
?DOC(" Set up the CLI application.\n").
-spec app() -> glint:glint(nil).
app() ->
_pipe = glint:new(),
_pipe@1 = glint:with_name(_pipe, <<"oaspec"/utf8>>),
_pipe@2 = glint:global_help(
_pipe@1,
<<"Generate Gleam code from OpenAPI 3.x specifications"/utf8>>
),
_pipe@3 = glint:pretty_help(_pipe@2, glint:default_pretty_help()),
_pipe@4 = glint:add(_pipe@3, [<<"generate"/utf8>>], generate_command()),
glint:add(_pipe@4, [<<"init"/utf8>>], init_command()).