fsdb/cli.ts

156 lines
4.5 KiB
TypeScript

import { parseArgs } from '@std/cli/parse-args';
import meta from './deno.json' with { type: 'json' };
import * as fsdb from './fsdb.ts';
import * as path from '@std/path';
import * as fs from '@std/fs';
import sanitize from './utils/sanitize.ts';
type COMMAND = 'get' | 'create' | 'update' | 'delete' | 'find';
const settings = parseArgs(Deno.args, {
boolean: ['help', 'version'],
alias: {
help: 'h',
root: 'r',
version: 'v'
},
default: {
root: './.fsdb'
}
});
if (settings.help) {
console.log(`Usage:
fsdb
[--h(elp)]
[--v(ersion)]
[--r(oot) <directory, default: ./.fsdb>]
<collection, eg: users> <commmand get|create|update|delete|find> [input]
Examples:
fsdb items create '{ "id": "able-fish-door-tall-wait-dark-dark-nose-tall-very", "value": "test" }'
fsdb items get able-fish-door-tall-wait-dark-dark-nose-tall-very
Options:
-h, --help Show this help message
-v, --version Show the version number
-r, --root <dir> Set the root directory of the database
`);
Deno.exit(0);
}
if (settings.version) {
console.log(meta.version ?? 'unknown');
Deno.exit(0);
}
const collection_name: string | number | undefined = settings._.shift();
if (typeof collection_name !== 'string') {
console.error('You must specify a collection name.');
Deno.exit(1);
}
const command: COMMAND | undefined = settings._.shift() as COMMAND;
if (!command) {
console.error('You must specify a valid command.');
Deno.exit(1);
}
const DB_ROOT = typeof settings.root === 'string' ? path.resolve(settings.root) : './.fsdb';
const collection_info_path: string = path.resolve(path.join(DB_ROOT, sanitize(collection_name), '.fsdb.collection.json'));
if (!fs.existsSync(collection_info_path)) {
console.error('fsdb command line interface cannot create collections at this time');
Deno.exit(1);
}
const collection_info = JSON.parse(Deno.readTextFileSync(collection_info_path));
const collection_directory = path.dirname(collection_info_path);
const collection: fsdb.FSDB_COLLECTION<any> = new fsdb.FSDB_COLLECTION<any>({
name: collection_info.name,
root: collection_directory,
id_field: collection_info.id_field
});
switch (command) {
case 'get': {
const id_to_get: string = settings._.shift() as string;
if (typeof id_to_get !== 'string') {
console.error('You must specify an id for the item to get from the collection.');
Deno.exit(1);
}
const item: any = await collection.get(id_to_get);
if (item === null || typeof item === 'undefined') {
console.error(`No item found in collection "${collection_name}" with id: "${id_to_get}"`);
Deno.exit(1);
}
console.log(JSON.stringify(item, null, 4));
break;
}
case 'create': {
const item_json_from_command_line: string | number | undefined = settings._.shift();
let item_json = item_json_from_command_line;
if (typeof item_json !== 'string') {
item_json = await new Response(Deno.stdin.readable).text();
}
if (typeof item_json !== 'string') {
console.error('You must specify some json for the item to create it in the collection.');
Deno.exit(1);
}
const item: any = JSON.parse(item_json);
const created_item: any = await collection.create(item);
console.log('created: ' + JSON.stringify(created_item, null, 4));
break;
}
case 'update': {
const item_json_from_command_line: string | number | undefined = settings._.shift();
let item_json = item_json_from_command_line;
if (typeof item_json !== 'string') {
item_json = await new Response(Deno.stdin.readable).text();
}
if (typeof item_json !== 'string') {
console.error('You must specify some json for the item to update it in the collection.');
Deno.exit(1);
}
const item: any = JSON.parse(item_json);
const updated_item: any = await collection.update(item);
console.log('updated: ' + JSON.stringify(updated_item, null, 4));
break;
}
case 'delete': {
const item_json_from_command_line: string | number | undefined = settings._.shift();
let item_json = item_json_from_command_line;
if (typeof item_json !== 'string') {
item_json = await new Response(Deno.stdin.readable).text();
}
if (typeof item_json !== 'string') {
console.error('You must specify some json for the item to delete it in from the collection.');
Deno.exit(1);
}
const item: any = JSON.parse(item_json);
const deleted_item: any = await collection.delete(item);
console.log('deleted: ' + JSON.stringify(deleted_item, null, 4));
break;
}
case 'find':
console.error('find is not yet implemented - need to find a way to restore the collection indexes on load');
Deno.exit(1);
}
Deno.exit(0);