snapai
Version:
AI-powered icon generation CLI for mobile app developers
60 lines (59 loc) • 2.43 kB
JavaScript
import { Command, Flags } from '@oclif/core';
import chalk from 'chalk';
import { ConfigService } from '../services/config.js';
import { ValidationService } from '../utils/validation.js';
export default class ConfigCommand extends Command {
static description = 'Manage SnapAI configuration';
static examples = [
'<%= config.bin %> <%= command.id %> --api-key sk-your-openai-key',
'<%= config.bin %> <%= command.id %> --show',
];
static flags = {
'api-key': Flags.string({
description: 'Set OpenAI API key',
}),
show: Flags.boolean({
description: 'Show current configuration',
}),
};
async run() {
const { flags } = await this.parse(ConfigCommand);
if (flags['api-key']) {
await this.setApiKey(flags['api-key']);
}
if (flags.show) {
await this.showConfig();
}
if (Object.keys(flags).length === 0) {
await this.showConfig();
}
}
async setApiKey(apiKey) {
const error = ValidationService.validateApiKey(apiKey);
if (error) {
this.error(chalk.red(error));
}
await ConfigService.set('openai_api_key', apiKey);
this.log(chalk.green('✅ OpenAI API key configured successfully!'));
this.log('');
this.log(chalk.dim('Built with ❤️ by \u001b]8;;https://codewithbeto.dev\u001b\\codewithbeto.dev\u001b]8;;\u001b\\ - Ship faster, contribute more, lead with confidence'));
}
async showConfig() {
const config = await ConfigService.getConfig();
this.log(chalk.bold('📋 Current Configuration:'));
this.log('');
if (config.openai_api_key) {
const maskedKey = `sk-...${config.openai_api_key.slice(-4)}`;
this.log(`🔑 OpenAI API Key: ${chalk.green(maskedKey)}`);
}
else {
this.log(`🔑 OpenAI API Key: ${chalk.red('Not configured')}`);
this.log(chalk.gray(' Set with: snapai config --api-key YOUR_KEY'));
}
if (config.default_output_path) {
this.log(`📁 Default Output: ${chalk.blue(config.default_output_path)}`);
}
this.log('');
this.log(chalk.dim('Built with ❤️ by \u001b]8;;https://codewithbeto.dev\u001b\\codewithbeto.dev\u001b]8;;\u001b\\ - Ship faster, contribute more, lead with confidence'));
}
}