const yargs = require("yargs") const csv = require("json-2-csv") const db = require("../lib/db") const {exitDelayed} = require("../lib/tools"); ///////////////////////////////////////////////////////////////////////// const argv = yargs .command({ command: "list", describe: "list course admin roles", builder: { format: { alias: "f", demandOption: false, describe: "output format: table(default) | json | csv", type: "string", default: "table" } }, handler: async function ({format}) { const data = await db.getCourseAdminRoles() await print(data, format) exitDelayed() } }) .command({ command: "listn", describe: "list courses without user assigned admin role", builder: { format: { alias: "f", demandOption: false, describe: "output format: table(default) | json | csv", type: "string", default: "table" } }, handler: async function ({format}) { const data = await db.getCourseWithoutAdminRoles() await print(data, format) exitDelayed() } }) // .command({ // command: "get [_id]", // describe: "get db items - if _id is given, get only one document identified by _id", // builder: { // skip: { // demandOption: false, // describe: "skip this number of items", // type: "number", // default: 0, // }, // limit: { // demandOption: false, // describe: "limit to this number of items", // type: "number", // default: 5, // }, // }, // handler: async function ({item, _id, skip, limit}) { // const db = require("../data/db/db") // if (!_id) { // const res = await db[item].find().skip(skip).limit(limit).lean() // console.log(JSON.stringify(res, null, " ")) // } else { // const res = await db[item].findOne({_id}).lean() // console.log(JSON.stringify(res, null, " ")) // } // exitDelayed() // } // }) .alias("h", "help") .demandCommand() .version(false) // .wrap(yargs.terminalWidth()) // .wrap(100) .strict() .argv ///////////////////////////////////////////////////////////////////////// async function print(data, format) { switch (format) { case "table": console.table(data) break; case "json": console.log(JSON.stringify(data, null, " ")) break; case "csv": const res = await csv.json2csv(data) console.log(res) break; default: throw new Error(`unknown format "${format}"`) } }