forked from vladimir-kotikov/clink-completions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubectl.lua
81 lines (71 loc) · 1.92 KB
/
kubectl.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local w = require('tables').wrap
local parser = clink.arg.new_parser
local function exec_kubectl(arguments, template)
local f = io.popen("kubectl "..arguments.." -o template --template=\""..template.."\"")
if not f then return w({}) end
local output = f:read('*all')
f:close()
local res = w({})
for element in output:gmatch("%S+") do table.insert(res, element) end
return res
end
local function get_config(config)
return exec_kubectl("config view", "{{ range ."..config.." }}{{ .name }} {{ end }}")
end
local function get_config_func(config)
return function()
return get_config(config)
end
end
local function get_resources(noun)
return exec_kubectl("get "..noun, "{{ range .items }}{{ .metadata.name }} {{ end }}")
end
local function get_resources_func(noun)
return function()
return get_resources(noun)
end
end
local resource_parser = parser(
{
"all" .. parser({get_resources_func("all")}),
"node" .. parser({get_resources_func("node")}),
"service" .. parser({get_resources_func("service")}),
"pod" .. parser({get_resources_func("pod")}),
"deployment" .. parser({get_resources_func("deployment")})
}
)
local scale_parser = parser(
{
"deployment" .. parser({get_resources_func("deployment")}, parser({"--replicas"}))
}
)
local config_parser = parser(
{
"current-context",
"delete-cluster",
"delete-context",
"get-clusters",
"get-contexts",
"rename-context",
"set",
"set-cluster",
"set-context",
"set-credentials",
"unset",
"use-context" .. parser({get_config_func("contexts")}),
"view"
}
)
local kubectl_parser = parser(
{
"apply",
"exec" .. parser({get_resources_func("pod")}, parser({ "-it"})),
"get" .. resource_parser,
"describe" .. resource_parser,
"logs" .. parser({get_resources_func("pod")}),
"port-forward" .. parser({get_resources_func("pod")}),
"scale" .. scale_parser,
"config" .. config_parser
}
)
clink.arg.register_parser("kubectl", kubectl_parser)