niledatabase
Version:
Command line interface for Nile databases
106 lines (105 loc) • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.configCommand = configCommand;
const commander_1 = require("commander");
const config_1 = require("../lib/config");
const globalOptions_1 = require("../lib/globalOptions");
function configCommand() {
const command = new commander_1.Command('config');
command.description('Manage Nile CLI configuration');
command
.addOption(new commander_1.Option('--api-key <key>', 'Set the API key')
.env('NILE_API_KEY'))
.action((options, cmd) => {
const parent = cmd.parent;
if (!parent) {
throw new Error('Parent command not found');
}
const globalOpts = (0, globalOptions_1.getGlobalOptions)(parent);
const debug = globalOpts.debug || false;
if (debug) {
console.log('Debug - Options:', options);
console.log('Debug - Global options:', globalOpts);
}
const configManager = new config_1.ConfigManager(globalOpts);
let configChanged = false;
// Handle each config option
if (globalOpts.apiKey) {
if (debug) {
console.log('Debug - Setting API key:', globalOpts.apiKey);
}
configManager.setApiKey(globalOpts.apiKey);
configChanged = true;
}
if (globalOpts.workspace) {
if (debug) {
console.log('Debug - Setting workspace:', globalOpts.workspace);
}
configManager.setWorkspace(globalOpts.workspace);
configChanged = true;
}
if (globalOpts.dbHost) {
if (debug) {
console.log('Debug - Setting database host:', globalOpts.dbHost);
}
configManager.setDbHost(globalOpts.dbHost);
configChanged = true;
}
if (globalOpts.globalHost) {
if (debug) {
console.log('Debug - Setting global host:', globalOpts.globalHost);
}
configManager.setGlobalHost(globalOpts.globalHost);
configChanged = true;
}
if (globalOpts.authUrl) {
if (debug) {
console.log('Debug - Setting auth URL:', globalOpts.authUrl);
}
configManager.setAuthUrl(globalOpts.authUrl);
configChanged = true;
}
if (globalOpts.db) {
if (debug) {
console.log('Debug - Setting database:', globalOpts.db);
}
configManager.setDatabase(globalOpts.db);
configChanged = true;
}
// Show the current/updated configuration
const currentConfig = configManager.getAllConfig();
if (configChanged) {
console.log('Updated configuration:');
}
else {
console.log('Current configuration:');
}
console.log(JSON.stringify(currentConfig, null, 2));
});
command
.command('reset')
.description('Reset configuration to defaults')
.action((options, cmd) => {
const parent = cmd.parent;
if (!parent) {
throw new Error('Parent command not found');
}
const globalOpts = (0, globalOptions_1.getGlobalOptions)(parent);
const configManager = new config_1.ConfigManager(globalOpts);
configManager.resetConfig();
console.log('Configuration has been reset to defaults.');
});
command.addHelpText('after', `
Examples:
$ nile config Show current configuration
$ nile config --api-key <key> Set API key
$ nile config --workspace <name> Set default workspace
$ nile config --db <name> Set default database
$ nile config --db-host db.example.com Set database host
$ nile config --global-host global.example.com Set global host
$ nile config --auth-url auth.example.com Set authentication URL
$ nile config --api-key <key> --workspace dev Set multiple configurations
$ nile config reset Reset configuration to defaults
`);
return command;
}