rustploy-gui 0.1.2

Rustploy — desktop client (glacier-ui) for the Rustploy PaaS daemon
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
--!strict
--!nolint FunctionUnused
-- handlers/projects.luau — grade de projetos: criar/editar/remover projeto,
-- variáveis de ambiente de projeto, ações de serviço a partir do card do grid
-- (parar/remover), e o "Stop All" de todo o cluster (topbar).

local State = require("../state")
local Helpers = require("../helpers")
local fmt = require("../fmt")
local Stream = require("stream")
local Registry = require("registry")

-- Abrir a lista de serviços de um projeto (grade read-only via snapshot).
function open_project(id: string): ()
    ctx.selected_project_id = id
    ctx.view = "project_services"
    ctx.proj_tab = "services"
    ctx.editing_project = "false"
    ctx.proj_action_msg = ""
    ctx.penv_text_open = "false"
    ctx.proj_loading = "true"
    Stream.update_open_project()
end

-- ── Criar projeto (agora em uma JANELA à parte) ──────────────────────────

-- Abre a janela de cadastro de projeto (motor Glacier próprio, isolado). Passa a
-- conexão atual (api_url/api_token) via `data` para que a janela chame
-- ProjectCreate por conta própria; ao concluir, ela emite broadcast(
-- "project_created"), recebido por `on_broadcast` abaixo. Ver
-- views/new_project_form.gvb + scripts/new_project_window.luau.
function open_new_project_window(): ()
    open_window{
        component = "new_project", size = "460 340", decorations = false,
        resizable = false,  -- diálogo fixo (borderless, sem alças de resize)
        data = {
            api_url = ctx.api_url or "",
            api_token = ctx.api_token or "",
        },
    }
end

-- Recebe mensagens de OUTRAS janelas (glacier broadcast). Em ambos os casos
-- força um refresh imediato do snapshot (o card novo aparece na hora, sem
-- esperar o próximo tick do SSE) e confirma num toast:
--   "project_created" — a janela de cadastro de projeto concluiu.
--   "service_created" — a janela do wizard de serviço concluiu (a grade do
--                       projeto aberto é reconstruída pelo update_open_project).
function on_broadcast(event: string, payload: any): ()
    local name = (type(payload) == "table" and payload.name) or ""
    if event == "project_created" then
        toast{
            message = tostring(
                "Projeto \"" .. (name ~= "" and name or "projeto") .. "\" criado"
            ),
            kind = "success"
        }
        Stream.refresh_now()
    elseif event == "service_created" then
        toast{
            message = tostring(
                "Serviço \"" .. (name ~= "" and name or "serviço") .. "\" criado"
            ),
            kind = "success"
        }
        Stream.refresh_now()
    elseif event == "job_created" then
        toast{
            message = tostring(
                "Job \"" .. (name ~= "" and name or "job") .. "\" criado"
            ),
            kind = "success"
        }
        Stream.refresh_now()
    elseif event == "job_updated" then
        toast{
            message = tostring(
                "Job \"" .. (name ~= "" and name or "job") .. "\" salvo"
            ),
            kind = "success"
        }
        Stream.refresh_now()
    elseif event == "registry_token_created" then
        toast{
            message = tostring(
                "Token \"" .. (name ~= "" and name or "token") .. "\" criado"
            ),
            kind = "success"
        }
        Registry.refresh_tokens()
    end
end

-- ── Criar / remover projeto ──────────────────────────────────────────────

-- NOTA: create_project (formulário inline, removido da UI em favor da janela
-- acima) é mantido por ser reutilizável — um fluxo de criação embutido poderia
-- voltar a chamá-lo. Hoje não há call-site na UI.
function create_project(): ()
    local api = State.api
    if not api then
        return
    end
    local name = Helpers.trim(ctx.new_proj_name)
    if name == "" then
        ctx.erro_new_proj_name = "informe um nome"
        return
    end
    ctx.erro_new_proj_name = ""
    local desc = Helpers.trim(ctx.new_proj_desc)
    local cmd = {
        ProjectCreate = {
            name = name,
            description = (desc ~= "" and desc or nil)
        }
    }
    ctx.new_proj_msg = "criando…"
    local ok, res = api:rpc_checked(cmd)
    if ok then
        ctx.new_proj_name = ""
        ctx.new_proj_desc = ""
        ctx.new_proj_msg = "projeto criado"
        toast{
            message = tostring("Projeto \"" .. name .. "\" criado"),
            kind = "success"
        }
        Stream.refresh_now()
    else
        ctx.new_proj_msg = "erro: " .. res
        toast{
            message = tostring(res),
            kind = "error"
        }
    end
