Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bonny.init mix task #160

Merged
merged 13 commits into from
Oct 2, 2022
Prev Previous commit
Next Next commit
ad api version validation
  • Loading branch information
Michael Ruoss committed Oct 1, 2022
commit 270e76d2557e80fb0764f5f37319bafefb8a3456
24 changes: 18 additions & 6 deletions lib/mix/tasks/bonny.init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Mix.Tasks.Bonny.Init do
requests: %{cpu: "200m", memory: "200Mi"}
}
@rfc_1123_subdomain_check ~r/^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/
@dns_1035_label_check ~r/^[a-z]([-a-z0-9]*[a-z0-9])?$/

def run(_args) do
input =
Expand Down Expand Up @@ -59,12 +60,22 @@ defmodule Mix.Tasks.Bonny.Init do
"Please enter the API Version of your controller in Elixir module form, e.g. V1 or V1Alpha1"
)

input
|> Keyword.put(
:version,
"#{Mix.Bonny.app_name()}.API.#{Mix.Bonny.ensure_module_name(version)}"
)
|> get_input()
next_input =
if valid_dns_1035_label?(String.downcase(version)) do
input
|> Keyword.put(
:version,
"#{Mix.Bonny.app_name()}.API.#{Mix.Bonny.ensure_module_name(version)}"
)
else
Mix.Bonny.error(
"Invalid value: #{inspect(input[:version])}. A DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character"
)

input
end

get_input(next_input)

is_nil(input[:namespace]) ->
namespace =
Expand Down Expand Up @@ -191,4 +202,5 @@ defmodule Mix.Tasks.Bonny.Init do
end

defp valid_rfc_1123_subdomain?(string), do: String.match?(string, @rfc_1123_subdomain_check)
defp valid_dns_1035_label?(string), do: String.match?(string, @dns_1035_label_check)
end