Files
cli/src/command/docker.ts
2026-01-15 10:48:41 +08:00

104 lines
3.0 KiB
TypeScript

import { program, Command } from '@/program.ts';
import { chalk } from '../module/chalk.ts';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { useKey } from '@kevisual/use-config';
import os from 'node:os'
import fs from 'node:fs';
import { select } from '@inquirer/prompts';
// 执行 docker指令
// docker login --username=${DOCKER_USERNAME} ${DOCKER_REGISTRY} --password ${DOCKER_PASSWORD}
// docker login -u cnb docker.cnb.cool --password ${CNB_TOKEN}
// helm registry login -u cnb helm.cnb.cool --password ${CNB_TOKEN}
const dockerCommand = new Command('docker')
.description('Docker 相关指令')
.action(async () => {
console.log(chalk.green('Docker command executed'));
});
const login = new Command('login')
.description('登录 Docker 镜像仓库')
.option('-r , --registry <registry>', 'Docker 镜像仓库地址', 'default')
.action(async (options) => {
const { registry = 'default' } = options;
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}`));
}
});
});
dockerCommand.addCommand(login);
const helmLogin = new Command('helm')
.description('Helm 登录镜像仓库')
.action(async () => {
});
const helmLoginCommand = new Command('login')
.description('登录 Helm 镜像仓库')
.action(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}`));
}
});
});
helmLogin.addCommand(helmLoginCommand);
program.addCommand(dockerCommand);
program.addCommand(helmLogin);