end

function delete_project(id: string): ()
    if id == "" then
        return
    end
    local ok_confirm = confirm{
        title = "Remover projeto?",
        message = "O projeto será removido. Só é possível remover projetos sem serviços.",
        confirm_label = "Remover projeto",
        destructive = true,
    }

    if not ok_confirm then
        return
    end

    local api = State.api
    if not api or id == "" then
        return
    end
    ctx.proj_action_msg = "removendo…"
    local ok, res = api:rpc_checked{ ProjectDelete = { id = id } }
    if ok then
        ctx.selected_project_id = ""
        ctx.view = "projects"
        toast{ message = tostring("Projeto removido"), kind = "success" }
        Stream.refresh_now()
    else
        ctx.proj_action_msg = "erro: " .. res
        toast{ message = tostring(res), kind = "error" }
    end
end



-- ── Editar projeto (cabeçalho de project_services) ──────────────────────

function edit_project_toggle(): ()
    ctx.edit_proj_name = ctx.proj_name or ""
    ctx.edit_proj_desc = ctx.proj_description or ""
    ctx.erro_edit_proj_name = ""
    ctx.editing_project = "true"
end

function cancel_project_edit(): ()
    ctx.editing_project = "false"
end

-- on_validation_error do <form name="edit_project"> — o motor já publicou
-- {erro_edit_proj_name} e acendeu o :invalid; aqui é só o aviso.
function proj_edit_apontar(_erros_json: string?): ()
    toast{ message = "Informe o nome do projeto", kind = "warning" }
end

-- on_submit: só roda com a validação (rules="required" no NOME) aprovada.
function save_project_edit(): ()
    local pid = ctx.selected_project_id or ""
    local api = State.api
    if pid == "" or not api then
        return
    end
    local name = Helpers.trim(ctx.edit_proj_name)
    local desc = Helpers.trim(ctx.edit_proj_desc)
    local ok, res = api:rpc_checked{
        ProjectUpdate = {
            id = pid,
            name = name,
            description = (desc ~= "" and desc or nil)
        }
    }
    if ok then
        ctx.editing_project = "false"
        ctx.proj_action_msg = "projeto atualizado"
        ctx.proj_name = name
        ctx.proj_description = desc
    else
        ctx.proj_action_msg = "erro: " .. res
    end
end

-- ── Variáveis de ambiente do projeto (aba "Variáveis") ──────────────────
-- Fluxo idêntico ao do serviço: env_vars + env_comments (`# ...` ancorados por
-- key). O `mutate` recebe uma tabela `pe = { env_vars, env_comments }` e a
-- altera in place; enviamos as duas listas no ProjectEnvSet.

