run method
Runs this command.
The return value is wrapped in a Future if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
Future<int> run() async {
final apiKey = argResults?['api-key'] as String?;
if (apiKey == null || apiKey.isEmpty) {
logger.err('Please provide an API key');
logger.info('');
logger.info('Usage: pubmind config set --api-key sk-...');
return ExitCode.usage.code;
}
if (!apiKey.startsWith('sk-')) {
logger.err('Invalid API key format. Key should start with sk-');
return ExitCode.usage.code;
}
try {
final configDir = Directory(path.dirname(_configPath));
if (!configDir.existsSync()) {
configDir.createSync(recursive: true);
}
final config = {'openai_api_key': apiKey};
File(_configPath).writeAsStringSync(
jsonEncode(config),
mode: FileMode.write,
);
logger.success('API key saved successfully!');
logger.info('');
logger.info('Config saved to: ${darkGray.wrap(_configPath)}');
return ExitCode.success.code;
} catch (e) {
logger.err('Failed to save configuration: $e');
return ExitCode.software.code;
}
}