niledatabase
Version:
Command line interface for Nile databases
101 lines (98 loc) • 4.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConnectCommand = createConnectCommand;
const commander_1 = require("commander");
const auth_1 = require("../lib/auth");
const colors_1 = require("../lib/colors");
const globalOptions_1 = require("../lib/globalOptions");
const config_1 = require("../lib/config");
function createConnectCommand(getOptions) {
const connect = new commander_1.Command('connect')
.description('Connect to Nile')
.addHelpText('after', `
Examples:
# Authentication
${(0, colors_1.formatCommand)('nile connect login')} Login to Nile using browser-based authentication
${(0, colors_1.formatCommand)('nile connect status')} Check connection status
${(0, colors_1.formatCommand)('nile connect logout')} Logout and clear stored credentials
# Alternative authentication methods
${(0, colors_1.formatCommand)('nile --api-key YOUR_API_KEY db list')} Use API key directly in commands
${(0, colors_1.formatCommand)('nile config --api-key YOUR_API_KEY')} Save API key in configuration
${(0, globalOptions_1.getGlobalOptionsHelp)()}`);
connect
.command('login')
.description('Connect to Nile using browser-based authentication')
.option('--client-id <id>', 'OAuth client ID', 'nilecli')
.action(async () => {
try {
const globalOptions = getOptions();
const configManager = new config_1.ConfigManager(globalOptions);
// First try to get token from existing methods
const existingToken = await configManager.getToken();
if (existingToken) {
console.log(colors_1.theme.success('Already connected to Nile!'));
return;
}
// If no existing token, start browser-based auth
console.log(colors_1.theme.info('Starting browser-based authentication...'));
if (configManager.getDebug()) {
console.log('Debug - Auth URL:', configManager.getAuthUrl());
}
const token = await auth_1.Auth.getAuthorizationToken(configManager);
if (token) {
configManager.setToken(token);
console.log(colors_1.theme.success('\nSuccessfully connected to Nile!'));
}
else {
console.error(colors_1.theme.error('Failed to connect to Nile'));
process.exit(1);
}
}
catch (error) {
console.error(colors_1.theme.error('Failed to connect:'), error);
process.exit(1);
}
});
connect
.command('status')
.description('Check connection status')
.action(async () => {
try {
const globalOptions = getOptions();
const configManager = new config_1.ConfigManager(globalOptions);
const token = await configManager.getToken();
if (token) {
if (globalOptions.apiKey) {
console.log(colors_1.theme.success('Connected to Nile using API key'));
}
else {
console.log(colors_1.theme.success('Connected to Nile'));
}
}
else {
console.log(colors_1.theme.warning('Not connected to Nile'));
console.log(colors_1.theme.secondary('Run "nile connect" to connect'));
}
}
catch (error) {
console.error(colors_1.theme.error('Failed to check status:'), error);
process.exit(1);
}
});
connect
.command('logout')
.description('Clear stored credentials')
.action(async () => {
try {
const globalOptions = getOptions();
const configManager = new config_1.ConfigManager(globalOptions);
configManager.removeToken();
console.log(colors_1.theme.success('Successfully logged out'));
}
catch (error) {
console.error(colors_1.theme.error('Failed to logout:'), error);
process.exit(1);
}
});
return connect;
}