local function with_project_env(mutate: (pe: any) -> ()): ()
    local pid = ctx.selected_project_id or ""
    local api = State.api
    if pid == "" or not api then
        return
    end
    local ok, res = api:rpc("ProjectList")
    if not ok or not res.Projects then
        return
    end
    local proj
    for _, p in ipairs(res.Projects) do
        if p.id == pid then
            proj = p;
            break
        end
    end
    if proj == nil then
        return
    end
    local pe = {
        env_vars = proj.env_vars or json.array{},
        env_comments = proj.env_comments or json.array{},
    }
    mutate(pe)
    local ok2, res2 = api:rpc_checked{
        ProjectEnvSet = {
            project_id = pid,
            env_vars = pe.env_vars,
            env_comments = pe.env_comments,
        }
    }
    if ok2 then
        ctx.proj_env = fmt.env_json_with_comments(pe.env_vars, pe.env_comments)
        ctx.proj_env_count = tostring(#pe.env_vars)
        -- Não sobrescrever o textarea enquanto o usuário edita (ver stream.luau).
        if ctx.penv_text_open ~= "true" then
            local dotenv = fmt.env_dotenv_with_comments(pe.env_vars, pe.env_comments)
            ctx.proj_env_text = dotenv
            ctx.proj_env_text_orig = dotenv
        end
    else
        toast{ message = tostring(res2), kind = "error" }
    end
end

-- Parser .env (KEY=VALUE, # comentário, <secret:nome>) → (env_vars, comments).
-- Comentários pendentes ancoram na próxima var real (before_key); os do fim
-- ficam sem âncora (before_key = nil).
local function parse_project_dotenv(text: string?): ({any}, {any})
    local vars = json.array{}
    local comments = json.array{}
    local pending = {}
    for line in string.gmatch((text or "") .. "\n", "([^\n]*)\n") do
        local l = Helpers.trim(line)
        if l == "" then
            -- ignora
        elseif l:sub(1, 1) == "#" then
            table.insert(pending, l)
        else
            local k, v = l:match("^([^=]+)=(.*)$")
            if k and v then
                local key = Helpers.trim(k)
                if key ~= "" then
                    for _, c in ipairs(pending) do
                        table.insert(comments, { text = c, before_key = key })
                    end
                    pending = {}
                    v = Helpers.trim(v)
                    local secret = Helpers.parse_secret_ref(v)
                    local value = secret and { Secret = secret } or { Plain = v }
                    table.insert(vars, { key = key, value = value })
                end
            end
        end
    end
    for _, c in ipairs(pending) do
        table.insert(comments, { text = c, before_key = nil })
    end
    return vars, comments
end

-- Reordena vars/comments a partir de uma ordem de keys (vars + linhas
-- `__c<idx>`): cada comentário reancora na primeira var real que o segue.
local function reorder_project_env(
    vars: {any},
    comments: {any}, keys: {any}
): ({any}, {any})
    local var_keys = {}
    local by_key = {}
    for _, v in ipairs(vars) do var_keys[v.key] = true; by_key[v.key] = v end
    local seen = {}
    local new_vars = json.array{}
    local new_comments = json.array{}
    for pos, k in ipairs(keys) do
        local ci = k:match("^__c(%d+)$")
        if ci then
            local idx = (tonumber(ci) or 0) + 1 -- __c é 0-based
            local c = comments[idx]
            if c then
                seen[idx] = true
                local anchor = nil
                for j = pos + 1, #keys do
                    if var_keys[keys[j]] then anchor = keys[j]; break end
                end
                table.insert(new_comments, { text = c.text, before_key = anchor })
            end
        elseif by_key[k] then
            table.insert(new_vars, by_key[k])
            by_key[k] = nil
        end
    end
    for _, v in ipairs(vars) do
        if by_key[v.key] then
            table.insert(new_vars, v)
        end
    end
    for idx, c in ipairs(comments) do
        if not seen[idx] then
            table.insert(new_comments, c)
        end
    end
    return new_vars, new_comments
end

-- on_validation_error do <form name="penv_add"> (a chave é obrigatória via
-- rules="required"). O erro do modo-secret abaixo continua no handler porque é
-- condicional (não dá pra declarar no <form>).
function penv_apontar(_erros_json: string?): ()
    toast{ message = "Informe a chave (KEY)", kind = "warning" }
end

function penv_add(): ()
    -- rules="required" no <form> garante penv_new_key preenchido aqui.
    local key = Helpers.trim(ctx.penv_new_key)
    ctx.erro_penv_new_key = ""
    local val = ctx.penv_new_val or ""
    -- Duas formas de virar referência de secret (a que o daemon decifra no
    -- deploy, deploy/env_resolve.rs):
    --   • toggle "usar secret" ligado (ver handlers/secrets.luau) — o campo
    --     valor carrega o NOME puro do secret, não o conteúdo;
    --   • `<secret:NOME>`/`secret:NOME` digitado no valor, como no editor .env.
    local secret_name: string? = Helpers.parse_secret_ref(val)
    if ctx.penv_new_is_secret == "true" and secret_name == nil then
        -- Modo secret: o valor JÁ é o nome (o chip preenche assim).
        local name = Helpers.trim(val)
        if name == "" then
            ctx.erro_penv_new_key = "escolha um secret"
            return
        end
        secret_name = name
    end
    with_project_env(function(pe: any): ()
        local kept = json.array{}
        for _, v in ipairs(pe.env_vars) do
            if v.key ~= key then
                table.insert(kept, v)
            end
        end
        local value = secret_name and { Secret = secret_name } or { Plain = val }
        table.insert(kept, { key = key, value = value })
        pe.env_vars = kept
    end)
    ctx.penv_new_key = ""
    ctx.penv_new_val = ""
    ctx.penv_new_is_secret = "false"
end

function penv_del(key: string): ()
    with_project_env(function(pe: any): ()
        local kept = json.array{}
        for _, v in ipairs(pe.env_vars) do
            if v.key ~= key then
                table.insert(kept, v)
            end
        end
        pe.env_vars = kept
        local kc = json.array{}
        for _, c in ipairs(pe.env_comments) do
            if c.before_key ~= key then
                table.insert(kc, c)
            end
        end
        pe.env_comments = kc
    end)
end

function penv_reorder(v: string?): ()
    if not v then
        return
    end
    local ok, keys = pcall(json.decode, v)
    if not ok or type(keys) ~= "table" then
        return
    end
    with_project_env(function(pe: any): ()
        local nv, nc = reorder_project_env(pe.env_vars, pe.env_comments, keys)
        pe.env_vars = nv
        pe.env_comments = nc
    end)
end

-- ── Editor .env do projeto (mesmo fluxo do serviço) ─────────────────────

function penv_text_toggle(): ()
    ctx.penv_text_open = (ctx.penv_text_open == "true") and "false" or "true"
end

function penv_export(): ()
    -- Abre o editor .env já preenchido (o texto vem do snapshot em proj_env_text).
    ctx.penv_text_open = "true"
end

function penv_text_cancel(): ()
    ctx.proj_env_text = ctx.proj_env_text_orig or ""
    ctx.penv_text_open = "false"
end

function penv_import(): ()
    local vars, comments = parse_project_dotenv(ctx.proj_env_text)
    with_project_env(function(pe: any): ()
        pe.env_vars = vars
        pe.env_comments = comments
    end)
    ctx.penv_text_open = "false"
end

-- ── Ações de serviço a partir de um card do grid do projeto ─────────────
-- O grid se atualiza no próximo snapshot; refresh_now traz isso pra hora.

function svc_stop_id(id: string): ()
    local ok_confirm = confirm{
        title = "Parar serviço?",
        message = "O tráfego para este serviço será interrompido até um novo deploy.",
        confirm_label = "Parar",
        destructive = true,
    }

    if not ok_confirm then
        return
    end

    local api = State.api
    if not api then
        return
    end
    ctx.proj_action_msg = "enviando…"
    local ok, res = api:rpc_checked{ ServiceStop = { service_id = id } }
    ctx.proj_action_msg = ok and "serviço parado" or ("erro: " .. res)
    if not ok then
        toast{ message = tostring(res), kind = "error" }
    end
    Stream.refresh_now()
end


function stop_delete_service(id: string): ()
    -- Jobs de Schedules gatilhados por este serviço são removidos em cascata
    -- pelo daemon — avisa quais no confirm (jobs_by_id vem do snapshot).
    local dep_names = {}
    for _, job in State.jobs_by_id do
        if job.trigger_service_id == id then
            table.insert(dep_names, tostring(job.name))
        end
    end
    local message = "O serviço será parado e removido."
    if #dep_names > 0 then
        table.sort(dep_names)
        message = message
            .. " Os jobs de Schedules que dependem dele também serão removidos, com histórico: "
            .. table.concat(dep_names, ", ")
            .. "."
    end
    message = message .. " Essa ação não pode ser desfeita."
    local ok_confirm = confirm{
        title = "Parar e remover serviço?",
        message = message,
        confirm_label = "Remover",
        destructive = true,
    }

    if not ok_confirm then
        return
    end


    local api = State.api
    if not api then
        return
    end
    ctx.proj_action_msg = "removendo…"
    local ok, res = api:rpc_checked{ ServiceStop = { service_id = id } }
    if not ok then
        ctx.proj_action_msg = "erro ao parar: " .. res;
        toast{ message = tostring(res), kind = "error" }; return
    end
    local ok2, res2 = api:rpc_checked{ ServiceDelete = { id = id } }
    ctx.proj_action_msg = ok2 and "serviço parado e removido" or ("erro ao remover: " .. res2)
    if ok2 then
        toast{ message = tostring("Serviço removido"), kind = "success" }
    else
        toast{ message = tostring(res2), kind = "error" }
    end
    Stream.refresh_now()
end

-- ── Stop All (topbar) ────────────────────────────────────────────────────

function stop_all(): ()
    local ok_confirm = confirm{
        title = "Parar todos os serviços?",
        message = "Todos os serviços em execução serão parados agora. Você pode reiniciá-los depois com um novo deploy.",
        confirm_label = "Parar todos",
        destructive = true,
    }

    if not ok_confirm then
        return
    end

    local api = State.api
    if not api then
        return
    end
    ctx.status_line = "parando todos…"
    local ok, res = api:rpc_checked("StopAllManaged")
    if ok then
        local n = res.StopAllResult and res.StopAllResult.count or 0
        ctx.status_line = "parados: " .. tostring(n)
    else
        ctx.status_line = "erro: " .. res
    end
end