Repository navigation
Expand file tree
/
Copy pathdocker-containers.lua
More file actions
294 lines (257 loc) · 8.02 KB
/
Copy pathdocker-containers.lua
File metadata and controls
294 lines (257 loc) · 8.02 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
local Morph = require 'morph'
local h = Morph.h
-- Share the Table component with examples/_shared.lua. Load it by path rather
-- than through `require`: a session may replace `package.loaders`, which makes
-- `package.path` irrelevant, while `morph` itself still resolves through the
-- runtimepath. The chunk's own directory locates the sibling file regardless.
---@diagnostic disable-next-line: need-check-nil
local source = debug.getinfo(1, 'S').source
-- File chunks normally name their source with a leading `@`, but some loaders
-- pass the path bare; stripping it unconditionally would eat the leading `/`.
if source:sub(1, 1) == '@' then source = source:sub(2) end
local Shared = dofile(vim.fs.joinpath(vim.fs.dirname(source), '_shared.lua'))
--------------------------------------------------------------------------------
-- Types
--------------------------------------------------------------------------------
--- @class morph.examples.DockerContainer
--- @field id string
--- @field name string
--- @field image string
--- @field status string
--- @field ports string
--- @field created string
--- @field raw unknown
--- @param cmd string
--- @param pos 'J' | 'L'
local function show_term(cmd, pos)
vim.cmd '20new'
vim.cmd.wincmd(pos)
vim.bo.buflisted = false
vim.wo[0][0].number = false
vim.wo[0][0].relativenumber = false
vim.cmd.terminal(cmd)
vim.cmd.startinsert()
end
-- _ _ _
-- | | | | ___| |_ __
-- | |_| |/ _ \ | '_ \
-- | _ | __/ | |_) |
-- |_| |_|\___|_| .__/
-- |_|
--- @param ctx morph.Ctx<{ show_help: boolean }, any>
local function Help(ctx)
if not ctx.props.show_help then return {} end
local help_table = {}
table.insert(help_table, {
h.Constant({}, 'KEY'),
h.Constant({}, 'DESCRIPTION'),
})
local keymaps = {
{ 'gi', 'Inspect container (JSON)' },
{ 'gl', 'View container logs' },
{ 'gx', 'Execute bash in container' },
{ 'gs', 'Start/stop container' },
{ 'gr', 'Restart container' },
{ '<Leader>r', 'Refresh containers' },
{ 'g?', 'Toggle this help' },
}
for _, keymap in ipairs(keymaps) do
table.insert(help_table, {
h.Title({}, keymap[1]),
h.Normal({}, keymap[2]),
})
end
return {
h['@markup.heading']({}, '## Help'),
'\n\n',
h(Shared.Table, { cells = help_table }),
}
end
-- ____ _ _
-- / ___|___ _ __ | |_ __ _(_)_ __ ___ _ __ ___
-- | | / _ \| '_ \| __/ _` | | '_ \ / _ \ '__/ __|
-- | |__| (_) | | | | || (_| | | | | | __/ | \__ \
-- \____\___/|_| |_|\__\__,_|_|_| |_|\___|_| |___/
--- @param ctx morph.Ctx<{ loading: boolean, containers: morph.examples.DockerContainer[] }, { filter: string }>
local function Containers(ctx)
if ctx.phase == 'mount' then ctx.state = { filter = '' } end
local state = assert(ctx.state)
local containers_table = {}
table.insert(containers_table, {
h.Constant({}, 'NAME'),
h.Constant({}, 'IMAGE'),
h.Constant({}, 'ID'),
h.Constant({}, 'STATUS'),
h.Constant({}, 'PORTS'),
})
for _, container in ipairs(ctx.props.containers) do
local passes_filter = state.filter == ''
or container.name:find(state.filter, 1, true) ~= nil
or container.image:find(state.filter, 1, true) ~= nil
or container.id:find(state.filter, 1, true) ~= nil
if passes_filter then
table.insert(containers_table, {
h.Constant({
nmap = {
['gi'] = function()
vim.schedule(
function()
show_term('docker inspect ' .. container.id .. ' | yq -Po yaml ".[0]"', 'L')
end
)
return ''
end,
['gl'] = function()
vim.schedule(function() show_term('docker logs -f ' .. container.id, 'J') end)
return ''
end,
['gx'] = function()
vim.schedule(
function() show_term('docker exec -it ' .. container.id .. ' bash', 'J') end
)
return ''
end,
['gs'] = function()
vim.schedule(function()
if container.status:find 'Up' then
show_term('docker stop ' .. container.id, 'J')
else
show_term('docker start ' .. container.id, 'J')
end
vim.defer_fn(function() vim.cmd 'normal! \\<Leader>r' end, 1000)
end)
return ''
end,
['gr'] = function()
vim.schedule(function()
show_term('docker restart ' .. container.id, 'J')
vim.defer_fn(function() vim.cmd 'normal! \\<Leader>r' end, 1000)
end)
return ''
end,
},
}, container.name),
h.String({}, container.image),
h.Normal({}, container.id),
h[container.status:find 'Up' and 'DiagnosticOk' or 'DiagnosticError']({}, container.status),
h.Comment({}, container.ports),
})
end
end
return {
h['@markup.heading'](
{},
('## Containers%s%s'):format(
#state.filter > 0 and ' (filter: ' .. state.filter .. ')' or '',
ctx.props.loading and '...' or ''
)
),
'\n\n',
'Filter: [',
h.String({
on_change = function(e)
state.filter = e.text
ctx:update(state)
end,
}, state.filter),
']',
ctx.props.containers and '\n\n' or '',
h(Shared.Table, { cells = containers_table }),
}
end
-- _
-- / \ _ __ _ __
-- / _ \ | '_ \| '_ \
-- / ___ \| |_) | |_) |
-- /_/ \_\ .__/| .__/
-- |_| |_|
--- @param ctx morph.Ctx<any, { loading: boolean, containers: morph.examples.DockerContainer[], show_help: boolean }>
local function App(ctx)
--
-- Helper: refresh_containers:
local refresh_containers = vim.schedule_wrap(function()
local state = assert(ctx.state)
state.loading = true
ctx:update(state)
local cmd = { 'docker', 'ps', '--format', 'json', '--all' }
vim.system(cmd, { text = true }, function(out)
---@type morph.examples.DockerContainer[]
local containers = {}
local lines = vim
.iter(vim.split(out.stdout or '', '\n'))
:filter(function(l) return l ~= '' end)
:totable()
for _, line in ipairs(lines) do
local raw_container = vim.json.decode(line)
---@type morph.examples.DockerContainer
local container = {
id = raw_container.ID or '',
name = raw_container.Names or '',
image = raw_container.Image or '',
status = raw_container.Status or '',
ports = raw_container.Ports or '',
created = raw_container.CreatedAt or '',
raw = raw_container,
}
table.insert(containers, container)
end
table.sort(containers, function(a, b) return a.name < b.name end)
state.loading = false
state.containers = containers
ctx:update(state)
end)
end)
if ctx.phase == 'mount' then
-- Initialize state:
ctx.state = {
loading = false,
containers = {},
show_help = false,
}
refresh_containers()
end
local state = assert(ctx.state)
return h('text', {
-- Global maps:
nmap = {
['<Leader>r'] = function()
refresh_containers()
return ''
end,
['g?'] = function()
state.show_help = not state.show_help
ctx:update(state)
return ''
end,
},
}, {
h['@markup.heading']({}, '# Docker'),
'\n\n',
--
-- Help (if enabled)
--
state.show_help and {
h(Help, {
show_help = state.show_help,
}),
'\n\n',
},
--
-- List of containers
--
{
h(Containers, {
loading = state.loading,
containers = state.containers,
}),
},
})
end
--------------------------------------------------------------------------------
-- Buffer/Render
--------------------------------------------------------------------------------
vim.cmd.tabnew()
vim.bo.buftype = 'nofile'
vim.bo.bufhidden = 'wipe'
vim.bo.buflisted = false
Morph.new():mount(h(App))