Skip to content

Commit ea7378f

Browse files
committed
Add mix kubegen.search
1 parent a598a48 commit ea7378f

File tree

4 files changed

+67
-36
lines changed

4 files changed

+67
-36
lines changed

README.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
# Kubegen
22

3-
**TODO: Add description**
3+
Generate resource scoped Kubernetes clients with `Kubegen`.
4+
5+
[![Module Version](https://img.shields.io/hexpm/v/kubegen.svg)](https://hex.pm/packages/kubegen)
6+
[![Last Updated](https://img.shields.io/github/last-commit/mruoss/kubegen.svg)](https://github.com/mruoss/kubegen/commits/main)
7+
8+
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/kubegen/)
9+
[![Total Download](https://img.shields.io/hexpm/dt/kubegen.svg)](https://hex.pm/packages/kubegen)
10+
[![License](https://img.shields.io/hexpm/l/kubegen.svg)](https://github.com/mruoss/kubegen/blob/main/LICENSE)
411

512
## Installation
613

7-
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
8-
by adding `kubegen` to your list of dependencies in `mix.exs`:
14+
`kubegen` is a code generator. Add the package as dev dependency:
915

1016
```elixir
1117
def deps do
1218
[
13-
{:kubegen, "~> 0.1.0"}
19+
{:kubegen, "~> 0.1.0", only: :dev, runtime: false}
1420
]
1521
end
1622
```
1723

18-
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
19-
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
20-
be found at <https://hexdocs.pm/kubegen>.
24+
The docs can be found at <https://hexdocs.pm/kubegen>.
2125

26+
## Usage

lib/kubegen/resource.ex

+2-28
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ defmodule Kubegen.Resource do
4343
resource_definition
4444
)
4545

46-
resource_list_path =
47-
resource_list_path(
48-
api_version,
49-
resource_definition
50-
)
51-
5246
generator_module =
5347
if Map.fetch!(resource_definition, "namespaced"),
5448
do: Kubegen.API.Namespaced,
@@ -63,17 +57,16 @@ defmodule Kubegen.Resource do
6357
attributes =
6458
quote do
6559
@resource_path unquote(resource_path)
66-
@resource_list_path unquote(resource_list_path)
6760
end
68-
|> Utils.flatten_blocks()
6961
|> Utils.put_newlines()
62+
|> List.wrap()
7063

7164
req_func =
7265
quote do
7366
defp req() do
7467
Kubeconf.Default
7568
|> Kubeconf.kubeconf()
76-
|> Kubereq.new(@resource_path, @resource_list_path)
69+
|> Kubereq.new(@resource_path)
7770
end
7871
end
7972

@@ -197,23 +190,4 @@ defmodule Kubegen.Resource do
197190
defp do_resource_path(api_version, %{"name" => resource_name, "namespaced" => false}) do
198191
"#{api_version}/#{resource_name}/:name"
199192
end
200-
201-
@spec resource_list_path(api_version :: String.t(), resource_definition :: map()) :: String.t()
202-
defp resource_list_path(<<?v, _::integer>> = api_version, resource_definition) do
203-
do_resource_list_path("api/#{api_version}", resource_definition)
204-
end
205-
206-
defp resource_list_path(api_version, resource_definition) do
207-
do_resource_list_path("apis/#{api_version}", resource_definition)
208-
end
209-
210-
@spec do_resource_list_path(api_version :: String.t(), resource_definition :: map()) ::
211-
String.t()
212-
defp do_resource_list_path(api_version, %{"name" => resource_name, "namespaced" => true}) do
213-
"#{api_version}/namespaces/:namespace/#{resource_name}"
214-
end
215-
216-
defp do_resource_list_path(api_version, %{"name" => resource_name, "namespaced" => false}) do
217-
"#{api_version}/#{resource_name}"
218-
end
219193
end

lib/mix/tasks/kubegen.ex

+10-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,16 @@ defmodule Mix.Tasks.Kubegen do
7575
file_path |> Path.dirname() |> File.mkdir_p!()
7676
File.write(file_path, rendered)
7777

78-
IO.puts("Generated module #{module_name} in file #{file_path}")
78+
Owl.IO.puts([
79+
"Generated module ",
80+
IO.ANSI.green(),
81+
"#{module_name}",
82+
IO.ANSI.reset(),
83+
" in file ",
84+
IO.ANSI.yellow(),
85+
file_path,
86+
IO.ANSI.reset()
87+
])
7988
end
8089
end
8190

lib/mix/tasks/kubegen.search.ex

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule Mix.Tasks.Kubegen.Search do
2+
@moduledoc ~S"""
3+
Search Group-Version-Kind (GVK) for Core Resources.
4+
Kubegen requires you to pass GVK as keys in `config.exs`. This mix tasks
5+
lets you search for GVK for a specific resource kind
6+
"""
7+
@shortdoc "Search Group-Version-Kind for Core Resources."
8+
9+
use Mix.Task
10+
11+
@impl Mix.Task
12+
def run([input]) do
13+
discovery = elem(Code.eval_file("build/discovery.ex"), 0)
14+
gvks = Map.keys(discovery)
15+
16+
if input in gvks do
17+
IO.puts(~s'"#{input} is a valid GVK."')
18+
exit({:shutdown, 0})
19+
end
20+
21+
list =
22+
gvks
23+
|> Enum.map(fn gvk -> {gvk, String.split(gvk, "/") |> Enum.reverse() |> List.first()} end)
24+
|> Enum.map(fn {gvk, kind} -> {gvk, kind, String.jaro_distance(input, kind)} end)
25+
|> Enum.filter(fn {_, _, score} -> score > 0.7 end)
26+
|> Enum.sort_by(&elem(&1, 2), :desc)
27+
|> Enum.map(&[IO.ANSI.green(), elem(&1, 0), IO.ANSI.reset(), "\n"])
28+
29+
case list do
30+
[] ->
31+
Owl.IO.puts([
32+
IO.ANSI.red(),
33+
"No resource matches your search term."
34+
])
35+
36+
list ->
37+
Owl.IO.puts([
38+
"Did you mean one of these?\n\n",
39+
list
40+
])
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)