niledatabase
Version:
Command line interface for Nile databases
185 lines (183 loc) • 7.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWorkspaceCommand = createWorkspaceCommand;
const commander_1 = require("commander");
const config_1 = require("../lib/config");
const api_1 = require("../lib/api");
const colors_1 = require("../lib/colors");
const globalOptions_1 = require("../lib/globalOptions");
const errorHandling_1 = require("../lib/errorHandling");
const cli_table3_1 = __importDefault(require("cli-table3"));
const axios_1 = __importDefault(require("axios"));
function createWorkspaceCommand(getOptions) {
const workspace = new commander_1.Command('workspace')
.description('Manage workspaces')
.addHelpText('after', `
Examples:
${(0, colors_1.formatCommand)('nile workspace list')} List all workspaces
${(0, colors_1.formatCommand)('nile workspace show')} Show current workspace
${(0, colors_1.formatCommand)('nile workspace select <name>')} Select a workspace
${(0, colors_1.formatCommand)('nile workspace create --name "My Workspace"')} Create a new workspace
${(0, colors_1.formatCommand)('nile workspace delete <name>')} Delete a workspace
${(0, colors_1.formatCommand)('nile workspace update <name>')} Update workspace settings
${(0, globalOptions_1.getGlobalOptionsHelp)()}`);
workspace
.command('list')
.description('List all workspaces')
.action(async () => {
try {
const options = getOptions();
const configManager = new config_1.ConfigManager(options);
let token = configManager.getToken();
if (!token) {
await (0, errorHandling_1.forceRelogin)(configManager);
// After re-login, get the new token
token = configManager.getToken();
if (!token) {
throw new Error('Failed to get token after re-login');
}
}
const api = new api_1.NileAPI({
token,
dbHost: configManager.getDbHost(),
controlPlaneUrl: configManager.getGlobalHost(),
debug: options.debug
});
const workspaces = await api.listWorkspaces();
if (options.format === 'json') {
console.log(JSON.stringify(workspaces, null, 2));
return;
}
if (options.format === 'csv') {
console.log('NAME,SLUG');
workspaces.forEach(w => {
console.log(`${w.name},${w.slug}`);
});
return;
}
if (workspaces.length === 0) {
console.log(colors_1.theme.warning('\nNo workspaces found.'));
console.log(colors_1.theme.secondary('Run "nile workspace create" to create a workspace'));
return;
}
console.log('\nWorkspaces:');
const table = new cli_table3_1.default({
head: [
colors_1.theme.header('NAME'),
colors_1.theme.header('SLUG')
],
style: { head: [], border: [] },
chars: {
'top': '─',
'top-mid': '┬',
'top-left': '┌',
'top-right': '┐',
'bottom': '─',
'bottom-mid': '┴',
'bottom-left': '└',
'bottom-right': '┘',
'left': '│',
'left-mid': '├',
'mid': '─',
'mid-mid': '┼',
'right': '│',
'right-mid': '┤',
'middle': '│'
}
});
workspaces.forEach(w => {
table.push([
colors_1.theme.primary(w.name),
colors_1.theme.info(w.slug)
]);
});
console.log(table.toString());
}
catch (error) {
if (axios_1.default.isAxiosError(error) && (error.response?.status === 401 || error.message === 'Token is required')) {
await (0, errorHandling_1.handleApiError)(error, 'list workspaces', new config_1.ConfigManager(getOptions()));
}
else {
throw error;
}
}
});
workspace
.command('show')
.description('Show current workspace')
.action(async () => {
try {
const options = getOptions();
const configManager = new config_1.ConfigManager(options);
const workspaceSlug = configManager.getWorkspace();
if (!workspaceSlug) {
console.log(colors_1.theme.warning('No workspace selected'));
console.log(colors_1.theme.secondary('Run "nile config --workspace <n>" to set a workspace'));
return;
}
let token = configManager.getToken();
if (!token) {
await (0, errorHandling_1.forceRelogin)(configManager);
token = configManager.getToken();
if (!token) {
throw new Error('Failed to get token after re-login');
}
}
// Get workspace details from API
const api = new api_1.NileAPI({
token,
dbHost: configManager.getDbHost(),
controlPlaneUrl: configManager.getGlobalHost(),
debug: options.debug
});
const workspace = await api.getWorkspace(workspaceSlug);
if (options.format === 'json') {
console.log(JSON.stringify(workspace, null, 2));
return;
}
if (options.format === 'csv') {
console.log('NAME,SLUG');
console.log(`${workspace.name},${workspace.slug}`);
return;
}
console.log(colors_1.theme.primary('\nCurrent workspace:'));
const detailsTable = new cli_table3_1.default({
style: {
head: [],
border: [],
},
chars: {
'top': '─',
'top-mid': '┬',
'top-left': '┌',
'top-right': '┐',
'bottom': '─',
'bottom-mid': '┴',
'bottom-left': '└',
'bottom-right': '┘',
'left': '│',
'left-mid': '├',
'mid': '─',
'mid-mid': '┼',
'right': '│',
'right-mid': '┤',
'middle': '│'
}
});
detailsTable.push([colors_1.theme.secondary('Name:'), colors_1.theme.primary(workspace.name)], [colors_1.theme.secondary('Slug:'), colors_1.theme.info(workspace.slug)]);
console.log(detailsTable.toString());
}
catch (error) {
if (axios_1.default.isAxiosError(error) && (error.response?.status === 401 || error.message === 'Token is required')) {
await (0, errorHandling_1.handleApiError)(error, 'get workspace details', new config_1.ConfigManager(getOptions()));
}
else {
throw error;
}
}
});
return workspace;
}