import {
dockerContainerRows,
dockerImageRows,
dockerVolumeRows,
dockerNetworkRows,
registryRepoRows,
registryTagRows,
registryTokenRows,
} from "../format.js";
import { t } from "../i18n.js";
document.addEventListener("alpine:init", () => {
Alpine.data("docker", () => ({
get store() {
return Alpine.store("app");
},
get containers() {
return dockerContainerRows(this.store.snap?.docker_containers, this.store.search);
},
get images() {
const rows = dockerImageRows(this.store.snap?.docker_images, this.store.search);
return this.store.onlyUsedImages ? rows.filter((row) => row.inUse) : rows;
},
get volumes() {
const rows = dockerVolumeRows(this.store.snap?.docker_volumes, this.store.search);
return this.store.onlyUsedVolumes ? rows.filter((row) => row.inUse) : rows;
},
get networks() {
const rows = dockerNetworkRows(this.store.snap?.docker_networks, this.store.search);
return this.store.onlyUsedNetworks ? rows.filter((row) => row.inUse) : rows;
},
get repos() {
return registryRepoRows(this.store.snap?.registry_repos, this.store.search);
},
get tags() {
return registryTagRows(this.store.registryTags);
},
get tokens() {
return registryTokenRows(this.store.registryTokens);
},
get registryHost() {
const registryStatus = this.store.snap?.registry_status;
if (!registryStatus) return "127.0.0.1:5100";
return registryStatus.domain && registryStatus.domain.trim() ? registryStatus.domain : `127.0.0.1:${registryStatus.port}`;
},
get registryStatusLabel() {
const registryStatus = this.store.snap?.registry_status;
if (!registryStatus) return "desabilitado";
return registryStatus.enabled ? t`ativo em ${this.registryHost}` : "desabilitado";
},
retentionEnabled: false,
retentionKeepLast: "10",
retentionLoaded: false,
syncRetention() {
const retention = this.store.snap?.registry_status?.retention;
if (!retention || this.retentionLoaded) return;
this.retentionEnabled = !!retention.enabled;
this.retentionKeepLast = String(retention.keep_last ?? 10);
this.retentionLoaded = true;
},
async saveRetention() {
const keepLast = Math.min(1000, Math.floor(Number(this.retentionKeepLast)));
if (!Number.isFinite(keepLast) || keepLast < 1) {
this.store.toastWarn(t("informe quantas tags manter (mínimo 1)"));
return;
}
await this.store.registryRetentionSet({ enabled: this.retentionEnabled, keep_last: keepLast });
},
showTokenModal: false,
newRegistryTokenStep: "form", newRegistryTokenName: "",
newRegistryTokenScope: "pull",
newRegistryTokenError: "",
newRegistryTokenLoginCommand: "",
openTokenModal() {
this.showTokenModal = true;
this.newRegistryTokenStep = "form";
this.newRegistryTokenName = "";
this.newRegistryTokenScope = "pull";
this.newRegistryTokenError = "";
this.newRegistryTokenLoginCommand = "";
},
closeTokenModal() {
this.showTokenModal = false;
},
async newRegistryTokenCreate() {
if (!this.newRegistryTokenName.trim()) {
this.newRegistryTokenError = t("nome obrigatório");
return;
}
this.newRegistryTokenError = "";
const registryCreateTokenResult = await this.store.registryCreateToken(this.newRegistryTokenName.trim(), this.newRegistryTokenScope);
if (!registryCreateTokenResult.ok) {
this.newRegistryTokenError = registryCreateTokenResult.error;
return;
}
this.newRegistryTokenLoginCommand = `docker login ${this.registryHost} -u ${this.newRegistryTokenName.trim()} -p ${registryCreateTokenResult.secret}`;
const tokenName = this.newRegistryTokenName.trim();
const safeHost = String(this.registryHost).replace(/[^A-Za-z0-9.-]+/g, "_");
const day = new Date().toISOString().slice(0, 10).replaceAll("-", "");
this.store.downloadTextFile(
`rustploy-registry-token-${tokenName}-${safeHost}-${day}.txt`,
`# Rustploy — token do registry (${this.newRegistryTokenScope})\n# Guarde em um cofre: o segredo não é mostrado de novo.\nregistry: ${this.registryHost}\nusuário: ${tokenName}\ntoken: ${registryCreateTokenResult.secret}\n\n${this.newRegistryTokenLoginCommand}\n`,
);
this.newRegistryTokenStep = "reveal";
},
}));
});