// image-ref-utils.ts — 容器镜像引用解析与拼接 // Logan.Li export interface ImageRefParts { repository: string; tag: string; /** digest 形式时存在,如 sha256:abc... // Logan.Li */ digest?: string; } /** 解析镜像引用为仓库地址 + tag(兼容 registry:port 与 digest)// Logan.Li */ export function parseImageRef(image: string): ImageRefParts { const raw = (image || '').trim(); if (!raw) return { repository: '', tag: 'latest' }; if (raw.includes('@')) { const at = raw.lastIndexOf('@'); return { repository: raw.slice(0, at), tag: raw.slice(at + 1), digest: raw.slice(at + 1), }; } const slashIdx = raw.lastIndexOf('/'); const colonIdx = raw.lastIndexOf(':'); if (colonIdx > slashIdx) { return { repository: raw.slice(0, colonIdx), tag: raw.slice(colonIdx + 1) || 'latest', }; } return { repository: raw, tag: 'latest' }; } /** 拼接完整镜像引用 // Logan.Li */ export function joinImageRef(repository: string, tag: string): string { const repo = repository.trim(); const t = tag.trim(); if (!repo) return ''; if (!t) return repo; if (t.startsWith('sha256:')) return `${repo}@${t}`; return `${repo}:${t}`; }