gemini-cost-tracker
Version:
CLI tool to display token usage and costs for Gemini and Vertex AI
95 lines ⢠3.94 kB
JavaScript
import chalk from 'chalk';
import inquirer from 'inquirer';
import { AppError, ErrorCode } from '../types/index.js';
import { AuthManager } from '../services/auth/authManager.js';
export async function configCommand(options) {
try {
const authManager = new AuthManager();
await authManager.initialize();
// Show current configuration
if (options.show) {
const config = await authManager.getConfiguration();
console.log(chalk.blue('š Current Configuration:'));
console.log(`Gemini API Key: ${config.geminiApiKey ? maskApiKey(config.geminiApiKey) : chalk.red('Not set')}`);
console.log(`GCP Project ID: ${config.gcpProjectId || chalk.red('Not set')}`);
console.log(`GCP Key File: ${config.gcpKeyFile || chalk.red('Not set')}`);
return;
}
// Set configuration values
if (options.setGeminiKey) {
await authManager.setGeminiApiKey(options.setGeminiKey);
console.log(chalk.green('ā
Gemini API key updated successfully'));
return;
}
if (options.setProject) {
await authManager.setGcpProjectId(options.setProject);
console.log(chalk.green('ā
GCP Project ID updated successfully'));
return;
}
if (options.setKeyFile) {
await authManager.setGcpKeyFile(options.setKeyFile);
console.log(chalk.green('ā
GCP Key file path updated successfully'));
return;
}
// Interactive configuration setup
console.log(chalk.blue('š§ Interactive Configuration Setup'));
console.log('This will guide you through setting up authentication for Gemini and Vertex AI.\n');
const answers = await inquirer.prompt([
{
type: 'input',
name: 'geminiApiKey',
message: 'Enter your Gemini API key:',
validate: (input) => {
if (!input.trim()) {
return 'Gemini API key is required';
}
return true;
},
},
{
type: 'input',
name: 'gcpProjectId',
message: 'Enter your Google Cloud Project ID:',
validate: (input) => {
if (!input.trim()) {
return 'GCP Project ID is required';
}
return true;
},
},
{
type: 'input',
name: 'gcpKeyFile',
message: 'Enter path to your GCP service account key file (optional):',
},
]);
// Save configuration
await authManager.setGeminiApiKey(answers.geminiApiKey);
await authManager.setGcpProjectId(answers.gcpProjectId);
if (answers.gcpKeyFile?.trim()) {
await authManager.setGcpKeyFile(answers.gcpKeyFile);
}
console.log(chalk.green('\nā
Configuration saved successfully!'));
// Test the configuration
console.log(chalk.yellow('š Testing configuration...'));
const isValid = await authManager.validateCredentials();
if (isValid) {
console.log(chalk.green('ā
Authentication test passed!'));
}
else {
console.log(chalk.red('ā Authentication test failed. Please check your credentials.'));
}
}
catch (error) {
if (error instanceof AppError) {
throw error;
}
throw new AppError(ErrorCode.CONFIG_COMMAND_ERROR, `Failed to manage configuration: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
function maskApiKey(key) {
if (key.length < 8)
return '***';
return key.substring(0, 4) + '...' + key.substring(key.length - 4);
}
//# sourceMappingURL=config.js.map