93 lines
2.3 KiB
TypeScript
93 lines
2.3 KiB
TypeScript
import { app } from '../app.ts';
|
|
import { z } from 'zod';
|
|
import { chalk } from '@/module/chalk.ts';
|
|
import { spawn } from 'node:child_process';
|
|
import { useKey } from '@kevisual/use-config';
|
|
|
|
app.route({
|
|
path: 'docker',
|
|
key: 'login',
|
|
description: '登录 Docker 镜像仓库',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {
|
|
registry: z.string().optional().describe('Docker 镜像仓库地址'),
|
|
}
|
|
}
|
|
}).define(async (ctx) => {
|
|
const registry = ctx.args.registry || 'default';
|
|
let DOCKER_USERNAME = useKey('DOCKER_USERNAME') as string;
|
|
let DOCKER_PASSWORD = useKey('DOCKER_PASSWORD') as string;
|
|
let DOCKER_REGISTRY = useKey('DOCKER_REGISTRY') as string;
|
|
|
|
if (registry !== 'default') {
|
|
DOCKER_USERNAME = 'cnb';
|
|
DOCKER_PASSWORD = useKey('CNB_TOKEN') as string;
|
|
DOCKER_REGISTRY = 'docker.cnb.cool';
|
|
}
|
|
if (!DOCKER_USERNAME || !DOCKER_PASSWORD) {
|
|
console.log(chalk.red('请先配置 DOCKER_USERNAME 和 DOCKER_PASSWORD'));
|
|
return;
|
|
}
|
|
const loginProcess = spawn('docker', [
|
|
'login',
|
|
'--username',
|
|
DOCKER_USERNAME,
|
|
DOCKER_REGISTRY,
|
|
'--password-stdin'
|
|
], {
|
|
stdio: ['pipe', 'inherit', 'inherit']
|
|
});
|
|
|
|
loginProcess.stdin.write(DOCKER_PASSWORD + '\n');
|
|
loginProcess.stdin.end();
|
|
|
|
loginProcess.on('close', (code) => {
|
|
if (code === 0) {
|
|
console.log(chalk.green('登录成功'));
|
|
} else {
|
|
console.log(chalk.red(`登录失败,退出码:${code}`));
|
|
}
|
|
});
|
|
}).addTo(app)
|
|
|
|
app.route({
|
|
path: 'helm',
|
|
key: 'login',
|
|
description: '登录 Helm 镜像仓库',
|
|
metadata: {
|
|
middleware: ['auth'],
|
|
args: {}
|
|
}
|
|
}).define(async () => {
|
|
let DOCKER_USERNAME = 'cnb';
|
|
let DOCKER_PASSWORD = useKey('CNB_TOKEN') as string;
|
|
|
|
if (!DOCKER_PASSWORD) {
|
|
console.log(chalk.red('请先配置 CNB_TOKEN'));
|
|
return;
|
|
}
|
|
|
|
const helmLoginProcess = spawn('helm', [
|
|
'registry',
|
|
'login',
|
|
'--username',
|
|
DOCKER_USERNAME,
|
|
'--password-stdin',
|
|
'helm.cnb.cool'
|
|
], {
|
|
stdio: ['pipe', 'inherit', 'inherit']
|
|
});
|
|
|
|
helmLoginProcess.stdin.write(DOCKER_PASSWORD + '\n');
|
|
helmLoginProcess.stdin.end();
|
|
|
|
helmLoginProcess.on('close', (code) => {
|
|
if (code === 0) {
|
|
console.log(chalk.green('Helm 登录成功'));
|
|
} else {
|
|
console.log(chalk.red(`Helm 登录失败,退出码:${code}`));
|
|
}
|
|
});
|
|
}).addTo(app)
|